Passed
Push — master ( d68e51...3a82ea )
by Kyle
02:50
created

MissingImport   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 20 5
A isSelfReference() 0 4 1
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 = ['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;
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...
58 4
            $fqcnLength = strlen($classNode->getImage());
59 4
            if ($classNameLength === $fqcnLength) {
60 2
                $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...
61
            }
62
        }
63 7
    }
64
65
    /**
66
     * Check whether a given class node is a self reference
67
     *
68
     * @param ASTNode $classNode A class node to check.
69
     * @return bool Whether the given class node is a self reference.
70
     */
71 5
    protected function isSelfReference(ASTNode $classNode)
72
    {
73 5
        return in_array($classNode->getImage(), $this->selfReferences, true);
74
    }
75
}
76