Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,21 @@ 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/
$ 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/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo? (the ,) And is the trailing / mandatory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the trailing slash I usually add them to make clear that it is a directory

```

### Output

There are different output formats available, by default the output is printed in the commandline.
Expand Down
26 changes: 20 additions & 6 deletions src/Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
);
}
Expand All @@ -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;
Expand Down
63 changes: 44 additions & 19 deletions src/DeprecationDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
26 changes: 19 additions & 7 deletions src/DetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/Console/Command/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 10 additions & 2 deletions tests/DeprecationDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -21,6 +22,7 @@ public function testClassIsInitializable()

$detector = new DeprecationDetector(
$preDefinedRuleSet->reveal(),
$sourceRuleSetLoader->reveal(),
$ruleSetLoader->reveal(),
$ancestorResolver->reveal(),
$deprecationFinder->reveal(),
Expand All @@ -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());

Expand Down Expand Up @@ -84,6 +91,7 @@ public function testCheckForDeprecations()

$detector = new DeprecationDetector(
$preDefinedRuleSet->reveal(),
$sourceRuleSetLoader->reveal(),
$ruleSetLoader->reveal(),
$ancestorResolver->reveal(),
$deprecationFinder->reveal(),
Expand All @@ -92,6 +100,6 @@ public function testCheckForDeprecations()
$defaultOutput->reveal()
);

$this->assertSame($violations, $detector->checkForDeprecations($sourceArg, $ruleSetArg));
$this->assertSame($violations, $detector->checkForDeprecations($sources, $ruleSetArg));
}
}
7 changes: 7 additions & 0 deletions tests/Fixtures/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace SensioLabs\DeprecationDetector\Tests\Fixtures;

class SomeClass extends \foo1
{
}