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 ( 9c8be8...090e97 )
by Michael
49s
created

DataStructureMethods::countSetter()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5
1
<?php
2
3
namespace MS\PHPMD\Rule\CleanCode;
4
5
use MS\PHPMD\Guesser\TestGuesser;
6
use PDepend\Source\AST\ASTMethod;
7
use PHPMD\AbstractNode;
8
use PHPMD\Node\ClassNode;
9
use PHPMD\Node\MethodNode;
10
11
/**
12
 * The data structure have to contain only simple getter and setter.
13
 */
14
class DataStructureMethods extends AbstractDataStructure
15
{
16
    use TestGuesser;
17
18
    /**
19
     * @var array
20
     */
21
    private $allowedPrefixes;
22
23
    /**
24
     * @var array
25
     */
26
    private $whitelist;
27
28
    /**
29
     * @param AbstractNode|ClassNode $node
30
     */
31 25
    public function apply(AbstractNode $node)
32
    {
33 25
        if (false === $this->isDataStructure($node) || 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...
34 19
            return;
35
        }
36
37 6
        $prefixes = $this->getStringProperty('prefixes');
38 6
        $this->allowedPrefixes = explode($this->getStringProperty('delimiter'), $prefixes);
39 6
        $this->whitelist = explode($this->getStringProperty('delimiter'), $this->getStringProperty('whitelist'));
40
41
        /** @var MethodNode $method */
42 6
        foreach ($node->getMethods() as $method) {
43 6
            if (true === $this->isMethodNameOnWhitelist($method)) {
44 5
                continue;
45
            }
46
47 6
            if (true === $this->hasCorrectPrefix($method) && true === $this->isSimpleMethod($method)) {
48 6
                continue;
49
            }
50
51 5
            $this->addViolation($method, [$prefixes]);
52 6
        }
53 6
    }
54
55
    /**
56
     * @param MethodNode $method
57
     *
58
     * @return bool
59
     */
60 6
    private function isMethodNameOnWhitelist(MethodNode $method)
61
    {
62 6
        return in_array($method->getImage(), $this->whitelist);
63
    }
64
65
    /**
66
     * @param MethodNode|ASTMethod $node
67
     *
68
     * @return bool
69
     */
70 6
    private function hasCorrectPrefix(MethodNode $node)
71
    {
72 6
        foreach ($this->allowedPrefixes as $prefix) {
73 6
            if ($prefix === substr($node->getImage(), 0, strlen($prefix))) {
74 6
                return true;
75
            }
76 6
        }
77
78 5
        return false;
79
    }
80
81
    /**
82
     * @param MethodNode|ASTMethod $node
83
     *
84
     * @return bool
85
     */
86 6
    private function isSimpleMethod(MethodNode $node)
87
    {
88 6
        $countScope = count($node->findChildrenOfType('ScopeStatement'));
89
90 6
        if (0 !== $countScope) {
91 5
            return false;
92
        }
93
94 6
        $countReturn = count($node->findChildrenOfType('ReturnStatement'));
95 6
        $countThis = $this->countThis($node);
96 6
        $countSetter = $this->countSetter($node);
97
98 6
        if (1 < $countReturn) {
99
            return false;
100
        }
101
102 6
        if (($countReturn + 1) < $countThis - $countSetter) {
103 5
            return false;
104
        }
105
106 6
        return true;
107
    }
108
109
    /**
110
     * @param MethodNode $node
111
     *
112
     * @return int
113
     */
114 6
    private function countThis(MethodNode $node)
115
    {
116 6
        $count = 0;
117 6
        $variables = $node->findChildrenOfType('Variable');
118
119 6
        foreach ($variables as $variable) {
120 6
            if ('$this' === $variable->getImage()) {
121 6
                $count++;
122 6
            }
123 6
        }
124
125 6
        return $count;
126
    }
127
128 6
    private function countSetter(MethodNode $node)
129
    {
130 6
        $count = 0;
131 6
        $methods = $node->findChildrenOfType('MethodPostfix');
132
133 6
        foreach ($methods as $method) {
134 5
            if ('set' === substr($method->getImage(), 0, 3)) {
135 4
                $child = $method->getFirstChildOfType('Variable');
136 4
                if($child && '$this' === $child->getImage()) {
137 4
                    $count++;
138 4
                }
139 4
            }
140 6
        }
141
142 6
        return $count;
143
    }
144
}
145