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

ControllerMethodName   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 44
ccs 17
cts 18
cp 0.9444
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B apply() 0 20 5
A isController() 0 12 3
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