|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Rule\CleanCode; |
|
4
|
|
|
|
|
5
|
|
|
use MS\PHPMD\Guesser\DataStructureGuesser; |
|
6
|
|
|
use PHPMD\AbstractNode; |
|
7
|
|
|
use PHPMD\Node\ClassNode; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* To have a high cohesion the private variables of a class should be used in many methods. Otherwise the class probably does more than one thing. |
|
11
|
|
|
*/ |
|
12
|
|
|
class PrivateFieldDeclaration extends AbstractDataStructure |
|
13
|
|
|
{ |
|
14
|
|
|
use DataStructureGuesser; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param AbstractNode|ClassNode $node |
|
18
|
|
|
*/ |
|
19
|
25 |
|
public function apply(AbstractNode $node) |
|
20
|
|
|
{ |
|
21
|
25 |
|
if (true === $this->isDataStructure($node)) { |
|
|
|
|
|
|
22
|
8 |
|
return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
17 |
|
$minPercent = $this->getIntProperty('percent'); |
|
26
|
17 |
|
$methods = $node->getMethods(); |
|
27
|
|
|
|
|
28
|
17 |
|
foreach ($node->findChildrenOfType('FieldDeclaration') as $fieldDeclaration) { |
|
29
|
4 |
|
if (false === $fieldDeclaration->isPrivate() || true === $fieldDeclaration->isStatic()) { |
|
30
|
3 |
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
$variableDeclarator = $fieldDeclaration->getFirstChildOfType('VariableDeclarator'); |
|
34
|
|
|
|
|
35
|
4 |
|
$variableName = substr($variableDeclarator->getName(), 1); |
|
36
|
4 |
|
$methodPercent = $this->calculateVariablePercentOfMethods($methods, $variableName); |
|
37
|
|
|
|
|
38
|
4 |
|
if ($minPercent >= $methodPercent) { |
|
39
|
4 |
|
$this->addViolation($fieldDeclaration, [$methodPercent, $minPercent]); |
|
40
|
4 |
|
} |
|
41
|
17 |
|
} |
|
42
|
17 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param array $methods |
|
46
|
|
|
* @param string $variableName |
|
47
|
|
|
* |
|
48
|
|
|
* @return float |
|
49
|
|
|
*/ |
|
50
|
4 |
|
private function calculateVariablePercentOfMethods(array $methods, $variableName) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$methodCount = count($methods); |
|
53
|
|
|
|
|
54
|
4 |
|
if (0 === $methodCount) { |
|
55
|
2 |
|
return 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
$methodsWithVariable = $this->getMethodsContainsVariable($methods, $variableName); |
|
59
|
|
|
|
|
60
|
2 |
|
return round(count($methodsWithVariable) / $methodCount * 100); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param AbstractNode[] $methods |
|
65
|
|
|
* @param string $variableName |
|
66
|
|
|
* |
|
67
|
|
|
* @return AbstractNode[] |
|
68
|
|
|
*/ |
|
69
|
17 |
|
private function getMethodsContainsVariable(array $methods, $variableName) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$methodsWithVariable = []; |
|
72
|
|
|
|
|
73
|
2 |
|
foreach ($methods as $method) { |
|
74
|
17 |
|
$propertyPostfixes = $method->findChildrenOfType('PropertyPostfix'); |
|
75
|
|
|
|
|
76
|
|
|
/** @var AbstractNode $propertyPostfix */ |
|
77
|
2 |
|
foreach ($propertyPostfixes as $propertyPostfix) { |
|
78
|
2 |
|
$identifier = $propertyPostfix->getFirstChildOfType('Identifier'); |
|
79
|
|
|
|
|
80
|
2 |
|
if (null === $identifier) { |
|
81
|
1 |
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
if ($variableName === $identifier->getName()) { |
|
85
|
1 |
|
$methodsWithVariable[] = $method; |
|
86
|
|
|
|
|
87
|
1 |
|
break; |
|
88
|
|
|
} |
|
89
|
2 |
|
} |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
return $methodsWithVariable; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.