Beispiel #1
0
 public function analyse()
 {
     $analyser = new PHPLOCAnalyser();
     $finder = new FinderFacade([$this->findSourceRoot()]);
     $files = $finder->findFiles();
     return $analyser->countFiles($files, true);
 }
 /**
  * @param TombstoneExtractor $tombstoneExtractor
  * @param string $sourcePath
  */
 public function __construct(TombstoneExtractor $tombstoneExtractor, $sourcePath, $excludeDirs, $excludeFiles, $ignoreErrors)
 {
     $this->ignoreErrors = $ignoreErrors;
     $this->tombstoneExtractor = $tombstoneExtractor;
     $finder = new FinderFacade(array($sourcePath), $excludeDirs, array('*.php'), $excludeFiles);
     $this->files = $finder->findFiles();
 }
Beispiel #3
0
 /**
  * Run the task.
  *
  * @param array &$options Additional options.
  *
  * @return integer Number of errors.
  */
 public function run(&$options)
 {
     $finder = new FinderFacade(array(realpath($this->_config->getPath())));
     $files = $finder->findFiles();
     $analyser = new PHPLOC\Analyser();
     $count = $analyser->countFiles($files, true);
     $this->_printResult($count);
 }
Beispiel #4
0
 /**
  * Run the task.
  *
  * @param array &$options Additional options.
  *
  * @return integer Number of errors.
  */
 public function run(&$options)
 {
     $finder = new FinderFacade(array(realpath($this->_config->getPath() . '/lib')));
     $files = $finder->findFiles();
     $detector = new PHPDCD\Detector();
     $result = $detector->detectDeadCode($files, true);
     $this->_printResult($result);
 }
Beispiel #5
0
 /**
  * Run the task.
  *
  * @param array &$options Additional options.
  *
  * @return integer Number of errors.
  */
 public function run(&$options)
 {
     $finder = new FinderFacade(array(realpath($this->_config->getPath() . '/lib')));
     $files = $finder->findFiles();
     $detector = new PHPCPD\Detector\Detector(new PHPCPD\Detector\Strategy\DefaultStrategy());
     $clones = $detector->copyPasteDetection($files, 5, 70);
     $this->_printResult($clones);
     return count($clones);
 }
Beispiel #6
0
 /**
  * Handle provided directory
  *
  * Optionally an existing result file can be provided
  *
  * If a valid file could be generated the file name is supposed to be
  * returned, otherwise return null.
  *
  * @param Project $project
  * @param string $existingResult
  * @return string
  */
 public function handle(Project $project, $existingResult = null)
 {
     $zipFile = __DIR__ . '/../../../../../data/source.zip';
     $archive = new \ZipArchive();
     $archive->open($zipFile, \ZipArchive::OVERWRITE | \ZipArchive::CREATE);
     $finder = new FinderFacade(array($project->baseDir), $project->excludes, array('*.php'));
     foreach ($finder->findFiles() as $existingResult) {
         $archive->addFile($existingResult, ltrim(str_replace($project->baseDir, '', $existingResult), '/'));
     }
     $archive->close();
 }
Beispiel #7
0
 /**
  * Handle provided directory
  *
  * Optionally an existing result file can be provided
  *
  * @param string $dir
  * @param array $excludes
  * @param string $file
  * @return void
  */
 public function handle($dir, array $excludes, $file = null)
 {
     $zipFile = __DIR__ . '/../../../../../data/source.zip';
     $archive = new \ZipArchive();
     $archive->open($zipFile, \ZipArchive::OVERWRITE | \ZipArchive::CREATE);
     $finder = new FinderFacade(array($dir), $excludes, array('*.php'));
     foreach ($finder->findFiles() as $file) {
         $archive->addFile($file, ltrim(str_replace($dir, '', $file), '/'));
     }
     $archive->close();
 }
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|integer null or 0 if everything went fine, or an error code
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $mergedCoverage = new PHP_CodeCoverage();
     $finder = new FinderFacade(array($input->getArgument('directory')), array(), array('*.cov'));
     foreach ($finder->findFiles() as $file) {
         $_coverage = (include $file);
         $mergedCoverage->merge($_coverage);
         unset($_coverage);
     }
     $this->handleReports($mergedCoverage, $input, $output);
 }
Beispiel #9
0
 /**
  *
  * @return int 0 if success, otherwise failure
  */
 public function run()
 {
     try {
         list($includePaths, $includeFiles) = $this->fillPathsAndFiles('include_paths');
         list($excludePaths, $excludeFiles) = $this->fillPathsAndFiles('exclude_paths');
         $finder = new FinderFacade($includePaths, $excludePaths, $includeFiles, $excludeFiles);
         $files = $finder->findFiles();
         if (empty($files)) {
             return 0;
         }
         $this->analyze($files);
     } catch (\Exception $e) {
         $this->errorWriter->write($e->getMessage());
         return 1;
     }
     return 0;
 }
