Passed
Push — master ( 902a34...c826a7 )
by Kyle
53s queued 11s
created

php/PHPMD/Rule/Naming/BooleanGetMethodName.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Rule\Naming;
19
20
use PHPMD\AbstractNode;
21
use PHPMD\AbstractRule;
22
use PHPMD\Node\MethodNode;
23
use PHPMD\Rule\MethodAware;
24
25
/**
26
 * This rule tests that a method which returns a boolean value does not start
27
 * with <b>get</b> or <b>_get</b> for a getter.
28
 */
29
class BooleanGetMethodName extends AbstractRule implements MethodAware
30
{
31
    /**
32
     * Extracts all variable and variable declarator nodes from the given node
33
     * and checks the variable name length against the configured minimum
34
     * length.
35
     *
36
     * @param \PHPMD\AbstractNode $node
37
     * @return void
38
     */
39 8
    public function apply(AbstractNode $node)
40
    {
41
        /** @var $node MethodNode */
42 8
        if ($this->isBooleanGetMethod($node)) {
43 4
            $this->addViolation($node, array($node->getImage()));
44
        }
45 8
    }
46
47
    /**
48
     * Tests if the given method matches all criteria to be an invalid
49
     * boolean get method.
50
     *
51
     * @param \PHPMD\Node\MethodNode $node
52
     * @return boolean
53
     */
54 8
    protected function isBooleanGetMethod(MethodNode $node)
55
    {
56 8
        return $this->isGetterMethodName($node)
57 8
            && $this->isReturnTypeBoolean($node)
58 8
            && $this->isParameterizedOrIgnored($node);
59
    }
60
61
    /**
62
     * Tests if the given method starts with <b>get</b> or <b>_get</b>.
63
     *
64
     * @param \PHPMD\Node\MethodNode $node
65
     * @return boolean
66
     */
67 8
    protected function isGetterMethodName(MethodNode $node)
68
    {
69 8
        return (preg_match('(^_?get)i', $node->getImage()) > 0);
70
    }
71
72
    /**
73
     * Tests if the given method is declared with return type boolean.
74
     *
75
     * @param \PHPMD\Node\MethodNode $node
76
     * @return boolean
77
     */
78 6
    protected function isReturnTypeBoolean(MethodNode $node)
79
    {
80 6
        $comment = $node->getDocComment();
0 ignored issues
show
Documentation Bug introduced by
The method getDocComment does not exist on object<PHPMD\Node\MethodNode>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81 6
82
        return (preg_match('(\*\s*@return\s+bool(ean)?\s)i', $comment) > 0);
83
    }
84
85
    /**
86
     * Tests if the property <b>$checkParameterizedMethods</b> is set to <b>true</b>
87
     * or has no parameters.
88
     *
89
     * @param \PHPMD\Node\MethodNode $node
90
     * @return boolean
91 5
     */
92
    protected function isParameterizedOrIgnored(MethodNode $node)
93 5
    {
94 1
        if ($this->getBooleanProperty('checkParameterizedMethods')) {
95
            return $node->getParameterCount() === 0;
96 4
        }
97
98
        return true;
99
    }
100
}
101