|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Rule\CleanCode; |
|
4
|
|
|
|
|
5
|
|
|
use MS\PHPMD\Guesser\TestGuesser; |
|
6
|
|
|
use PDepend\Source\AST\ASTMemberPrimaryPrefix; |
|
7
|
|
|
use PHPMD\AbstractNode; |
|
8
|
|
|
use PHPMD\AbstractRule; |
|
9
|
|
|
use PHPMD\Node\ClassNode; |
|
10
|
|
|
use PHPMD\Rule\ClassAware; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Don't chain methods excessively. The code becomes hard to test and violate the law of demeter. |
|
14
|
|
|
*/ |
|
15
|
|
|
class MemberPrimaryPrefix extends AbstractRule implements ClassAware |
|
16
|
|
|
{ |
|
17
|
|
|
use TestGuesser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param AbstractNode|ClassNode $node |
|
21
|
|
|
*/ |
|
22
|
20 |
|
public function apply(AbstractNode $node) |
|
23
|
|
|
{ |
|
24
|
20 |
|
if (true === $this->isTest($node)) { |
|
|
|
|
|
|
25
|
3 |
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
17 |
|
$maxChainCount = $this->getIntProperty('maxChainCount'); |
|
29
|
17 |
|
$allowedPrefixes = explode($this->getStringProperty('delimiter'), $this->getStringProperty('allowedPrefixes')); |
|
30
|
|
|
|
|
31
|
17 |
|
foreach ($node->getMethods() as $method) { |
|
32
|
15 |
|
foreach ($method->findChildrenOfType('MemberPrimaryPrefix') as $memberPrimaryPrefix) { |
|
33
|
10 |
|
if (false === $this->hasMethodsChainAllowedPrefixes($memberPrimaryPrefix, $allowedPrefixes) && true === $this->isMethodsChainExcessively($memberPrimaryPrefix, $maxChainCount)) { |
|
34
|
3 |
|
$this->addViolation($memberPrimaryPrefix, [$maxChainCount]); |
|
35
|
3 |
|
} |
|
36
|
15 |
|
} |
|
37
|
17 |
|
} |
|
38
|
17 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param AbstractNode $memberPrimaryPrefix |
|
42
|
|
|
* @param array $allowedPrefixes |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
10 |
|
private function hasMethodsChainAllowedPrefixes(AbstractNode $memberPrimaryPrefix, array $allowedPrefixes) |
|
47
|
|
|
{ |
|
48
|
10 |
|
$methodPostfixes = $memberPrimaryPrefix->findChildrenOfType('MethodPostfix'); |
|
49
|
|
|
|
|
50
|
10 |
|
foreach ($methodPostfixes as $methodPostfix) { |
|
51
|
10 |
|
foreach ($allowedPrefixes as $allowedPrefix) { |
|
52
|
10 |
|
if ($allowedPrefix === substr($methodPostfix->getName(), 0, strlen($allowedPrefix))) { |
|
53
|
8 |
|
continue 2; |
|
54
|
|
|
} |
|
55
|
10 |
|
} |
|
56
|
|
|
|
|
57
|
10 |
|
return false; |
|
58
|
8 |
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param AbstractNode $memberPrimaryPrefix |
|
65
|
|
|
* @param int $chainCount |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
17 |
|
private function isMethodsChainExcessively(AbstractNode $memberPrimaryPrefix, $chainCount) |
|
70
|
|
|
{ |
|
71
|
10 |
|
for ($chain = 0; $chain < $chainCount; $chain++) { |
|
72
|
10 |
|
$children = $memberPrimaryPrefix->getChildren(); |
|
73
|
|
|
|
|
74
|
17 |
|
if (false === isset($children[1]) || !$children[1] instanceof ASTMemberPrimaryPrefix) { |
|
75
|
10 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
8 |
|
$memberPrimaryPrefix = $children[1]; |
|
79
|
8 |
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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.