Beispiel #10
0
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $finder = new FinderFacade($input->getArgument('values'), $input->getOption('exclude'), $this->handleCSVOption($input, 'names'), $this->handleCSVOption($input, 'names-exclude'));
     $files = $finder->findFiles();
     if (empty($files)) {
         $output->writeln('No files found to scan');
         exit(1);
     }
     $quiet = $output->getVerbosity() == OutputInterface::VERBOSITY_QUIET;
     $detector = new Detector();
     $result = $detector->detectDeadCode($files, $input->getOption('recursive'));
     if (!$quiet) {
         $printer = new Text();
         $printer->printResult($output, $result);
         $output->writeln(\PHP_Timer::resourceUsage());
     }
 }
Beispiel #11
0
 /**
  * Handle provided directory
  *
  * Optionally an existing result file can be provided
  *
  * @param string $dir
  * @param array $excludes
  * @param string $file
  * @return void
  */
 public function handle($dir, array $excludes, $file = null)
 {
     $currentDir = getcwd();
     chdir($dir);
     if ($excludes) {
         $finder = new FinderFacade(array($dir), array(), $excludes);
         $excludes = array_map(function ($path) use($dir) {
             $path = substr($path, strlen($dir) + 1);
             return is_file($path) ? $path : $path . '/*';
         }, $finder->findFiles());
         array_unshift($excludes, '-x');
     }
     $zipFile = __DIR__ . '/../../../../../data/source.zip';
     if (file_exists($zipFile)) {
         unlink($zipFile);
     }
     $this->shell->exec('zip', array_merge(array('-r', $zipFile, './', '-i', '*.php'), $excludes));
     chdir($currentDir);
 }
Beispiel #12
0
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|integer null or 0 if everything went fine, or an error code
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $finder = new FinderFacade($input->getArgument('values'), $input->getOption('exclude'), $this->handleCSVOption($input, 'names'), $this->handleCSVOption($input, 'names-exclude'));
     $files = $finder->findFiles();
     if (empty($files)) {
         $output->writeln('No files found to scan');
         exit(1);
     }
     $progressHelper = null;
     if ($input->getOption('progress')) {
         $progressHelper = $this->getHelperSet()->get('progress');
         $progressHelper->start($output, count($files));
     }
     $strategy = new DefaultStrategy();
     $detector = new Detector($strategy, $progressHelper);
     $quiet = $output->getVerbosity() == OutputInterface::VERBOSITY_QUIET;
     $clones = $detector->copyPasteDetection($files, $input->getOption('min-lines'), $input->getOption('min-tokens'), $input->getOption('fuzzy'));
     if ($input->getOption('progress')) {
         $progressHelper->finish();
         $output->writeln('');
     }
     if (!$quiet) {
         $printer = new Text();
         $printer->printResult($output, $clones);
         unset($printer);
     }
     $logPmd = $input->getOption('log-pmd');
     if ($logPmd) {
         $pmd = new PMD($logPmd);
         $pmd->processClones($clones);
         unset($pmd);
     }
     if (!$quiet) {
         print \PHP_Timer::resourceUsage() . "\n";
     }
     if (count($clones) > 0) {
         exit(1);
     }
 }
Beispiel #13
0
 /**
  * @throws \Exception
  */
 public function handle()
 {
     \Log::info('QA::PHPCPD Run copy past detector');
     $detector = new Detector(new DefaultStrategy());
     $finder = new FinderFacade(['app'], []);
     $files = $finder->findFiles();
     foreach ($files as $key => $value) {
         if (!is_file($value)) {
             unset($files[$key]);
         }
     }
     $clones = $detector->copyPasteDetection($files);
     if ($this->getOutput()->getVerbosity() > 1) {
         $printer = new Text();
         $printer->printResult($this->getOutput(), $clones);
         unset($printer);
     }
     $percentage = floatval($clones->getPercentage());
     if ($percentage > $this->option('limit')) {
         $this->error('[Shame] The copy/paste percentage is ' . $percentage);
         throw new \Exception('Your code is bad, and you should feel bad');
     }
 }
 /**
  * @param LogReader $logReader
  * @param string $logDir
  */
 public function __construct(LogReader $logReader, $logDir)
 {
     $this->logReader = $logReader;
     $finder = new FinderFacade(array($logDir), array(), array('*.tombstone'));
     $this->files = $finder->findFiles();
 }
