Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 8427920

Browse files
committed
Merge pull request #100 from MarvinKlemp/fix/parsedphpfilefinder
Changed the ParsedPhpFileFinder into a stateless service
2 parents c5f4692 + 9856cee commit 8427920

22 files changed

+238
-209
lines changed

src/DeprecationDetector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,15 @@ public function checkForDeprecations($sourceArg, $ruleSetArg)
9696
$lib,
9797
));
9898

99-
/** @var ParsedPhpFileFinder $files */
100-
$files = $this->deprecationFinder->in($sourceArg);
101-
$violations = $this->violationDetector->getViolations($ruleSet, $files);
99+
$result = $this->deprecationFinder->parsePhpFiles($sourceArg);
100+
$violations = $this->violationDetector->getViolations($ruleSet, $result->parsedFiles());
102101
$this->output->endUsageDetection();
103102

104103
$this->output->startOutputRendering();
105-
$this->renderer->renderViolations($violations, $files->getParserErrors());
104+
$this->renderer->renderViolations($violations, $result->parserErrors());
106105
$this->output->endOutputRendering();
107106

108-
$this->output->endProgress($files->count(), count($violations));
107+
$this->output->endProgress($result->fileCount(), count($violations));
109108

110109
return $violations;
111110
}

src/DetectorFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use SensioLabs\DeprecationDetector\Console\Output\VerboseProgressOutput;
1010
use SensioLabs\DeprecationDetector\FileInfo\Deprecation\FunctionDeprecation;
1111
use SensioLabs\DeprecationDetector\FileInfo\Deprecation\MethodDeprecation;
12+
use SensioLabs\DeprecationDetector\Finder\DeprecationFinderFactory;
1213
use SensioLabs\DeprecationDetector\Finder\ParsedPhpFileFinder;
14+
use SensioLabs\DeprecationDetector\Finder\UsageFinderFactory;
1315
use SensioLabs\DeprecationDetector\Parser\DeprecationParser;
1416
use SensioLabs\DeprecationDetector\Parser\UsageParser;
1517
use SensioLabs\DeprecationDetector\RuleSet\Cache;
@@ -100,9 +102,10 @@ public function create(Configuration $configuration, OutputInterface $output)
100102
'Deprecation detection'
101103
);
102104
$deprecationUsageParser = $this->getUsageParser($configuration);
103-
$deprecationUsageFinder = ParsedPhpFileFinder::usageFinder(
105+
$deprecationUsageFinder = new ParsedPhpFileFinder(
104106
$deprecationUsageParser,
105-
$deprecationProgressOutput
107+
$deprecationProgressOutput,
108+
new UsageFinderFactory()
106109
);
107110

108111
$this->ancestorResolver = new AncestorResolver($deprecationUsageParser);
@@ -113,9 +116,10 @@ public function create(Configuration $configuration, OutputInterface $output)
113116
'RuleSet generation'
114117
);
115118
$ruleSetDeprecationParser = $this->getDeprecationParser();
116-
$ruleSetDeprecationFinder = ParsedPhpFileFinder::deprecationFinder(
119+
$ruleSetDeprecationFinder = new ParsedPhpFileFinder(
117120
$ruleSetDeprecationParser,
118-
$ruleSetProgressOutput
121+
$ruleSetProgressOutput,
122+
new DeprecationFinderFactory()
119123
);
120124
$deprecationDirectoryTraverser = new DirectoryTraverser($ruleSetDeprecationFinder);
121125

@@ -288,6 +292,7 @@ private function getViolationFilter(Configuration $configuration)
288292
/**
289293
* @param Configuration $configuration
290294
* @param OutputInterface $output
295+
*
291296
* @return ConsoleOutputRenderer|Violation\Renderer\Html\Renderer
292297
*/
293298
private function getRenderer(Configuration $configuration, OutputInterface $output)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Finder;
4+
5+
use Symfony\Component\Finder\Finder;
6+
7+
class DeprecationFinderFactory implements FinderFactoryInterface
8+
{
9+
public function createFinder()
10+
{
11+
$finder = new Finder();
12+
$finder
13+
->name('*.php')
14+
->contains('@deprecated')
15+
->exclude('vendor')
16+
->exclude('Tests')
17+
->exclude('Test');
18+
19+
return $finder;
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Finder;
4+
5+
use Symfony\Component\Finder\Finder;
6+
7+
interface FinderFactoryInterface
8+
{
9+
/**
10+
* @return Finder
11+
*/
12+
public function createFinder();
13+
}

src/Finder/ParsedPhpFileFinder.php

Lines changed: 24 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,134 +5,68 @@
55
use SensioLabs\DeprecationDetector\Console\Output\VerboseProgressOutput;
66
use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo;
77
use SensioLabs\DeprecationDetector\Parser\ParserInterface;
8-
use Symfony\Component\Finder\Finder;
8+
use PhpParser\Error;
99

10-
class ParsedPhpFileFinder extends Finder
10+
class ParsedPhpFileFinder
1111
{
1212
/**
1313
* @var ParserInterface
1414
*/
15-
protected $parser;
15+
private $parser;
1616

1717
/**
1818
* @var VerboseProgressOutput
1919
*/
20-
protected $progressOutput;
20+
private $progressOutput;
2121

2222
/**
23-
* @var \PhpParser\Error[]
23+
* @var FinderFactoryInterface
2424
*/
25-
protected $parserErrors = array();
25+
private $finderFactory;
2626

2727
/**
28-
* @param ParserInterface $parser
29-
* @param VerboseProgressOutput $progressOutput
28+
* @param ParserInterface $parser
29+
* @param VerboseProgressOutput $progressOutput
30+
* @param FinderFactoryInterface $finderFactory
3031
*/
31-
public function __construct(ParserInterface $parser, VerboseProgressOutput $progressOutput)
32+
public function __construct(ParserInterface $parser, VerboseProgressOutput $progressOutput, FinderFactoryInterface $finderFactory)
3233
{
33-
parent::__construct();
34-
3534
$this->parser = $parser;
36-
$this->files()->name('*.php');
37-
3835
$this->progressOutput = $progressOutput;
36+
$this->finderFactory = $finderFactory;
3937
}
4038

4139
/**
42-
* @return \Iterator
40+
* @param string $path
41+
*
42+
* @return Result
4343
*/
44-
public function getIterator()
44+
public function parsePhpFiles($path)
4545
{
46-
$iterator = parent::getIterator();
47-
$files = new \ArrayIterator();
48-
$total = $this->count();
46+
$files = $this->finderFactory->createFinder()->in($path);
47+
$parsedFiles = array();
48+
$parserErrors = array();
4949

50-
$this->progressOutput->start($total);
50+
$this->progressOutput->start($fileCount = $files->count());
5151

5252
$i = 0;
53-
foreach ($iterator as $file) {
53+
foreach ($files->getIterator() as $file) {
5454
$file = PhpFileInfo::create($file);
5555

5656
try {
5757
$this->progressOutput->advance(++$i, $file);
5858
$this->parser->parseFile($file);
59-
} catch (\PhpParser\Error $ex) {
59+
} catch (Error $ex) {
6060
$raw = $ex->getRawMessage().' in file '.$file;
6161
$ex->setRawMessage($raw);
62-
$this->parserErrors[] = $ex;
62+
$parserErrors[] = $ex;
6363
}
6464

65-
$files->append($file);
65+
$parsedFiles[] = $file;
6666
}
6767

6868
$this->progressOutput->end();
6969

70-
return $files;
71-
}
72-
73-
/**
74-
* @return int
75-
*/
76-
public function count()
77-
{
78-
return iterator_count(parent::getIterator());
79-
}
80-
81-
/**
82-
* @return \PhpParser\Error[]
83-
*/
84-
public function getParserErrors()
85-
{
86-
return $this->parserErrors;
87-
}
88-
89-
/**
90-
* @param ParserInterface $parser
91-
* @param VerboseProgressOutput $progressOutput
92-
* @return ParsedPhpFileFinder
93-
*/
94-
public static function deprecationFinder(ParserInterface $parser, VerboseProgressOutput $progressOutput)
95-
{
96-
$finder = new ParsedPhpFileFinder($parser, $progressOutput);
97-
$finder
98-
->contains('@deprecated')
99-
->exclude('vendor')
100-
->exclude('Tests')
101-
->exclude('Test');
102-
103-
return $finder;
104-
105-
}
106-
107-
/**
108-
* @param ParserInterface $parser
109-
* @param VerboseProgressOutput $progressOutput
110-
* @return ParsedPhpFileFinder
111-
*/
112-
public static function usageFinder(ParserInterface $parser, VerboseProgressOutput $progressOutput)
113-
{
114-
$finder = new ParsedPhpFileFinder($parser, $progressOutput);
115-
$finder
116-
->exclude('vendor')
117-
->exclude('Tests')
118-
->exclude('Test');
119-
120-
return $finder;
121-
}
122-
123-
/**
124-
* @return ParserInterface
125-
*/
126-
public function getParser()
127-
{
128-
return $this->parser;
129-
}
130-
131-
/**
132-
* @return VerboseProgressOutput
133-
*/
134-
public function getOutput()
135-
{
136-
return $this->progressOutput;
70+
return new Result($parsedFiles, $parserErrors, $fileCount);
13771
}
13872
}

src/Finder/Result.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Finder;
4+
5+
use PhpParser\Error;
6+
use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo;
7+
8+
class Result
9+
{
10+
private $files;
11+
12+
private $errors;
13+
14+
private $fileCount;
15+
16+
/**
17+
* @param PhpFileInfo[] $files
18+
* @param Error[] $errors
19+
* @param int $fileCount
20+
*/
21+
public function __construct(array $files, array $errors, $fileCount)
22+
{
23+
$this->files = $files;
24+
$this->errors = $errors;
25+
$this->fileCount = $fileCount;
26+
}
27+
28+
public function parsedFiles()
29+
{
30+
return $this->files;
31+
}
32+
33+
public function parserErrors()
34+
{
35+
return $this->errors;
36+
}
37+
38+
public function fileCount()
39+
{
40+
return $this->fileCount;
41+
}
42+
}

src/Finder/UsageFinderFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace SensioLabs\DeprecationDetector\Finder;
4+
5+
use Symfony\Component\Finder\Finder;
6+
7+
class UsageFinderFactory implements FinderFactoryInterface
8+
{
9+
public function createFinder()
10+
{
11+
$finder = new Finder();
12+
$finder
13+
->name('*.php')
14+
->exclude('vendor')
15+
->exclude('Tests')
16+
->exclude('Test');
17+
18+
return $finder;
19+
}
20+
}

src/RuleSet/DirectoryTraverser.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
namespace SensioLabs\DeprecationDetector\RuleSet;
44

5-
use SensioLabs\DeprecationDetector\FileInfo\PhpFileInfo;
65
use SensioLabs\DeprecationDetector\Finder\ParsedPhpFileFinder;
76

8-
/**
9-
* Class Traverser.
10-
*
11-
* @author Christopher Hertel <[email protected]>
12-
*/
137
class DirectoryTraverser
148
{
159
/**
@@ -33,27 +27,18 @@ public function __construct(ParsedPhpFileFinder $finder)
3327
*/
3428
public function traverse($path, RuleSet $ruleSet = null)
3529
{
36-
$files = $this->finder->in($path);
30+
$result = $this->finder->parsePhpFiles($path);
3731

3832
if (!$ruleSet instanceof RuleSet) {
3933
$ruleSet = new RuleSet();
4034
}
4135

42-
foreach ($files as $i => $file) {
43-
/** @var PhpFileInfo $file */
36+
foreach ($result->parsedFiles() as $file) {
4437
if ($file->hasDeprecations()) {
4538
$ruleSet->merge($file);
4639
}
4740
}
4841

4942
return $ruleSet;
5043
}
51-
52-
public function reset()
53-
{
54-
$this->finder = ParsedPhpFileFinder::deprecationFinder(
55-
$this->finder->getParser(),
56-
$this->finder->getOutput()
57-
);
58-
}
5944
}

src/RuleSet/Loader/Composer/ComposerLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ private function loadPackageRuleSet(Package $package)
7373
if ($this->cache->has($key)) {
7474
$ruleSet = $this->cache->getCachedRuleSet($key);
7575
} elseif (is_dir($path = $package->getPackagePath(self::PACKAGE_PATH))) {
76-
$this->traverser->reset();
7776
$ruleSet = $this->traverser->traverse($path);
7877
$this->cache->cacheRuleSet($key, $ruleSet);
7978
} else {

0 commit comments

Comments
 (0)