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(); |
|
|
|
|
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
|
|
|
|
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: