|
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
|
6 |
|
public function apply(AbstractNode $node) |
|
29
|
|
|
{ |
|
30
|
6 |
|
if (false === $this->isDataStructure($node)) { |
|
|
|
|
|
|
31
|
3 |
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
$prefixes = $this->getStringProperty('prefixes'); |
|
35
|
3 |
|
$this->allowedPrefixes = explode($this->getStringProperty('delimiter'), $prefixes); |
|
36
|
3 |
|
$this->whitelist = explode($this->getStringProperty('delimiter'), $this->getStringProperty('whitelist')); |
|
37
|
|
|
|
|
38
|
|
|
/** @var MethodNode $method */ |
|
39
|
3 |
|
foreach ($node->getMethods() as $method) { |
|
40
|
3 |
|
if (true === $this->isMethodNameOnWhitelist($method)) { |
|
41
|
3 |
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
if (true === $this->hasCorrectPrefix($method) && true === $this->isSimpleMethod($method)) { |
|
45
|
3 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
$this->addViolation($method, [$prefixes]); |
|
49
|
3 |
|
} |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param MethodNode $method |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
3 |
|
private function isMethodNameOnWhitelist(MethodNode $method) |
|
58
|
|
|
{ |
|
59
|
3 |
|
return in_array($method->getImage(), $this->whitelist); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param MethodNode|ASTMethod $node |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
3 |
|
private function hasCorrectPrefix(MethodNode $node) |
|
68
|
|
|
{ |
|
69
|
3 |
|
foreach ($this->allowedPrefixes as $prefix) { |
|
70
|
3 |
|
if ($prefix === substr($node->getImage(), 0, strlen($prefix))) { |
|
71
|
3 |
|
return true; |
|
72
|
|
|
} |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param MethodNode|ASTMethod $node |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
3 |
|
private function isSimpleMethod(MethodNode $node) |
|
84
|
|
|
{ |
|
85
|
3 |
|
$countScope = count($node->findChildrenOfType('ScopeStatement')); |
|
86
|
|
|
|
|
87
|
3 |
|
if (0 !== $countScope) { |
|
88
|
3 |
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
$countReturn = count($node->findChildrenOfType('ReturnStatement')); |
|
92
|
3 |
|
$countThis = $this->countThis($node); |
|
93
|
|
|
|
|
94
|
3 |
|
if (1 < $countReturn) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
if (($countReturn + 1) < $countThis) { |
|
99
|
3 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param MethodNode $node |
|
107
|
|
|
* |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
3 |
|
private function countThis(MethodNode $node) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$count = 0; |
|
113
|
3 |
|
$variables = $node->findChildrenOfType('Variable'); |
|
114
|
|
|
|
|
115
|
3 |
|
foreach ($variables as $variable) { |
|
116
|
3 |
|
if ($variable->getImage() === '$this') { |
|
117
|
3 |
|
$count++; |
|
118
|
3 |
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return $count; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.