Completed
Push — master ( f99704...527926 )
by Michael
8s
created

ControllerMethodName::apply()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
crap 5.0144
1
<?php
2
3
namespace MS\PHPMD\Rule\Symfony2;
4
5
use PDepend\Source\AST\ASTClass;
6
use PHPMD\AbstractNode;
7
use PHPMD\AbstractRule;
8
use PHPMD\Node\ClassNode;
9
use PHPMD\Node\MethodNode;
10
use PHPMD\Rule\ClassAware;
11
12
/**
13
 * Class ControllerMethodName
14
 *
15
 * When the class is concrete and ends with Controller, the method names have to end with Action.
16
 *
17
 * @package PHPMD\Rule\Symfony2
18
 */
19
class ControllerMethodName extends AbstractRule implements ClassAware
20
{
21
    /**
22
     * @param AbstractNode|ClassNode $node
23
     */
24 3
    public function apply(AbstractNode $node)
25
    {
26 3
        if (false === $this->isController($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...
27 2
            return;
28
        }
29
30 1
        $allowedMethodNames = explode($this->getStringProperty('delimiter'), $this->getStringProperty('allowedMethodNames'));
31
32
        /** @var MethodNode $method */
33 1
        foreach ($node->getMethods() as $method) {
34
            // Check if method is whitelisted
35 1
            if (true === in_array($method->getImage(), $allowedMethodNames)) {
36
              continue;
37
            }
38
39 1
            if ('Action' !== substr($method->getImage(), -6, 6)) {
40 1
                $this->addViolation($method);
41 1
            }
42 1
        }
43 1
    }
44
45
    /**
46
     * @param ClassNode|ASTClass $node
47
     *
48
     * @return bool
49
     */
50 3
    private function isController(ClassNode $node)
51
    {
52 3
        if (true === $node->isAbstract()) {
53 1
            return false;
54
        }
55
56 2
        if ('Controller' === substr($node->getImage(), -10, 10)) {
57 1
            return true;
58
        }
59
60 1
        return false;
61
    }
62
}
63