|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Rule\Test; |
|
4
|
|
|
|
|
5
|
|
|
use PHPMD\Node\ClassNode; |
|
6
|
|
|
use PHPMD\Node\MethodNode; |
|
7
|
|
|
use PHPMD\AbstractNode; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractNumberOf extends AbstractTest |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $names; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private $match; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param AbstractNode|ClassNode $node |
|
25
|
|
|
*/ |
|
26
|
3 |
|
public function apply(AbstractNode $node) |
|
27
|
|
|
{ |
|
28
|
3 |
|
if (false === $this->isTest($node)) { |
|
|
|
|
|
|
29
|
1 |
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
$this->names = explode($this->getStringProperty('delimiter'), $this->getStringProperty('names')); |
|
33
|
2 |
|
$this->match = $this->getBooleanProperty('match'); |
|
34
|
2 |
|
$number = $this->getIntProperty('number'); |
|
35
|
|
|
|
|
36
|
2 |
|
foreach ($node->getMethods() as $method) { |
|
37
|
2 |
|
$count = $this->count($method); |
|
38
|
|
|
|
|
39
|
2 |
|
if ($number < $count) { |
|
40
|
2 |
|
$this->addViolation($method, [$count, $number]); |
|
41
|
2 |
|
} |
|
42
|
2 |
|
} |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param MethodNode $node |
|
47
|
|
|
* |
|
48
|
|
|
* @return int |
|
49
|
|
|
*/ |
|
50
|
2 |
|
private function count(MethodNode $node) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$count = 0; |
|
53
|
2 |
|
$methodPostfixes = $node->findChildrenOfType('MethodPostfix'); |
|
54
|
|
|
|
|
55
|
2 |
|
foreach ($methodPostfixes as $methodPostfix) { |
|
56
|
2 |
|
if (true === $this->isMethodPostfixInNames($methodPostfix)) { |
|
57
|
2 |
|
$count++; |
|
58
|
2 |
|
} |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
return $count; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param AbstractNode $methodPostfix |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
2 |
|
private function isMethodPostfixInNames(AbstractNode $methodPostfix) |
|
70
|
|
|
{ |
|
71
|
2 |
|
if (true === $this->match) { |
|
72
|
2 |
|
return in_array($methodPostfix->getImage(), $this->names); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
return $this->isMethodPostfixContainsNames($methodPostfix); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param AbstractNode $methodPostfix |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
2 |
|
private function isMethodPostfixContainsNames(AbstractNode $methodPostfix) |
|
84
|
|
|
{ |
|
85
|
2 |
|
foreach ($this->names as $name) { |
|
86
|
2 |
|
if (false !== strpos($methodPostfix->getImage(), $name)) { |
|
87
|
2 |
|
return true; |
|
88
|
|
|
} |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
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.