Test Failed
Pull Request — master (#59)
by Matthias
02:28
created

recordDefinedConstDefinition()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 2
nop 1
crap 12
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
    }
49 2
50
    private function recordClassDefinition(Node $node)
51 2
    {
52 2
        if ($node instanceof Node\Stmt\Class_ && !$node->isAnonymous()) {
53
            $this->recordDefinitionOf($node);
54 1
        }
55
    }
56 1
57
    private function recordInterfaceDefinition(Node $node)
58 1
    {
59 1
        if ($node instanceof Node\Stmt\Interface_) {
60
            $this->recordDefinitionOf($node);
61 1
        }
62
    }
63 1
64
    private function recordTraitDefinition(Node $node)
65 1
    {
66
        if ($node instanceof Node\Stmt\Trait_) {
67
            $this->recordDefinitionOf($node);
68 1
        }
69
    }
70 1
71
    private function recordFunctionDefinition(Node $node)
72 1
    {
73
        if ($node instanceof Node\Stmt\Function_) {
74
            $this->recordDefinitionOf($node);
75 1
        }
76
    }
77 1
78
    private function recordConstDefinition(Node $node)
79 1
    {
80
        if ($node instanceof Node\Stmt\Const_) {
81
            foreach ($node->consts as $const) {
82
                $this->recordDefinitionOf($const);
83
            }
84 1
        }
85
    }
86
87
    private function recordDefinedConstDefinition(Node $node)
88
    {
89
        if ($node instanceof Node\Expr\FuncCall && $node->name->toString() === 'define') {
0 ignored issues
show
Bug introduced by
The method toString() does not exist on PhpParser\Node\Expr. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        if ($node instanceof Node\Expr\FuncCall && $node->name->/** @scrutinizer ignore-call */ toString() === 'define') {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            $this->recordDefinitionOfStringSymbol((string)$node->args[0]->value->value);
91 2
        }
92
    }
93 2
94 1
    /**
95
     * @param Node $node
96 1
     *
97 1
     * @return void
98 1
     */
99
    private function recordDefinitionOf(Node $node)
100
    {
101
        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 1
                'Given node of type "%s" (defined at line %s)does not have an assigned "namespacedName" property: '
104
                . 'did you pass it through a name resolver visitor?',
105
                get_class($node),
106
                $node->getLine()
107
            ));
108
        }
109
110
        $this->recordDefinitionOfStringSymbol((string)$node->namespacedName);
111
    }
112
113
    private function recordDefinitionOfStringSymbol(string $symbolName)
114
    {
115
        $this->definedSymbols[$symbolName] = $symbolName;
116
    }
117
}
118