isMethodNameOnWhitelist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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