|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\NodeTraverser; |
|
8
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
|
9
|
|
|
use PhpParser\ParserFactory; |
|
10
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO; |
|
|
|
|
|
|
11
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\MissingDocblockConfigDTO; |
|
|
|
|
|
|
12
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\Checkers\NodeNeedsDocblockChecker; |
|
|
|
|
|
|
13
|
|
|
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\MissingDocBlockVisitor; |
|
14
|
|
|
|
|
15
|
|
|
final class MissingDocBlockAnalyzer |
|
16
|
|
|
{ |
|
17
|
|
|
public const NAME = 'missingDocblock'; |
|
18
|
|
|
public const COLOR = 'red'; |
|
19
|
|
|
|
|
20
|
|
|
private bool $exceedThreshold = false; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
private readonly MissingDocblockConfigDTO $docblockConfigDTO, |
|
24
|
|
|
) {} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Analyzes the AST of a file for missing docblocks. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $code the code to analyze |
|
30
|
|
|
* @param string $filename the filename of the code |
|
31
|
|
|
* |
|
32
|
|
|
* @return CommentDTO[] the analysis results |
|
33
|
|
|
*/ |
|
34
|
|
|
public function analyze(string $code, string $filename): array |
|
35
|
|
|
{ |
|
36
|
|
|
$parser = (new ParserFactory())->createForHostVersion(); |
|
37
|
|
|
$ast = $parser->parse($code); |
|
38
|
|
|
$traverser = new NodeTraverser(); |
|
39
|
|
|
|
|
40
|
|
|
$nameResolver = new NameResolver(); |
|
41
|
|
|
$traverser->addVisitor($nameResolver); |
|
42
|
|
|
$fqnNodes = $traverser->traverse($ast); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$visitor = new MissingDocBlockVisitor( |
|
45
|
|
|
$filename, |
|
46
|
|
|
new NodeNeedsDocblockChecker($this->docblockConfigDTO), |
|
47
|
|
|
); |
|
48
|
|
|
$traverser->removeVisitor($nameResolver); |
|
49
|
|
|
$traverser->addVisitor($visitor); |
|
50
|
|
|
$traverser->traverse($fqnNodes); |
|
51
|
|
|
|
|
52
|
|
|
return $visitor->missingDocBlocks; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return CommentDTO[] |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getMissingDocblocks(string $code, string $filename): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->analyze($code, $filename); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getColor(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return self::COLOR; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array<string, float> $thresholds |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getStatColor(float $count, array $thresholds): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (!isset($thresholds['missingDocBlock'])) { |
|
74
|
|
|
return 'white'; |
|
75
|
|
|
} |
|
76
|
|
|
if ($count <= $thresholds['missingDocBlock']) { |
|
77
|
|
|
return 'green'; |
|
78
|
|
|
} |
|
79
|
|
|
$this->exceedThreshold = true; |
|
80
|
|
|
|
|
81
|
|
|
return 'red'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function hasExceededThreshold(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->exceedThreshold; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getName(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return self::NAME; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths