/** * 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); }
/** * 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); } }
/** * @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'); } }
/** * 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); } }
/** * @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; }
/** * @param $files * * @return array */ protected function analyze($files) { $strategy = new DefaultStrategy(); $detector = new Detector($strategy); $clones = $detector->copyPasteDetection($files, $this->config['lines'], $this->config['tokens'], $this->config['fuzzy']); $reports = $this->formatter->formatResults($clones); foreach ($reports as $key => $report) { $this->defaultWriter->write($report); } }