Failed Conditions
Pull Request — master (#38)
by Matthias
02:46
created

DefinedSymbolCollector   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 95
ccs 38
cts 42
cp 0.9048
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinedSymbols() 0 3 1
A recordConstDefinition() 0 5 3
A __construct() 0 2 1
A recordInterfaceDefinition() 0 4 2
A enterNode() 0 7 1
A recordClassDefinition() 0 4 3
A beforeTraverse() 0 5 1
A recordFunctionDefinition() 0 4 2
A recordDefinitionOf() 0 12 2
A recordTraitDefinition() 0 4 2
1
<?php
2
3
namespace ComposerRequireChecker\NodeVisitor;
4
5
use PhpParser\Node;
6
use PhpParser\NodeVisitorAbstract;
7
8
final class DefinedSymbolCollector extends NodeVisitorAbstract
9
{
10
    /**
11
     * @var mixed[]
12
     */
13
    private $definedSymbols = [];
14
15 2
    public function __construct()
16
    {
17 2
    }
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 1
    public function beforeTraverse(array $nodes)
23
    {
24 1
        $this->definedSymbols = [];
25
26 1
        return parent::beforeTraverse($nodes);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::beforeTraverse($nodes) targeting PhpParser\NodeVisitorAbstract::beforeTraverse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
27
    }
28
29
    /**
30
     * @return string[]
31
     */
32 1
    public function getDefinedSymbols(): array
33
    {
34 1
        return array_keys($this->definedSymbols);
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 2
    public function enterNode(Node $node)
41
    {
42 2
        $this->recordClassDefinition($node);
43 1
        $this->recordInterfaceDefinition($node);
44 1
        $this->recordTraitDefinition($node);
45 1
        $this->recordFunctionDefinition($node);
46 1
        $this->recordConstDefinition($node);
47 1
    }
48
49 2
    private function recordClassDefinition(Node $node)
50
    {
51 2
        if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
52 2
            $this->recordDefinitionOf($node);
53
        }
54 1
    }
55
56 1
    private function recordInterfaceDefinition(Node $node)
57
    {
58 1
        if ($node instanceof Node\Stmt\Interface_) {
59 1
            $this->recordDefinitionOf($node);
60
        }
61 1
    }
62
63 1
    private function recordTraitDefinition(Node $node)
64
    {
65 1
        if ($node instanceof Node\Stmt\Trait_) {
66
            $this->recordDefinitionOf($node);
67
        }
68 1
    }
69
70 1
    private function recordFunctionDefinition(Node $node)
71
    {
72 1
        if ($node instanceof Node\Stmt\Function_) {
73
            $this->recordDefinitionOf($node);
74
        }
75 1
    }
76
77 1
    private function recordConstDefinition(Node $node)
78
    {
79 1
        if ($node instanceof Node\Stmt\Const_) {
80
            foreach ($node->consts as $const) {
81
                $this->recordDefinitionOf($const);
82
            }
83
        }
84 1
    }
85
86
    /**
87
     * @param Node $node
88
     *
89
     * @return void
90
     */
91 2
    private function recordDefinitionOf(Node $node)
92
    {
93 2
        if (!isset($node->namespacedName)) {
0 ignored issues
show
Bug introduced by
Accessing namespacedName on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
94 1
            throw new \UnexpectedValueException(sprintf(
95
                'Given node of type "%s" (defined at line %s)does not have an assigned "namespacedName" property: '
96 1
                . 'did you pass it through a name resolver visitor?',
97 1
                get_class($node),
98 1
                $node->getLine()
99
            ));
100
        }
101
102 1
        $this->definedSymbols[(string)$node->namespacedName] = $node->namespacedName;
103 1
    }
104
}
105