Passed
Push — master ( c3b5bc...853f24 )
by Tobias van
02:35 queued 12s
created

MissingImport::apply()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 8.9457
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
        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
            $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()));
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...
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