Passed
Push — master ( 853f24...484d62 )
by Tobias van
02:10 queued 10s
created

MissingImport::isGlobalNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        $ignoreGlobal = $this->getBooleanProperty('ignore-global');
47 5
        foreach ($node->findChildrenOfType('AllocationExpression') as $allocationNode) {
48
            if (!$allocationNode) {
49
                continue;
50
            }
51 5
52
            $classNode = $allocationNode->getChild(0);
53 5
54 1
            if ($this->isSelfReference($classNode)) {
55
                continue;
56
            }
57 4
58 4
            if ($ignoreGlobal && $this->isGlobalNamespace($classNode)) {
59 4
                continue;
60 2
            }
61
62
            $classNameLength = $classNode->getEndColumn() - $classNode->getStartColumn() + 1;
0 ignored issues
show
Documentation Bug introduced by
The method getEndColumn 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...
Documentation Bug introduced by
The method getStartColumn 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...
63 7
            $className = $classNode->getImage();
64
            $fqcnLength = strlen($className);
65
66
            if ($classNameLength === $fqcnLength && substr($className, 0, 1) !== '$') {
67
                $this->addViolation($classNode, array($classNode->getBeginLine(), $classNode->getStartColumn()));
0 ignored issues
show
Documentation Bug introduced by
The method getStartColumn 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...
68
            }
69
        }
70
    }
71 5
72
    /**
73 5
     * Check whether a given class node is a self reference
74
     *
75
     * @param ASTNode $classNode A class node to check.
76
     * @return bool Whether the given class node is a self reference.
77
     */
78
    protected function isSelfReference(ASTNode $classNode)
79
    {
80
        return in_array($classNode->getImage(), $this->selfReferences, true);
81
    }
82
83
    /**
84
     * Check whether a given class node is in the global namespace
85
     *
86
     * @param ASTNode $classNode A class node to check.
87
     * @return bool Whether the given class node is in the global namespace.
88
     */
89
    protected function isGlobalNamespace(ASTNode $classNode)
90
    {
91
        return !strpos($classNode->getImage(), '\\', 1);
92
    }
93
}
94