Skip to content

Commit fe31268

Browse files
committed
add comment analyze as visitor
1 parent 512b578 commit fe31268

File tree

6 files changed

+76
-137
lines changed

6 files changed

+76
-137
lines changed

comments_density.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@
2424
'src',
2525
],
2626
docblockConfigDTO: new MissingDocblockConfigDTO(),
27-
disable: [
28-
'missingDocBlock',
29-
]
27+
disable: []
3028
);

src/AnalyzeComments/Analyzer/CommentFinder.php

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,58 @@
44

55
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer;
66

7-
use PhpToken;
7+
use PhpParser\NodeTraverser;
8+
use PhpParser\Parser;
9+
use PhpParser\ParserFactory;
810
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;
914
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
1015
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO;
1116
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
12-
13-
use function array_merge;
1417
use function in_array;
1518

16-
use const T_COMMENT;
17-
use const T_DOC_COMMENT;
18-
1919
final readonly class CommentFinder
2020
{
21+
private Parser $parser;
22+
2123
public function __construct(
2224
private CommentTypeFactory $commentFactory,
2325
private ConfigDTO $configDTO,
2426
private MissingDocBlockAnalyzer $missingDocBlockAnalyzer,
25-
) {}
27+
?Parser $parser = null,
28+
) {
29+
$this->parser = $parser ?? (new ParserFactory())->createForHostVersion();
30+
}
2631

2732
/**
2833
* @return CommentDTO[]
2934
*/
3035
public function run(string $content, string $filename): array
3136
{
32-
$tokens = PhpToken::tokenize($content);
37+
$traverser = new NodeTraverser();
3338

34-
$comments = $this->getCommentsFromFile($tokens, $filename);
39+
// $nameResolverVisitor = new NameResolver();
40+
// $traverser->addVisitor($nameResolverVisitor);
41+
42+
$missingDocBlockVisitor = new MissingDocBlockVisitor(
43+
$filename,
44+
new NodeNeedsDocblockChecker($this->configDTO->docblockConfigDTO),
45+
);
3546
if ($this->shouldAnalyzeMissingDocBlocks()) {
36-
$missingDocBlocks = $this->missingDocBlockAnalyzer->getMissingDocblocks($content, $filename);
37-
$comments = array_merge($missingDocBlocks, $comments);
47+
$traverser->addVisitor($missingDocBlockVisitor);
3848
}
3949

40-
return $comments;
50+
$commentVisitor = new CommentVisitor(
51+
$filename,
52+
$this->commentFactory,
53+
);
54+
$traverser->addVisitor($commentVisitor);
55+
56+
$traverser->traverse($this->parser->parse($content));
57+
58+
return [...$missingDocBlockVisitor->missingDocBlocks, ...$commentVisitor->comments];
4159
}
4260

4361
private function shouldAnalyzeMissingDocBlocks(): bool
@@ -50,31 +68,4 @@ private function shouldAnalyzeMissingDocBlocks(): bool
5068
true,
5169
);
5270
}
53-
54-
/**
55-
* @param PhpToken[] $tokens
56-
* @return CommentDTO[]
57-
*/
58-
private function getCommentsFromFile(array $tokens, string $filename): array
59-
{
60-
$comments = [];
61-
foreach ($tokens as $token) {
62-
if ($token->is([T_COMMENT, T_DOC_COMMENT])) {
63-
continue;
64-
}
65-
$commentType = $this->commentFactory->classifyComment($token->text);
66-
if ($commentType) {
67-
$comments[] =
68-
new CommentDTO(
69-
$commentType->getName(),
70-
$commentType->getColor(),
71-
$filename,
72-
$token->line,
73-
$token->text,
74-
);
75-
}
76-
}
77-
78-
return $comments;
79-
}
8071
}

src/AnalyzeComments/MissingDocblock/Visitors/Checkers/NodeNeedsDocblockChecker.php renamed to src/AnalyzeComments/Analyzer/Visitors/Checkers/NodeNeedsDocblockChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\Checkers;
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\Checkers;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Class_;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitorAbstract;
7+
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
8+
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
9+
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
10+
11+
final class CommentVisitor extends NodeVisitorAbstract
12+
{
13+
/** @var CommentDTO[] */
14+
public array $comments = [];
15+
16+
public function __construct(
17+
private readonly string $filename,
18+
private readonly CommentTypeFactory $commentFactory,
19+
) {}
20+
21+
public function enterNode(Node $node): null
22+
{
23+
$comments = array_filter([$node->getDocComment(), ...$node->getComments()]);
24+
foreach ($comments as $comment) {
25+
$commentType = $this->commentFactory->classifyComment($comment->getText());
26+
if ($commentType === null) {
27+
continue;
28+
}
29+
$this->comments[] =
30+
new CommentDTO(
31+
$commentType->getName(),
32+
$commentType->getColor(),
33+
$this->filename,
34+
$comment->getStartLine(),
35+
$comment->getText(),
36+
);
37+
}
38+
39+
return null;
40+
}
41+
}

src/AnalyzeComments/MissingDocblock/Visitors/MissingDocBlockVisitor.php renamed to src/AnalyzeComments/Analyzer/Visitors/MissingDocBlockVisitor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors;
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors;
66

77
use PhpParser\Node;
88
use PhpParser\NodeVisitorAbstract;
99
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
10+
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\Checkers\NodeNeedsDocblockChecker;
1011
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
11-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\Checkers\NodeNeedsDocblockChecker;
1212

1313
final class MissingDocBlockVisitor extends NodeVisitorAbstract
1414
{
@@ -32,7 +32,7 @@ public function enterNode(Node $node): null
3232

3333
$this->missingDocBlocks[] =
3434
new CommentDTO(
35-
MissingDocBlockAnalyzer::NAME, // todo
35+
MissingDocBlockAnalyzer::NAME,
3636
MissingDocBlockAnalyzer::COLOR,
3737
$this->filename,
3838
$node->getLine(),

src/AnalyzeComments/MissingDocblock/MissingDocBlockAnalyzer.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)