|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\MissingDocblock; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\Node\Expr\Throw_; |
|
9
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
10
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
|
11
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
12
|
|
|
use PhpParser\Node\Stmt\Enum_; |
|
13
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
14
|
|
|
use PhpParser\Node\Stmt\Interface_; |
|
15
|
|
|
use PhpParser\Node\Stmt\Property; |
|
16
|
|
|
use PhpParser\Node\Stmt\Trait_; |
|
17
|
|
|
use PhpParser\NodeTraverser; |
|
18
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
19
|
|
|
use SavinMikhail\CommentsDensity\DTO\Input\MissingDocblockConfigDTO; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
final class MissingDocBlockVisitor extends NodeVisitorAbstract |
|
22
|
|
|
{ |
|
23
|
|
|
public array $missingDocBlocks = []; |
|
24
|
|
|
|
|
25
|
26 |
|
public function __construct( |
|
26
|
|
|
private readonly string $filename, |
|
27
|
|
|
private readonly MissingDocblockConfigDTO $config |
|
28
|
|
|
) { |
|
29
|
26 |
|
} |
|
30
|
|
|
|
|
31
|
26 |
|
public function enterNode(Node $node): void |
|
32
|
|
|
{ |
|
33
|
26 |
|
if (! $this->requiresDocBlock($node)) { |
|
34
|
26 |
|
return; |
|
35
|
|
|
} |
|
36
|
16 |
|
$docComment = $node->getDocComment(); |
|
37
|
16 |
|
if ($docComment !== null) { |
|
38
|
9 |
|
return; |
|
39
|
|
|
} |
|
40
|
14 |
|
$this->missingDocBlocks[] = [ |
|
41
|
14 |
|
'type' => 'missingDocblock', |
|
42
|
14 |
|
'content' => '', |
|
43
|
14 |
|
'file' => $this->filename, |
|
44
|
14 |
|
'line' => $node->getLine(), |
|
|
|
|
|
|
45
|
14 |
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
26 |
|
private function requiresDocBlock(Node $node): bool |
|
49
|
|
|
{ |
|
50
|
26 |
|
if ($node instanceof Class_ && $this->config->class) { |
|
51
|
11 |
|
return !$node->isAnonymous(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
26 |
|
if ($node instanceof Trait_ && $this->config->trait) { |
|
55
|
1 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
26 |
|
if ($node instanceof Interface_ && $this->config->interface) { |
|
59
|
1 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
26 |
|
if ($node instanceof Enum_ && $this->config->enum) { |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
26 |
|
if ($node instanceof Function_ && $this->config->function) { |
|
67
|
1 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
26 |
|
if ($node instanceof ClassMethod && $this->config->function) { |
|
71
|
6 |
|
if ($this->config->requireDocblocksForAllMethods) { |
|
72
|
3 |
|
return true; |
|
73
|
|
|
} |
|
74
|
3 |
|
return $this->methodRequiresAdditionalDocBlock($node); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
26 |
|
if ($node instanceof Property && $this->config->property) { |
|
78
|
5 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
26 |
|
if ($node instanceof ClassConst && $this->config->constant) { |
|
82
|
1 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
26 |
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* here we want to find methods that has uncaught throws statements or their return type will be better |
|
90
|
|
|
* described as generic |
|
91
|
|
|
*/ |
|
92
|
3 |
|
private function methodRequiresAdditionalDocBlock(Node $node): bool |
|
93
|
|
|
{ |
|
94
|
3 |
|
$returnType = $node->getReturnType(); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
3 |
|
if ($returnType instanceof Node\Identifier && $returnType->toString() === 'array') { |
|
97
|
2 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
if ($this->methodThrowsExceptions($node)) { |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
private function methodThrowsExceptions(Node $node): bool |
|
108
|
|
|
{ |
|
109
|
1 |
|
$traverser = new NodeTraverser(); |
|
110
|
1 |
|
$visitor = new class extends NodeVisitorAbstract { |
|
111
|
|
|
public bool $throwsExceptions = false; |
|
112
|
|
|
|
|
113
|
|
|
public function enterNode(Node $node): void |
|
114
|
|
|
{ |
|
115
|
1 |
|
if ($node instanceof Throw_) { |
|
116
|
|
|
$this->throwsExceptions = true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
1 |
|
}; |
|
120
|
1 |
|
$traverser->addVisitor($visitor); |
|
121
|
1 |
|
$traverser->traverse([$node]); |
|
122
|
|
|
|
|
123
|
1 |
|
return $visitor->throwsExceptions; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
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