MethodNameUnderstandable::apply()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 5
Bugs 1 Features 3
Metric Value
c 5
b 1
f 3
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.6737
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
3
namespace MS\PHPMD\Rule\Test;
4
5
use PHPMD\AbstractNode;
6
use PHPMD\Node\ClassNode;
7
8
/**
9
 * Class MethodNameUnderstandable
10
 *
11
 * The method name in your test should describe what they will check. This works only with a few more words.
12
 *
13
 * @package MS\PHPMD\Rule\Test
14
 */
15
class MethodNameUnderstandable extends AbstractTestRule
16
{
17
    /**
18
     * @param AbstractNode|ClassNode $node
19
     */
20 4
    public function apply(AbstractNode $node)
21
    {
22 4
        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...
23 1
            return;
24
        }
25
26 3
        $regex = $this->getStringProperty('regex');
27 3
        $number = $this->getIntProperty('number');
28
29 3
        foreach ($node->getMethods() as $method) {
30 3
            if ('test' !== substr($method->getImage(), 0, 4)) {
31 1
                continue;
32
            }
33
34 2
            $wordsInclusiveTest = count(preg_split($regex, $method->getImage()));
35 2
            $words = $wordsInclusiveTest - 1;
36
37 2
            if ($number > $words) {
38 1
                $this->addViolation($method, [$words, $number]);
39 1
            }
40 3
        }
41 3
    }
42
}
43