protected function execute(InputInterface $input, OutputInterface $output) { $targets = $input->getArgument('files'); foreach ($targets as $target) { if (!file_exists($target)) { throw new \Exception("'{$target}' is not a valid path"); } } $analyser = Analyser::buildDefault(); foreach ($targets as $target) { if (is_file($target)) { if (pathinfo($target, PATHINFO_EXTENSION) === 'php') { $analyser->analyseFile($target); } } else { $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($target, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST, \RecursiveIteratorIterator::CATCH_GET_CHILD); foreach ($iterator as $path => $target) { if ($target->isFile() && $target->getExtension() === 'php') { $analyser->analyseFile($path); } } } } $violations = $analyser->getReporter()->getViolations(); $this->printViolations($violations, $output); return count($violations) === 0 ? 0 : 1; }
/** * @dataProvider sampleProvider */ public function testSamples($file, array $expectedViolations) { $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); $set = new Ruleset(); $set->addRule($this->getRule()); $analyser = new Analyser($parser, new NodeTraverser(), $set, new Reporter()); $analyser->analyseFile($file); $violations = $analyser->getReporter()->getViolations(); $count = count($violations); $expectedCount = count($expectedViolations); // @codeCoverageIgnoreStart if ($count === 0 && $expectedCount > 0) { $this->fail("{$expectedCount} violations were expected in {$file}, none found"); } // @codeCoverageIgnoreEnd $stdViolations = array_map(function ($violation) { return $violation->toStdClass(); }, $violations); $this->assertEquals($expectedViolations, $stdViolations, "\nExpected:\n" . json_encode($expectedViolations, JSON_PRETTY_PRINT) . "\nActual:\n" . json_encode($stdViolations, JSON_PRETTY_PRINT)); }
public function testAnalyseValidCodeWithDefaultRuleset() { $analyser = Analyser::buildDefault(); $analyser->analyse('<?php class Foo {}'); $this->assertEquals([], $analyser->getReporter()->getViolations()); }