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.
Passed
Push — master ( bd34e5...b66988 )
by Michael
05:06
created

getMethodsContainsVariable()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 2
crap 5
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)) {
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 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