Passed
Pull Request — master (#59)
by Matthias
02:52
created

recordInterfaceDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 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
        $this->recordDefinedConstDefinition($node);
48 1
    }
49
50 2
    private function recordClassDefinition(Node $node)
51
    {
52 2
        if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
53 2
            $this->recordDefinitionOf($node);
54
        }
55 1
    }
56
57 1
    private function recordInterfaceDefinition(Node $node)
58
    {
59 1
        if ($node instanceof Node\Stmt\Interface_) {
60 1
            $this->recordDefinitionOf($node);
61
        }
62 1
    }
63
64 1
    private function recordTraitDefinition(Node $node)
65
    {
66 1
        if ($node instanceof Node\Stmt\Trait_) {
67
            $this->recordDefinitionOf($node);
68
        }
69 1
    }
70
71 1
    private function recordFunctionDefinition(Node $node)
72
    {
73 1
        if ($node instanceof Node\Stmt\Function_) {
74
            $this->recordDefinitionOf($node);
75
        }
76 1
    }
77
78 1
    private function recordConstDefinition(Node $node)
79
    {
80 1
        if ($node instanceof Node\Stmt\Const_) {
81
            foreach ($node->consts as $const) {
82
                $this->recordDefinitionOf($const);
83
            }
84
        }
85 1
    }
86
87 1
    private function recordDefinedConstDefinition(Node $node)
88
    {
89 1
        if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && $node->name->toString() === 'define') {
90
            $this->recordDefinitionOfStringSymbol((string)$node->args[0]->value->value);
91
        }
92 1
    }
93
94
    /**
95
     * @param Node $node
96
     *
97
     * @return void
98
     */
99 2
    private function recordDefinitionOf(Node $node)
100
    {
101 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...
102 1
            throw new \UnexpectedValueException(sprintf(
103
                'Given node of type "%s" (defined at line %s)does not have an assigned "namespacedName" property: '
104 1
                . 'did you pass it through a name resolver visitor?',
105 1
                get_class($node),
106 1
                $node->getLine()
107
            ));
108
        }
109
110 1
        $this->recordDefinitionOfStringSymbol((string)$node->namespacedName);
111 1
    }
112
113 1
    private function recordDefinitionOfStringSymbol(string $symbolName)
114
    {
115 1
        $this->definedSymbols[$symbolName] = $symbolName;
116 1
    }
117
}
118