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