GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( adbc2a...6af8e5 )
by Michael
02:42
created

PrivateFieldDeclaration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 98
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B apply() 0 24 6
A getPublicMethods() 0 13 3
A calculateVariablePercentOfMethods() 0 12 2
A getMethodsContainsVariable() 0 21 4
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
    public function apply(AbstractNode $node)
20
    {
21
        if (true === $this->isDataStructure($node)) {
0 ignored issues
show
Compatibility introduced by
$node of type object<PHPMD\AbstractNode> is not a sub-type of object<PHPMD\Node\ClassNode>. It seems like you assume a child class of the class PHPMD\AbstractNode to be always present.

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.

Loading history...
22
            return;
23
        }
24
25
        $minPercent = $this->getIntProperty('percent');
26
        $publicMethods = $this->getPublicMethods($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PHPMD\AbstractNode> is not a sub-type of object<PHPMD\Node\ClassNode>. It seems like you assume a child class of the class PHPMD\AbstractNode to be always present.

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.

Loading history...
27
28
        foreach ($node->findChildrenOfType('FieldDeclaration') as $fieldDeclaration) {
29
            if (false === $fieldDeclaration->isPrivate() || true === $fieldDeclaration->isStatic()) {
30
                continue;
31
            }
32
33
            $variableDeclarator = $fieldDeclaration->getFirstChildOfType('VariableDeclarator');
34
35
            $variableName = substr($variableDeclarator->getName(), 1);
36
            $methodPercent = $this->calculateVariablePercentOfMethods($publicMethods, $variableName);
37
38
            if ($minPercent >= $methodPercent) {
39
                $this->addViolation($fieldDeclaration, [$methodPercent, $minPercent]);
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param ClassNode $node
46
     *
47
     * @return array
48
     */
49
    private function getPublicMethods(ClassNode $node)
50
    {
51
        $methods = $node->getMethods();
52
        $publicMethods = [];
53
54
        foreach ($methods as $method) {
55
            if (true === $method->isPublic()) {
56
                $publicMethods[] = $method;
57
            }
58
        }
59
60
        return $publicMethods;
61
    }
62
63
    /**
64
     * @param array  $methods
65
     * @param string $variableName
66
     *
67
     * @return float
68
     */
69
    private function calculateVariablePercentOfMethods(array $methods, $variableName)
70
    {
71
        $methodCount = count($methods);
72
73
        if (0 === $methodCount) {
74
            return 0;
75
        }
76
77
        $methodsWithVariable = $this->getMethodsContainsVariable($methods, $variableName);
78
79
        return round(count($methodsWithVariable) / $methodCount * 100);
80
    }
81
82
    /**
83
     * @param AbstractNode[] $methods
84
     * @param string         $variableName
85
     *
86
     * @return AbstractNode[]
87
     */
88
    private function getMethodsContainsVariable(array $methods, $variableName)
89
    {
90
        $methodsWithVariable = [];
91
92
        foreach ($methods as $method) {
93
            $propertyPostfixes = $method->findChildrenOfType('PropertyPostfix');
94
95
            /** @var AbstractNode $propertyPostfix */
96
            foreach ($propertyPostfixes as $propertyPostfix) {
97
                $identifier = $propertyPostfix->getFirstChildOfType('Identifier');
98
99
                if ($variableName === $identifier->getName()) {
100
                    $methodsWithVariable[] = $method;
101
102
                    break;
103
                }
104
            }
105
        }
106
107
        return $methodsWithVariable;
108
    }
109
}
110