diff --git a/README.md b/README.md index 4ee467b..4b7de7d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,8 @@ Otherwise you can call `bin/deprecation-detector` directly. ## Usage -To use the DeprecationDetector you need to provide the `source` and the `ruleset` arguments +To use the DeprecationDetector you need to provide the `source` and the `ruleset` arguments. +Please note that rulesets are automatically generated for every source argument. ```bash $ deprecation-detector check src/ vendor/ @@ -73,6 +74,12 @@ $ deprecation-detector check src/ composer.lock $ deprecation-detector check src/ .rules/some_generated_rule_set ``` +If you have different source paths you can pass them seperated by a comma. + +```bash +$ deprecation-detector check src/,legacy/ vendor/ +``` + ### Output There are different output formats available, by default the output is printed in the commandline. diff --git a/src/Console/Command/CheckCommand.php b/src/Console/Command/CheckCommand.php index d357bcd..f27c389 100644 --- a/src/Console/Command/CheckCommand.php +++ b/src/Console/Command/CheckCommand.php @@ -92,15 +92,29 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $sourceArg = realpath($input->getArgument('source')); + $sourcesRaw = explode(',', $input->getArgument('source')); + $sources = array(); + foreach ($sourcesRaw as $sourceRaw) { + $source = realpath($sourceRaw); + if (false === $source) { + throw new \InvalidArgumentException( + sprintf( + 'Source directory argument is invalid: "%s" is not a path.', + $sourceRaw + ) + ); + } + + $sources[] = $source; + } + $ruleSetArg = realpath($input->getArgument('ruleset')); - if (false === $sourceArg || false === $ruleSetArg) { + if (false === $ruleSetArg) { throw new \InvalidArgumentException( sprintf( - '%s argument is invalid: "%s" is not a path.', - $sourceArg ? 'Rule set' : 'Source directory', - $sourceArg ? $input->getArgument('ruleset') : $input->getArgument('source') + 'Rule set argument is invalid: "%s" is not a path.', + $input->getArgument('ruleset') ) ); } @@ -118,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $factory = new DetectorFactory(); $detector = $factory->create($config, $output); - $violations = $detector->checkForDeprecations($sourceArg, $ruleSetArg); + $violations = $detector->checkForDeprecations($sources, $ruleSetArg); if ($config->failOnDeprecation() && !empty($violations)) { return 1; diff --git a/src/DeprecationDetector.php b/src/DeprecationDetector.php index 6af04b3..c37dc49 100644 --- a/src/DeprecationDetector.php +++ b/src/DeprecationDetector.php @@ -4,6 +4,7 @@ use SensioLabs\DeprecationDetector\Console\Output\DefaultProgressOutput; use SensioLabs\DeprecationDetector\Finder\ParsedPhpFileFinder; +use SensioLabs\DeprecationDetector\RuleSet\Loader\DirectoryLoader; use SensioLabs\DeprecationDetector\RuleSet\Loader\LoaderInterface; use SensioLabs\DeprecationDetector\RuleSet\RuleSet; use SensioLabs\DeprecationDetector\TypeGuessing\AncestorResolver; @@ -42,18 +43,24 @@ class DeprecationDetector * @var DefaultProgressOutput */ private $output; + /** + * @var DirectoryLoader + */ + private $sourceRuleSetLoader; /** - * @param RuleSet $preDefinedRuleSet - * @param LoaderInterface $ruleSetLoader - * @param AncestorResolver $ancestorResolver - * @param ParsedPhpFileFinder $deprecationFinder - * @param ViolationDetector $violationDetector - * @param RendererInterface $renderer + * @param RuleSet $preDefinedRuleSet + * @param DirectoryLoader $sourceRuleSetLoader + * @param LoaderInterface $ruleSetLoader + * @param AncestorResolver $ancestorResolver + * @param ParsedPhpFileFinder $deprecationFinder + * @param ViolationDetector $violationDetector + * @param RendererInterface $renderer * @param DefaultProgressOutput $output */ public function __construct( RuleSet $preDefinedRuleSet, + DirectoryLoader $sourceRuleSetLoader, LoaderInterface $ruleSetLoader, AncestorResolver $ancestorResolver, ParsedPhpFileFinder $deprecationFinder, @@ -68,43 +75,61 @@ public function __construct( $this->violationDetector = $violationDetector; $this->renderer = $renderer; $this->output = $output; + $this->sourceRuleSetLoader = $sourceRuleSetLoader; } /** - * @param string $sourceArg - * @param string $ruleSetArg + * @param string[] $sources + * @param string $ruleSetArg * * @return Violation[] * * @throws \Exception */ - public function checkForDeprecations($sourceArg, $ruleSetArg) + public function checkForDeprecations(array $sources, $ruleSetArg) { $this->output->startProgress(); $this->output->startRuleSetGeneration(); $ruleSet = $this->ruleSetLoader->loadRuleSet($ruleSetArg); $ruleSet->merge($this->preDefinedRuleSet); - $this->output->endRuleSetGeneration(); + foreach ($sources as $source) { + $ruleSet->merge($this->sourceRuleSetLoader->loadRuleSet($source)); + } + $this->output->endRuleSetGeneration(); $this->output->startUsageDetection(); // TODO: Move to AncestorResolver not hard coded $lib = (is_dir($ruleSetArg) ? $ruleSetArg : realpath('vendor')); - $this->ancestorResolver->setSourcePaths(array( - $sourceArg, - $lib, - )); - - $result = $this->deprecationFinder->parsePhpFiles($sourceArg); - $violations = $this->violationDetector->getViolations($ruleSet, $result->parsedFiles()); + $sourcePaths = array(); + $sourcePaths = array_merge($sourcePaths, $sources); + $sourcePaths[] = $lib; + $this->ancestorResolver->setSourcePaths($sourcePaths); + + $results = array(); + $violations = array(); + foreach ($sources as $source) { + $result = $this->deprecationFinder->parsePhpFiles($source); + $results[] = $result; + foreach ($this->violationDetector->getViolations($ruleSet, $result->parsedFiles()) as $violation) { + $violations[] = $violation; + } + } + + $errors = array(); + $fileCount = 0; + foreach ($results as $result) { + $errors = array_merge($errors, $result->parserErrors()); + $fileCount += $result->fileCount(); + } $this->output->endUsageDetection(); $this->output->startOutputRendering(); - $this->renderer->renderViolations($violations, $result->parserErrors()); + $this->renderer->renderViolations($violations, $errors); $this->output->endOutputRendering(); - $this->output->endProgress($result->fileCount(), count($violations)); + $this->output->endProgress($fileCount, count($violations)); return $violations; } diff --git a/src/DetectorFactory.php b/src/DetectorFactory.php index 0df970b..971daea 100644 --- a/src/DetectorFactory.php +++ b/src/DetectorFactory.php @@ -133,6 +133,7 @@ public function create(Configuration $configuration, OutputInterface $output) return new DeprecationDetector( $this->getPredefinedRuleSet(), + new DirectoryLoader($deprecationDirectoryTraverser, $this->getRuleSetCache($configuration)), $ruleSetLoader, $this->ancestorResolver, $deprecationUsageFinder, @@ -343,6 +344,23 @@ private function getDeprecationParser() ); } + /** + * @param Configuration $configuration + * @return Cache + */ + private function getRuleSetCache(Configuration $configuration) + { + $ruleSetCache = new Cache(new Filesystem()); + + if ($configuration->useCachedRuleSet()) { + $ruleSetCache->disable(); + } else { + $ruleSetCache->setCacheDir($configuration->ruleSetCacheDir()); + } + + return $ruleSetCache; + } + /** * RuleSet. */ @@ -355,13 +373,7 @@ private function getDeprecationParser() */ private function getRuleSetLoader(DirectoryTraverser $traverser, Configuration $configuration) { - $ruleSetCache = new Cache(new Filesystem()); - - if ($configuration->useCachedRuleSet()) { - $ruleSetCache->disable(); - } else { - $ruleSetCache->setCacheDir($configuration->ruleSetCacheDir()); - } + $ruleSetCache = $this->getRuleSetCache($configuration); if (is_dir($configuration->ruleSet())) { $loader = new DirectoryLoader($traverser, $ruleSetCache); diff --git a/tests/Console/Command/CheckCommandTest.php b/tests/Console/Command/CheckCommandTest.php index 26bfddd..52a3f94 100644 --- a/tests/Console/Command/CheckCommandTest.php +++ b/tests/Console/Command/CheckCommandTest.php @@ -71,6 +71,14 @@ public function testCommandWithExampleCodeWorks() $this->assertRegExp('/34 deprecations found/', $this->commandTester->getDisplay()); } + public function testCommandWithMultipleSourcePathsWorks() + { + $this->executeCommand('examples,tests/Fixtures', 'examples'); + + $this->assertEquals(0, $this->commandTester->getStatusCode()); + $this->assertRegExp('/35 deprecations found/', $this->commandTester->getDisplay()); + } + public function testCommandWithFailOption() { $this->executeCommand('examples', 'examples', array('--fail' => true)); diff --git a/tests/DeprecationDetectorTest.php b/tests/DeprecationDetectorTest.php index d60e48d..e1ac140 100644 --- a/tests/DeprecationDetectorTest.php +++ b/tests/DeprecationDetectorTest.php @@ -10,6 +10,7 @@ class DeprecationDetectorTest extends \PHPUnit_Framework_TestCase public function testClassIsInitializable() { $preDefinedRuleSet = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\RuleSet'); + $sourceRuleSetLoader = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\Loader\DirectoryLoader'); $ruleSetLoader = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\Loader\LoaderInterface'); $ancestorResolver = $this->prophesize('SensioLabs\DeprecationDetector\TypeGuessing\AncestorResolver'); $deprecationFinder = $this->prophesize('SensioLabs\DeprecationDetector\Finder\ParsedPhpFileFinder'); @@ -21,6 +22,7 @@ public function testClassIsInitializable() $detector = new DeprecationDetector( $preDefinedRuleSet->reveal(), + $sourceRuleSetLoader->reveal(), $ruleSetLoader->reveal(), $ancestorResolver->reveal(), $deprecationFinder->reveal(), @@ -36,13 +38,18 @@ public function testCheckForDeprecations() { $preDefinedRuleSet = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\RuleSet'); - $sourceArg = 'path/to/ruleset'; + $sources = array($sourceArg = 'path/to/ruleset'); $ruleSetArg = 'path/to/source/code'; $fileCount = 10; $violationCount = 2; + $sourceRuleSet = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\RuleSet'); + $sourceRuleSetLoader = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\Loader\DirectoryLoader'); + $sourceRuleSetLoader->loadRuleSet($sourceArg)->willReturn($sourceRuleSet->reveal()); + $ruleSet = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\RuleSet'); $ruleSet->merge($preDefinedRuleSet->reveal())->shouldBeCalled(); + $ruleSet->merge($sourceRuleSet->reveal())->shouldBeCalled(); $ruleSetLoader = $this->prophesize('SensioLabs\DeprecationDetector\RuleSet\Loader\LoaderInterface'); $ruleSetLoader->loadRuleSet($ruleSetArg)->willReturn($ruleSet->reveal()); @@ -84,6 +91,7 @@ public function testCheckForDeprecations() $detector = new DeprecationDetector( $preDefinedRuleSet->reveal(), + $sourceRuleSetLoader->reveal(), $ruleSetLoader->reveal(), $ancestorResolver->reveal(), $deprecationFinder->reveal(), @@ -92,6 +100,6 @@ public function testCheckForDeprecations() $defaultOutput->reveal() ); - $this->assertSame($violations, $detector->checkForDeprecations($sourceArg, $ruleSetArg)); + $this->assertSame($violations, $detector->checkForDeprecations($sources, $ruleSetArg)); } } diff --git a/tests/Fixtures/SomeClass.php b/tests/Fixtures/SomeClass.php new file mode 100644 index 0000000..de3921a --- /dev/null +++ b/tests/Fixtures/SomeClass.php @@ -0,0 +1,7 @@ +