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