PhpBench\Benchmark\Metadata\Factory::getMetadataForFile PHP Method

getMetadataForFile() public method

Return a Benchmark instance for the given file or NULL if the given file contains no classes, or the class in the given file is abstract.
public getMetadataForFile ( string $file ) : Benchmark
$file string
return Benchmark
    public function getMetadataForFile($file)
    {
        $hierarchy = $this->reflector->reflect($file);
        if ($hierarchy->isEmpty()) {
            return;
        }
        if ($hierarchy->getTop() && true === $hierarchy->getTop()->abstract) {
            return;
        }
        $metadata = $this->driver->getMetadataForHierarchy($hierarchy);
        $this->validateBenchmark($hierarchy, $metadata);
        // validate the subject and load the parameter sets
        foreach ($metadata->getSubjects() as $subject) {
            $this->validateSubject($hierarchy, $subject);
            $paramProviders = $subject->getParamProviders();
            $parameterSets = $this->reflector->getParameterSets($metadata->getPath(), $paramProviders);
            foreach ($parameterSets as $parameterSet) {
                if (!is_array($parameterSet)) {
                    throw new \InvalidArgumentException(sprintf('Each parameter set must be an array, got "%s" for %s::%s', gettype($parameterSet), $metadata->getClass(), $subject->getName()));
                }
            }
            $subject->setParameterSets($parameterSets);
        }
        return $metadata;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Build the BenchmarkMetadata collection.
  *
  * @param string $path
  * @param array $subjectFilter
  * @param array $groupFilter
  */
 public function findBenchmarks($path, array $subjectFilter = [], array $groupFilter = [])
 {
     $finder = new Finder();
     $path = PhpBench::normalizePath($path);
     if (!file_exists($path)) {
         throw new \InvalidArgumentException(sprintf('File or directory "%s" does not exist (cwd: %s)', $path, getcwd()));
     }
     if (is_dir($path)) {
         $finder->in($path)->name('*.php');
     } else {
         // the path is already a file, just restrict the finder to that.
         $finder->in(dirname($path))->depth(0)->name(basename($path));
     }
     $benchmarks = [];
     foreach ($finder as $file) {
         if (!is_file($file)) {
             continue;
         }
         $benchmark = $this->factory->getMetadataForFile($file->getPathname());
         if (null === $benchmark) {
             continue;
         }
         if ($groupFilter) {
             $benchmark->filterSubjectGroups($groupFilter);
         }
         if ($subjectFilter) {
             $benchmark->filterSubjectNames($subjectFilter);
         }
         if (false === $benchmark->hasSubjects()) {
             continue;
         }
         $benchmarks[] = $benchmark;
     }
     return $benchmarks;
 }
All Usage Examples Of PhpBench\Benchmark\Metadata\Factory::getMetadataForFile