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.

TryStatement::hasNotAllowedChildren()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
namespace MS\PHPMD\Rule\CleanCode;
4
5
use PHPMD\AbstractNode;
6
use PHPMD\AbstractRule;
7
use PHPMD\Node\MethodNode;
8
use PHPMD\Rule\MethodAware;
9
10
/**
11
 * Methods should only have one try statement. If not, swap out the try statement in an extra method. It increase the readability.
12
 */
13
class TryStatement extends AbstractRule implements MethodAware
14
{
15
    /**
16
     * @param AbstractNode|MethodNode $node
17
     */
18 23
    public function apply(AbstractNode $node)
19
    {
20 23
        $countTry = count($node->findChildrenOfType('TryStatement'));
21
22 23
        if (1 < $countTry) {
23 1
            $this->addViolation($node);
24 1
        }
25
26 23
        if (1 === $countTry &&  true === $this->hasNotAllowedChildren($node)) {
0 ignored issues
show
Compatibility introduced by
$node of type object<PHPMD\AbstractNode> is not a sub-type of object<PHPMD\Node\MethodNode>. 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 1
            $this->addViolation($node);
28 1
        }
29 23
    }
30
31
    /**
32
     * @param MethodNode $node
33
     *
34
     * @return bool
35
     */
36 1
    private function hasNotAllowedChildren(MethodNode $node)
37
    {
38 1
        $children = $node->findChildrenOfType('ScopeStatement');
39
40 1
        $allowedChildren = explode(
41 1
            $this->getStringProperty('delimiter'),
42 1
            $this->getStringProperty('allowedChildren')
43 1
        );
44
45
        /** @var AbstractNode $child */
46 1
        foreach ($children as $child) {
47 1
            if (true === in_array($child->getImage(), $allowedChildren) || true === $this->isChildOfTry($child)) {
48 1
                continue;
49
            }
50
51 1
            return true;
52 1
        }
53
54 1
        return false;
55
    }
56
57
    /**
58
     * @param AbstractNode $node
59
     *
60
     * @return bool
61
     */
62 1
    private function isChildOfTry(AbstractNode $node)
63
    {
64 1
        $parent = $node->getParent();
65
66 1
        while (is_object($parent)) {
67 1
            if ($parent->isInstanceOf('TryStatement')) {
68 1
                return true;
69
            }
70
71 1
            $parent = $parent->getParent();
72 1
        }
73
74 1
        return false;
75
    }
76
}
77