Beispiel #15
0
 /**
  * Processes a directory
  *
  * @param array $arguments
  * @param $excludes
  * @param $names
  * @param $namesExclude
  * @param $countTests
  * @return array|bool
  */
 private function count(array $arguments, $excludes, $names, $namesExclude, $countTests)
 {
     try {
         $finder = new FinderFacade($arguments, $excludes, $names, $namesExclude);
         $files = $finder->findFiles();
     } catch (\InvalidArgumentException $ex) {
         return false;
     }
     if (empty($files)) {
         return false;
     }
     $analyser = new Analyser();
     return $analyser->countFiles($files, $countTests);
 }
<?php

/**
 * Generate report
 * adopted and modified version from http://stackoverflow.com/questions/28120280/how-to-generate-php-code-coverage-reports-from-xdebug-output
 */
require_once 'vendor/autoload.php';
use SebastianBergmann\FinderFacade\FinderFacade;
$finder = new FinderFacade(array('build/coverage'), array(), array('*.cov'));
foreach ($finder->findFiles() as $key => $filename) {
    if (isset($codeCoverage)) {
        $_coverage = (include $filename);
        $codeCoverage->filter()->addFilesToWhitelist($_coverage->filter()->getWhitelist());
        $codeCoverage->merge($_coverage);
        unset($_coverage);
    } else {
        $codeCoverage = (include $filename);
    }
}
print "\nGenerating code coverage report in Clover format ...";
$writer = new PHP_CodeCoverage_Report_Clover();
$writer->process($codeCoverage, 'build/logs/clover.xml');
print "Generating code coverage report in Clover format done\n";
Beispiel #17
0
 /**
  * Main method.
  */
 public function main()
 {
     $input = new \ezcConsoleInput();
     $input->registerOption(new \ezcConsoleOption('', 'exclude', \ezcConsoleInput::TYPE_STRING, array(), TRUE));
     $input->registerOption(new \ezcConsoleOption('h', 'help', \ezcConsoleInput::TYPE_NONE, NULL, FALSE, '', '', array(), array(), FALSE, FALSE, TRUE));
     $input->registerOption(new \ezcConsoleOption('', 'log-pmd', \ezcConsoleInput::TYPE_STRING));
     $input->registerOption(new \ezcConsoleOption('', 'min-lines', \ezcConsoleInput::TYPE_INT, 5));
     $input->registerOption(new \ezcConsoleOption('', 'min-tokens', \ezcConsoleInput::TYPE_INT, 70));
     $input->registerOption(new \ezcConsoleOption('', 'names', \ezcConsoleInput::TYPE_STRING, '*.php', FALSE));
     $input->registerOption(new \ezcConsoleOption('', 'quiet', \ezcConsoleInput::TYPE_NONE, NULL, FALSE));
     $input->registerOption(new \ezcConsoleOption('v', 'version', \ezcConsoleInput::TYPE_NONE, NULL, FALSE, '', '', array(), array(), FALSE, FALSE, TRUE));
     $input->registerOption(new \ezcConsoleOption('', 'progress', \ezcConsoleInput::TYPE_NONE));
     $input->registerOption(new \ezcConsoleOption('', 'verbose', \ezcConsoleInput::TYPE_NONE));
     try {
         $input->process();
     } catch (\ezcConsoleOptionException $e) {
         print $e->getMessage() . "\n";
         exit(1);
     }
     if ($input->getOption('help')->value) {
         $this->showHelp();
         exit(0);
     } else {
         if ($input->getOption('version')->value) {
             $this->printVersionString();
             exit(0);
         }
     }
     $arguments = $input->getArguments();
     if (empty($arguments)) {
         $this->showHelp();
         exit(1);
     }
     $excludes = $input->getOption('exclude')->value;
     $logPmd = $input->getOption('log-pmd')->value;
     $minLines = $input->getOption('min-lines')->value;
     $minTokens = $input->getOption('min-tokens')->value;
     $names = explode(',', $input->getOption('names')->value);
     $quiet = $input->getOption('quiet')->value;
     $verbose = $input->getOption('verbose')->value;
     array_map('trim', $names);
     if ($input->getOption('progress')->value !== FALSE) {
         $output = new \ezcConsoleOutput();
     } else {
         $output = NULL;
     }
     $this->printVersionString();
     $finder = new FinderFacade($arguments, $excludes, $names);
     $files = $finder->findFiles();
     if (empty($files)) {
         $this->showError("No files found to scan.\n");
     }
     $strategy = new DefaultStrategy();
     $detector = new Detector($strategy, $output);
     $clones = $detector->copyPasteDetection($files, $minLines, $minTokens);
     $printer = new ResultPrinter();
     $printer->printResult($clones, !$quiet, $verbose);
     unset($printer);
     if ($logPmd) {
         $pmd = new PMD($logPmd);
         $pmd->processClones($clones);
         unset($pmd);
     }
     if (count($clones) > 0) {
         exit(1);
     }
 }
 /**
  * @param TombstoneExtractor $tombstoneExtractor
  * @param string $sourcePath
  * @param array $regularExpressions Match source files against passed patterns. Defaults to ['*.php']
  */
 public function __construct(TombstoneExtractor $tombstoneExtractor, $sourcePath, $regularExpressions = array('*.php'))
 {
     $this->tombstoneExtractor = $tombstoneExtractor;
     $finder = new FinderFacade(array($sourcePath), array(), $regularExpressions);
     $this->files = $finder->findFiles();
 }
