|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity; |
|
6
|
|
|
|
|
7
|
|
|
final class MissingDocBlockAnalyzer |
|
8
|
|
|
{ |
|
9
|
|
|
private bool $exceedThreshold = true; |
|
10
|
|
|
/** |
|
11
|
|
|
* Analyzes the tokens of a file for docblocks. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $tokens The tokens to analyze. |
|
14
|
|
|
* @return array The analysis results. |
|
15
|
|
|
*/ |
|
16
|
|
|
private function analyzeTokens(array $tokens, string $filename): array |
|
17
|
|
|
{ |
|
18
|
|
|
$lastDocBlock = null; |
|
19
|
|
|
$missingDocBlocks = []; |
|
20
|
|
|
$tokenCount = count($tokens); |
|
21
|
|
|
|
|
22
|
|
|
for ($i = 0; $i < $tokenCount; $i++) { |
|
23
|
|
|
$token = $tokens[$i]; |
|
24
|
|
|
|
|
25
|
|
|
if (!is_array($token)) { |
|
26
|
|
|
continue; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($token[0] === T_DOC_COMMENT) { |
|
30
|
|
|
$lastDocBlock = $token[1]; |
|
31
|
|
|
} elseif (in_array($token[0], [T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM, T_FUNCTION], true)) { |
|
32
|
|
|
$this->getNextNonWhitespaceToken($tokens, ++$i); |
|
33
|
|
|
if (empty($lastDocBlock)) { |
|
34
|
|
|
$missingDocBlocks[] = [ |
|
35
|
|
|
'type' => 'missingDocblock', |
|
36
|
|
|
'content' => '',//"$name missing docblock", |
|
37
|
|
|
'file' => $filename, |
|
38
|
|
|
'line' => $token[2] |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
$lastDocBlock = null; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $missingDocBlocks; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets the next non-whitespace token. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $tokens The tokens to analyze. |
|
52
|
|
|
* @param int $currentIndex The current index in the tokens array. |
|
53
|
|
|
* @return string The next non-whitespace token. |
|
54
|
|
|
*/ |
|
55
|
|
|
private function getNextNonWhitespaceToken(array $tokens, int $currentIndex): string |
|
56
|
|
|
{ |
|
57
|
|
|
$count = count($tokens); |
|
58
|
|
|
for ($i = $currentIndex; $i < $count; $i++) { |
|
59
|
|
|
if (! is_array($tokens[$i])) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
if ($tokens[$i][0] !== T_WHITESPACE) { |
|
63
|
|
|
return $tokens[$i][1]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getMissingDocblocks(array $tokens, string $filename): array |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->analyzeTokens($tokens, $filename); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getColor(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return 'red'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getStatColor(float $count, array $thresholds): string |
|
80
|
|
|
{ |
|
81
|
|
|
if (! isset($thresholds['missingDocBlock'])) { |
|
82
|
|
|
return 'white'; |
|
83
|
|
|
} |
|
84
|
|
|
if ($count <= $thresholds['missingDocBlock']) { |
|
85
|
|
|
return 'green'; |
|
86
|
|
|
} |
|
87
|
|
|
$this->exceedThreshold = true; |
|
88
|
|
|
return 'red'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function hasExceededThreshold(): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->exceedThreshold; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getName(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return 'missingDocblock'; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|