1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SavinMikhail\CommentsDensity\AnalyzeComments\File; |
||
6 | |||
7 | use PhpParser\NodeTraverser; |
||
8 | use PhpParser\Parser; |
||
9 | use PhpParser\ParserFactory; |
||
10 | use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO; |
||
11 | use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\Checkers\NodeNeedsDocblockChecker; |
||
12 | use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\CommentVisitor; |
||
13 | use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\MissingDocBlockVisitor; |
||
14 | use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory; |
||
15 | use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\MissingDocBlock; |
||
16 | use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config; |
||
17 | |||
18 | use function in_array; |
||
19 | |||
20 | final readonly class CommentFinder |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
21 | { |
||
22 | private Parser $parser; |
||
23 | |||
24 | 31 | public function __construct( |
|
25 | private CommentTypeFactory $commentFactory, |
||
26 | private Config $configDTO, |
||
27 | ?Parser $parser = null, |
||
28 | ) { |
||
29 | 31 | $this->parser = $parser ?? (new ParserFactory())->createForHostVersion(); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * @return CommentDTO[] |
||
34 | */ |
||
35 | 31 | public function __invoke(string $content, string $filename): array |
|
36 | { |
||
37 | 31 | $traverser = new NodeTraverser(); |
|
38 | |||
39 | 31 | $missingDocBlockVisitor = new MissingDocBlockVisitor( |
|
40 | 31 | $filename, |
|
41 | 31 | new NodeNeedsDocblockChecker($this->configDTO->docblockConfigDTO), |
|
42 | 31 | ); |
|
43 | 31 | if (in_array(MissingDocBlock::NAME, $this->configDTO->getAllowedTypes(), true)) { |
|
44 | 31 | $traverser->addVisitor($missingDocBlockVisitor); |
|
45 | } |
||
46 | |||
47 | 31 | $commentVisitor = new CommentVisitor( |
|
48 | 31 | $filename, |
|
49 | 31 | $this->commentFactory, |
|
50 | 31 | ); |
|
51 | 31 | $traverser->addVisitor($commentVisitor); |
|
52 | |||
53 | 31 | $traverser->traverse($this->parser->parse($content)); |
|
54 | |||
55 | 31 | return [...$missingDocBlockVisitor->missingDocBlocks, ...$commentVisitor->comments]; |
|
56 | } |
||
57 | } |
||
58 |