Completed
Pull Request — master (#11)
by Cedric
06:33
created

ControllerMethodName::isController()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 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 (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