|
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
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function beforeTraverse(array $nodes) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->definedSymbols = []; |
|
25
|
|
|
|
|
26
|
|
|
return parent::beforeTraverse($nodes); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getDefinedSymbols() : array |
|
33
|
|
|
{ |
|
34
|
|
|
return array_keys($this->definedSymbols); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function enterNode(Node $node) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->recordClassDefinition($node); |
|
43
|
|
|
$this->recordInterfaceDefinition($node); |
|
44
|
|
|
$this->recordTraitDefinition($node); |
|
45
|
|
|
$this->recordFunctionDefinition($node); |
|
46
|
|
|
$this->recordConstDefinition($node); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function recordClassDefinition(Node $node) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($node instanceof Node\Stmt\Class_) { |
|
52
|
|
|
$this->recordDefinitionOf($node); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function recordInterfaceDefinition(Node $node) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($node instanceof Node\Stmt\Interface_) { |
|
59
|
|
|
$this->recordDefinitionOf($node); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function recordTraitDefinition(Node $node) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($node instanceof Node\Stmt\Trait_) { |
|
66
|
|
|
$this->recordDefinitionOf($node); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function recordFunctionDefinition(Node $node) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($node instanceof Node\Stmt\Function_) { |
|
73
|
|
|
$this->recordDefinitionOf($node); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function recordConstDefinition(Node $node) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($node instanceof Node\Stmt\Const_) { |
|
80
|
|
|
foreach ($node->consts as $const) { |
|
81
|
|
|
$this->recordDefinitionOf($const); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Node $node |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
private function recordDefinitionOf(Node $node) |
|
92
|
|
|
{ |
|
93
|
|
|
if (! isset($node->namespacedName)) { |
|
|
|
|
|
|
94
|
|
|
throw new \UnexpectedValueException(sprintf( |
|
95
|
|
|
'Given node of type "%s" (defined at line %s)does not have an assigned "namespacedName" property: ' |
|
96
|
|
|
. 'did you pass it through a name resolver visitor?', |
|
97
|
|
|
get_class($node), |
|
98
|
|
|
$node->getLine() |
|
99
|
|
|
)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->definedSymbols[(string) $node->namespacedName] = $node->namespacedName; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: