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 ( 6b72d9...7fec6d )
by Michael
05:51
created

getMemberPrimaryPrefixWithChainCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3
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)) {
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...
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