AbstractMethodNumberOf   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 12
c 4
b 1
f 2
lcom 1
cbo 3
dl 0
loc 83
ccs 32
cts 32
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 18 4
A count() 0 13 3
A isMethodPostfixInNames() 0 8 2
A isMethodPostfixContainsNames() 0 10 3
1
<?php
2
3
namespace MS\PHPMD\Rule\Test;
4
5
use PHPMD\Node\ClassNode;
6
use PHPMD\Node\MethodNode;
7
use PHPMD\AbstractNode;
8
9
/**
10
 * Class AbstractMethodNumberOf
11
 *
12
 * @package MS\PHPMD\Rule\Test
13
 */
14
abstract class AbstractMethodNumberOf extends AbstractTestRule
15
{
16
    /**
17
     * @var array
18
     */
19
    private $names;
20
21
    /**
22
     * @var bool
23
     */
24
    private $match;
25
26
    /**
27
     * @param AbstractNode|ClassNode $node
28
     */
29 5
    public function apply(AbstractNode $node)
30
    {
31 5
        if (false === $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...
32 2
            return;
33
        }
34
35 3
        $this->names = explode($this->getStringProperty('delimiter'), $this->getStringProperty('names'));
36 3
        $this->match = $this->getBooleanProperty('match');
37 3
        $number = $this->getIntProperty('number');
38
39 3
        foreach ($node->getMethods() as $method) {
40 3
            $count = $this->count($method);
41
42 3
            if ($number < $count) {
43 2
                $this->addViolation($method, [$count, $number]);
44 2
            }
45 3
        }
46 3
    }
47
48
    /**
49
     * @param MethodNode $node
50
     *
51
     * @return int
52
     */
53 3
    private function count(MethodNode $node)
54
    {
55 3
        $count = 0;
56 3
        $methodPostfixes = $node->findChildrenOfType('MethodPostfix');
57
58 3
        foreach ($methodPostfixes as $methodPostfix) {
59 3
            if (true === $this->isMethodPostfixInNames($methodPostfix)) {
60 3
                $count++;
61 3
            }
62 3
        }
63
64 3
        return $count;
65
    }
66
67
    /**
68
     * @param AbstractNode $methodPostfix
69
     *
70
     * @return bool
71
     */
72 3
    private function isMethodPostfixInNames(AbstractNode $methodPostfix)
73
    {
74 3
        if (true === $this->match) {
75 1
            return in_array($methodPostfix->getImage(), $this->names);
76
        }
77
78 2
        return $this->isMethodPostfixContainsNames($methodPostfix);
79
    }
80
81
    /**
82
     * @param AbstractNode $methodPostfix
83
     *
84
     * @return bool
85
     */
86 2
    private function isMethodPostfixContainsNames(AbstractNode $methodPostfix)
87
    {
88 2
        foreach ($this->names as $name) {
89 2
            if (false !== strpos($methodPostfix->getImage(), $name)) {
90 2
                return true;
91
            }
92 1
        }
93
94 1
        return false;
95
    }
96
}
97