1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Rule\CleanCode; |
4
|
|
|
|
5
|
|
|
use MS\PHPMD\Guesser\TestGuesser; |
6
|
|
|
use PDepend\Source\AST\ASTMethod; |
7
|
|
|
use PHPMD\AbstractNode; |
8
|
|
|
use PHPMD\Node\ClassNode; |
9
|
|
|
use PHPMD\Node\MethodNode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The data structure have to contain only simple getter and setter. |
13
|
|
|
*/ |
14
|
|
|
class DataStructureMethods extends AbstractDataStructure |
15
|
|
|
{ |
16
|
|
|
use TestGuesser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $allowedPrefixes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $whitelist; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param AbstractNode|ClassNode $node |
30
|
|
|
*/ |
31
|
25 |
|
public function apply(AbstractNode $node) |
32
|
|
|
{ |
33
|
25 |
|
if (false === $this->isDataStructure($node) || true === $this->isTest($node)) { |
|
|
|
|
34
|
19 |
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
$prefixes = $this->getStringProperty('prefixes'); |
38
|
6 |
|
$this->allowedPrefixes = explode($this->getStringProperty('delimiter'), $prefixes); |
39
|
6 |
|
$this->whitelist = explode($this->getStringProperty('delimiter'), $this->getStringProperty('whitelist')); |
40
|
|
|
|
41
|
|
|
/** @var MethodNode $method */ |
42
|
6 |
|
foreach ($node->getMethods() as $method) { |
43
|
6 |
|
if (true === $this->isMethodNameOnWhitelist($method)) { |
44
|
5 |
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
if (true === $this->hasCorrectPrefix($method) && true === $this->isSimpleMethod($method)) { |
48
|
6 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
$this->addViolation($method, [$prefixes]); |
52
|
6 |
|
} |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param MethodNode $method |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
6 |
|
private function isMethodNameOnWhitelist(MethodNode $method) |
61
|
|
|
{ |
62
|
6 |
|
return in_array($method->getImage(), $this->whitelist); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param MethodNode|ASTMethod $node |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
6 |
|
private function hasCorrectPrefix(MethodNode $node) |
71
|
|
|
{ |
72
|
6 |
|
foreach ($this->allowedPrefixes as $prefix) { |
73
|
6 |
|
if ($prefix === substr($node->getImage(), 0, strlen($prefix))) { |
74
|
6 |
|
return true; |
75
|
|
|
} |
76
|
6 |
|
} |
77
|
|
|
|
78
|
5 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param MethodNode|ASTMethod $node |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
6 |
|
private function isSimpleMethod(MethodNode $node) |
87
|
|
|
{ |
88
|
6 |
|
$countScope = count($node->findChildrenOfType('ScopeStatement')); |
89
|
|
|
|
90
|
6 |
|
if (0 !== $countScope) { |
91
|
5 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
$countReturn = count($node->findChildrenOfType('ReturnStatement')); |
95
|
6 |
|
$countThis = $this->countThis($node); |
96
|
6 |
|
$countSetter = $this->countSetter($node); |
97
|
|
|
|
98
|
6 |
|
if (1 < $countReturn) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
if (($countReturn + 1) < $countThis - $countSetter) { |
103
|
5 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param MethodNode $node |
111
|
|
|
* |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
6 |
|
private function countThis(MethodNode $node) |
115
|
|
|
{ |
116
|
6 |
|
$count = 0; |
117
|
6 |
|
$variables = $node->findChildrenOfType('Variable'); |
118
|
|
|
|
119
|
6 |
|
foreach ($variables as $variable) { |
120
|
6 |
|
if ('$this' === $variable->getImage()) { |
121
|
6 |
|
$count++; |
122
|
6 |
|
} |
123
|
6 |
|
} |
124
|
|
|
|
125
|
6 |
|
return $count; |
126
|
|
|
} |
127
|
|
|
|
128
|
6 |
|
private function countSetter(MethodNode $node) |
129
|
|
|
{ |
130
|
6 |
|
$count = 0; |
131
|
6 |
|
$methods = $node->findChildrenOfType('MethodPostfix'); |
132
|
|
|
|
133
|
6 |
|
foreach ($methods as $method) { |
134
|
5 |
|
if ('set' === substr($method->getImage(), 0, 3)) { |
135
|
4 |
|
$child = $method->getFirstChildOfType('Variable'); |
136
|
4 |
|
if($child && '$this' === $child->getImage()) { |
137
|
4 |
|
$count++; |
138
|
4 |
|
} |
139
|
4 |
|
} |
140
|
6 |
|
} |
141
|
|
|
|
142
|
6 |
|
return $count; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.