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.

hasMethodsChainAllowedPrefixes()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
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 25
    public function apply(AbstractNode $node)
23
    {
24 25
        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 22
        $maxChainCount = $this->getIntProperty('maxChainCount');
29 22
        $allowedPrefixes = explode($this->getStringProperty('delimiter'), $this->getStringProperty('allowedPrefixes'));
30
31 22
        foreach ($node->getMethods() as $method) {
32 19
            foreach ($method->findChildrenOfType('MemberPrimaryPrefix') as $memberPrimaryPrefix) {
33 13
                if (false === $this->hasMethodsChainAllowedPrefixes($memberPrimaryPrefix, $allowedPrefixes) && true === $this->isMethodsChainExcessively($memberPrimaryPrefix, $maxChainCount)) {
34 2
                    $this->addViolation($memberPrimaryPrefix, [$maxChainCount]);
35 2
                }
36 19
            }
37 22
        }
38 22
    }
39
40
    /**
41
     * @param AbstractNode $memberPrimaryPrefix
42
     * @param array        $allowedPrefixes
43
     *
44
     * @return bool
45
     */
46 13
    private function hasMethodsChainAllowedPrefixes(AbstractNode $memberPrimaryPrefix, array $allowedPrefixes)
47
    {
48 13
        $methodPostfixes = $memberPrimaryPrefix->findChildrenOfType('MethodPostfix');
49
50 13
        foreach ($methodPostfixes as $methodPostfix) {
51 10
            foreach ($allowedPrefixes as $allowedPrefix) {
52 10
                if ($allowedPrefix === substr($methodPostfix->getName(), 0, strlen($allowedPrefix))) {
53 7
                    continue 2;
54
                }
55 10
            }
56
57 10
            return false;
58 11
        }
59
60 11
        return true;
61
    }
62
63
    /**
64
     * @param AbstractNode $memberPrimaryPrefix
65
     * @param int          $chainCount
66
     *
67
     * @return bool
68
     */
69 22
    private function isMethodsChainExcessively(AbstractNode $memberPrimaryPrefix, $chainCount)
70
    {
71 10
        for ($chain = 0; $chain < $chainCount; $chain++) {
72 10
            $children = $memberPrimaryPrefix->getChildren();
73
74 10
            if (false === isset($children[1]) || !$children[1] instanceof ASTMemberPrimaryPrefix) {
75 10
                return false;
76
            }
77
78 8
            $memberPrimaryPrefix = $children[1];
79 8
        }
80
81 22
        return true;
82 1
    }
83
}
84