Beispiel #19
0
 private function count(array $arguments, $excludes, $names, $namesExclude, $countTests)
 {
     $finder = new FinderFacade($arguments, $excludes, $names, $namesExclude);
     $files = $finder->findFiles();
     if (empty($files)) {
         return FALSE;
     }
     $analyser = new Analyser();
     return $analyser->countFiles($files, $countTests);
 }
 /**
  * @return array
  */
 public function getDuplicatedCode()
 {
     $finder = new FinderFacade(array($this->getRealPath()), array(), array('*.php'));
     $detector = new Detector(new DefaultStrategy());
     $clones = $detector->copyPasteDetection($finder->findFiles());
     $numClones = count($clones);
     $summary = array();
     if ($numClones > 0) {
         $files = array();
         $locations = array();
         $lines = 0;
         foreach ($clones as $clone) {
             foreach ($clone->getFiles() as $file) {
                 $filename = $file->getName();
                 if (!isset($files[$filename])) {
                     $files[$filename] = true;
                 }
             }
             $lines += $clone->getSize() * (count($clone->getFiles()) - 1);
             foreach ($clone->getFiles() as $file) {
                 $locations[] = sprintf("%s:%d-%d", $file->getName(), $file->getStartLine(), $file->getStartLine() + $clone->getSize());
             }
         }
         $summary = array('num_clones' => $numClones, 'duplicated_lines' => $lines, 'num_files' => count($files), 'locations' => $locations);
     }
     $summary['percentage'] = $clones->getPercentage();
     $summary['total_loc'] = $clones->getNumLines();
     return $summary;
 }
 /**
  * @covers SebastianBergmann\FinderFacade\FinderFacade::loadConfiguration
  */
 public function testFilesCanBeFoundBasedOnXmlConfiguration()
 {
     $facade = new FinderFacade();
     $facade->loadConfiguration($this->fixtureDir . 'test.xml');
     $this->assertEquals(array($this->fixtureDir . 'bar.phtml', $this->fixtureDir . 'foo' . DIRECTORY_SEPARATOR . 'bar.php'), $facade->findFiles());
 }
Beispiel #22
0
#!/usr/bin/env php
<?php 
if (!file_exists(__DIR__ . '/../vendor/autoload.php')) {
    die('You need to set up the project dependencies using the following commands:' . PHP_EOL . 'wget https://getcomposer.org/composer.phar' . PHP_EOL . 'php composer.phar install' . PHP_EOL);
}
require __DIR__ . '/../vendor/autoload.php';
use SebastianBergmann\FinderFacade\FinderFacade;
$finder = new FinderFacade(array(__DIR__ . '/../example/src'), array(), array('*.php'));
print 'digraph G {' . PHP_EOL;
foreach ($finder->findFiles() as $file) {
    $file = new PHP_Token_Stream($file);
    foreach ($file->getClasses() as $className => $class) {
        if ($class['parent'] == 'AbstractDoorState') {
            foreach ($class['methods'] as $methodName => $method) {
                if (!in_array($methodName, array('open', 'close', 'lock', 'unlock'))) {
                    continue;
                }
                $annotations = array('return' => array());
                if (preg_match_all('/@(?P<name>[A-Za-z_-]+)(?:[ \\t]+(?P<value>.*?))?[ \\t]*\\r?$/m', $method['docblock'], $matches)) {
                    for ($i = 0; $i < count($matches[0]); ++$i) {
                        $annotations[$matches['name'][$i]][] = $matches['value'][$i];
                    }
                }
                foreach ($annotations['return'] as $return) {
                    printf('  "%s" -> "%s";' . PHP_EOL, str_replace(array('Door', 'State'), array(' Door', ''), $className), str_replace(array('Door', 'State'), array(' Door', ''), $return));
                }
            }
        }
    }
}
print '}' . PHP_EOL;