BooleanArgumentFlag   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
ccs 0
cts 15
cp 0
wmc 7
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 13 3
A isBooleanValue() 0 4 4
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\CleanCode;
19
20
use PDepend\Source\AST\ASTValue;
21
use PHPMD\AbstractNode;
22
use PHPMD\AbstractRule;
23
use PHPMD\Rule\FunctionAware;
24
use PHPMD\Rule\MethodAware;
25
26
/**
27
 * Check for a boolean flag in the method/function signature.
28
 *
29
 * Boolean flags are signs for single responsibility principle violations.
30
 */
31
class BooleanArgumentFlag extends AbstractRule implements MethodAware, FunctionAware
32
{
33
    /**
34
     * This method checks if a method/function has boolean flag arguments and warns about them.
35
     *
36
     * @param \PHPMD\AbstractNode $node
37
     * @return void
38
     */
39
    public function apply(AbstractNode $node)
40
    {
41
        foreach ($node->findChildrenOfType('FormalParameter') as $param) {
42
            $declarator = $param->getFirstChildOfType('VariableDeclarator');
43
            $value = $declarator->getValue();
0 ignored issues
show
Documentation Bug introduced by
The method getValue does not exist on object<PHPMD\Node\ASTNode>? 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...
44
45
            if (false === $this->isBooleanValue($value)) {
46
                continue;
47
            }
48
49
            $this->addViolation($param, array($node->getImage(), $declarator->getImage()));
50
        }
51
    }
52
53
    private function isBooleanValue(ASTValue $value = null)
54
    {
55
        return $value && $value->isValueAvailable() && ($value->getValue() === true || $value->getValue() === false);
56
    }
57
}
58