|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Rule\CleanCode; |
|
4
|
|
|
|
|
5
|
|
|
use PDepend\Source\AST\ASTClass; |
|
6
|
|
|
use PHPMD\AbstractNode; |
|
7
|
|
|
use PHPMD\AbstractRule; |
|
8
|
|
|
use PHPMD\Node\ClassNode; |
|
9
|
|
|
use PHPMD\Rule\ClassAware; |
|
10
|
|
|
use PDepend\Source\AST\AbstractASTArtifact; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class SuperfluousComment |
|
14
|
|
|
* |
|
15
|
|
|
* Don't write superfluous comments. It's adding by subtracting. |
|
16
|
|
|
* |
|
17
|
|
|
* @package MS\PHPMD\Rule\CleanCode |
|
18
|
|
|
*/ |
|
19
|
|
|
class SuperfluousComment extends AbstractRule implements ClassAware |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private $percent; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param AbstractNode $node |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function apply(AbstractNode $node) |
|
30
|
|
|
{ |
|
31
|
3 |
|
$this->percent = $this->getIntProperty('percent'); |
|
32
|
|
|
|
|
33
|
3 |
|
if (true === $this->getBooleanProperty('checkClass')) { |
|
34
|
3 |
|
$this->checkClass($node); |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
if (true === $this->getBooleanProperty('checkProperties')) { |
|
38
|
3 |
|
$this->checkProperties($node); |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
if (true === $this->getBooleanProperty('checkMethods')) { |
|
42
|
3 |
|
$this->checkMethods($node); |
|
43
|
3 |
|
} |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param AbstractNode $node |
|
48
|
|
|
*/ |
|
49
|
3 |
|
private function checkClass(AbstractNode $node) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$this->checkNode( |
|
52
|
3 |
|
$node, |
|
53
|
3 |
|
$node->getType(), |
|
54
|
3 |
|
$this->calculateNameToCommentSimilarityInPercent($node) |
|
55
|
3 |
|
); |
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param AbstractNode|ASTClass $node |
|
60
|
|
|
*/ |
|
61
|
3 |
|
private function checkProperties(AbstractNode $node) |
|
62
|
|
|
{ |
|
63
|
3 |
|
foreach ($node->getProperties() as $property) { |
|
64
|
1 |
|
$this->checkNode( |
|
65
|
1 |
|
$node, |
|
66
|
1 |
|
'property ' . $property->getName(), |
|
67
|
1 |
|
$this->calculateNameToCommentSimilarityInPercent($property) |
|
68
|
1 |
|
); |
|
69
|
3 |
|
} |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param AbstractNode|ClassNode $node |
|
74
|
|
|
*/ |
|
75
|
3 |
|
private function checkMethods(AbstractNode $node) |
|
76
|
|
|
{ |
|
77
|
3 |
|
foreach ($node->getMethods() as $method) { |
|
78
|
1 |
|
$this->checkNode( |
|
79
|
1 |
|
$method, |
|
80
|
1 |
|
$method->getType(), |
|
81
|
1 |
|
$this->calculateNameToCommentSimilarityInPercent($method) |
|
82
|
1 |
|
); |
|
83
|
3 |
|
} |
|
84
|
3 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param AbstractNode $node |
|
88
|
|
|
* @param string $type |
|
89
|
|
|
* @param int $percent |
|
90
|
|
|
*/ |
|
91
|
3 |
|
private function checkNode(AbstractNode $node, $type, $percent) |
|
92
|
|
|
{ |
|
93
|
3 |
|
if ($this->percent < $percent) { |
|
94
|
2 |
|
$this->addViolation($node, [$type, $percent]); |
|
95
|
2 |
|
} |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param AbstractNode|AbstractASTArtifact $node |
|
100
|
|
|
* |
|
101
|
|
|
* @return float |
|
102
|
|
|
*/ |
|
103
|
3 |
|
private function calculateNameToCommentSimilarityInPercent($node) |
|
104
|
|
|
{ |
|
105
|
3 |
|
$docComment = $node->getDocComment(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
3 |
|
if (empty($docComment)) { |
|
108
|
2 |
|
return 0; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
similar_text( |
|
112
|
2 |
|
$this->transformString($node->getName()), |
|
113
|
2 |
|
$this->getCommentDescription($docComment), |
|
114
|
|
|
$percent |
|
115
|
2 |
|
); |
|
116
|
|
|
|
|
117
|
2 |
|
return round($percent); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $docComment |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
2 |
|
private function getCommentDescription($docComment) |
|
126
|
|
|
{ |
|
127
|
2 |
|
$lines = explode(PHP_EOL, $docComment); |
|
128
|
|
|
|
|
129
|
2 |
|
$descriptionLines = []; |
|
130
|
|
|
|
|
131
|
2 |
|
foreach ($lines as $line) { |
|
132
|
2 |
|
if (false === strpos($line, '@')) { |
|
133
|
2 |
|
$descriptionLines[] = $line; |
|
134
|
2 |
|
} |
|
135
|
2 |
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
return $this->transformDescriptionLines($descriptionLines); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param array $descriptionLines |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
2 |
|
private function transformDescriptionLines(array $descriptionLines) |
|
146
|
|
|
{ |
|
147
|
2 |
|
$description = ''; |
|
148
|
|
|
|
|
149
|
2 |
|
foreach ($descriptionLines as $line) { |
|
150
|
2 |
|
$description .= $this->transformString($line); |
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
2 |
|
return $description; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $string |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
2 |
|
private function transformString($string) |
|
162
|
|
|
{ |
|
163
|
2 |
|
$string = strtolower($string); |
|
164
|
2 |
|
$string = preg_replace('/[^a-z0-9]/', '', $string); |
|
165
|
|
|
|
|
166
|
2 |
|
return $string; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: