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 PHPMD\AbstractNode; |
21
|
|
|
use PHPMD\AbstractRule; |
22
|
|
|
use PHPMD\Node\ASTNode; |
23
|
|
|
use PHPMD\Rule\FunctionAware; |
24
|
|
|
use PHPMD\Rule\MethodAware; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Checks that all classes are imported |
28
|
|
|
* |
29
|
|
|
* This rule can be used to prevent use of fully qualified class names. |
30
|
|
|
*/ |
31
|
|
|
class MissingImport extends AbstractRule implements MethodAware, FunctionAware |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var array Self reference class names. |
35
|
|
|
*/ |
36
|
|
|
protected $selfReferences = array('self', 'static'); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Checks for missing class imports and warns about it |
40
|
|
|
* |
41
|
|
|
* @param AbstractNode $node The node to check upon. |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
7 |
|
public function apply(AbstractNode $node) |
45
|
|
|
{ |
46
|
7 |
|
foreach ($node->findChildrenOfType('AllocationExpression') as $allocationNode) { |
47
|
5 |
|
if (!$allocationNode) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
$classNode = $allocationNode->getChild(0); |
52
|
|
|
|
53
|
5 |
|
if ($this->isSelfReference($classNode)) { |
54
|
1 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
$classNameLength = $classNode->getEndColumn() - $classNode->getStartColumn() + 1; |
|
|
|
|
58
|
4 |
|
$className = $classNode->getImage(); |
59
|
4 |
|
$fqcnLength = strlen($className); |
60
|
2 |
|
|
61
|
|
|
if ($classNameLength === $fqcnLength && substr($className, 0, 1) !== '$') { |
62
|
|
|
$this->addViolation($classNode, array($classNode->getBeginLine(), $classNode->getStartColumn())); |
|
|
|
|
63
|
7 |
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check whether a given class node is a self reference |
69
|
|
|
* |
70
|
|
|
* @param ASTNode $classNode A class node to check. |
71
|
5 |
|
* @return bool Whether the given class node is a self reference. |
72
|
|
|
*/ |
73
|
5 |
|
protected function isSelfReference(ASTNode $classNode) |
74
|
|
|
{ |
75
|
|
|
return in_array($classNode->getImage(), $this->selfReferences, true); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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: