1 | <?php |
||
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) |
|
61 | |||
62 | /** |
||
63 | * @param MethodNode|ASTMethod $node |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | 5 | private function hasCorrectPrefix(MethodNode $node) |
|
77 | |||
78 | /** |
||
79 | * @param MethodNode|ASTMethod $node |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | 5 | private function isSimpleMethod(MethodNode $node) |
|
105 | |||
106 | /** |
||
107 | * @param MethodNode $node |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | 5 | private function countThis(MethodNode $node) |
|
124 | |||
125 | 5 | private function countSetter(MethodNode $node) |
|
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.