|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\PhpParser\NodeVisitor\Collection; |
|
16
|
|
|
|
|
17
|
|
|
use ArrayIterator; |
|
18
|
|
|
use Humbug\PhpScoper\PhpParser\Node\NamedIdentifier; |
|
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender; |
|
20
|
|
|
use IteratorAggregate; |
|
21
|
|
|
use PhpParser\Node; |
|
22
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
|
23
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
24
|
|
|
use PhpParser\Node\Name; |
|
25
|
|
|
use PhpParser\Node\Stmt\ClassLike; |
|
26
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
27
|
|
|
use PhpParser\Node\Stmt\Use_; |
|
28
|
|
|
use PhpParser\Node\Stmt\UseUse; |
|
29
|
|
|
use function count; |
|
30
|
|
|
use function Humbug\PhpScoper\clone_node; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Utility class collecting all the use statements for the scoped files allowing to easily find the use which a node |
|
34
|
|
|
* may use. |
|
35
|
|
|
* |
|
36
|
|
|
* @private |
|
37
|
|
|
*/ |
|
38
|
|
|
final class UseStmtCollection implements IteratorAggregate |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Use_[][] |
|
42
|
240 |
|
*/ |
|
43
|
|
|
private $nodes = [ |
|
44
|
240 |
|
null => [], |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
public function add(?Name $namespaceName, Use_ $node): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->nodes[(string) $namespaceName][] = clone_node($node); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Finds the statements matching the given name. |
|
54
|
|
|
* |
|
55
|
|
|
* $name = 'Foo'; |
|
56
|
|
|
* |
|
57
|
|
|
* use X; |
|
58
|
|
|
* use Bar\Foo; |
|
59
|
|
|
* use Y; |
|
60
|
|
|
* |
|
61
|
|
|
* will return the use statement for `Bar\Foo`. |
|
62
|
|
|
* |
|
63
|
224 |
|
* @param Name|null $namespaceName |
|
64
|
|
|
* @param Name $node |
|
65
|
224 |
|
* |
|
66
|
|
|
* @return null|Name |
|
67
|
224 |
|
*/ |
|
68
|
|
|
public function findStatementForNode(?Name $namespaceName, Name $node): ?Name |
|
69
|
224 |
|
{ |
|
70
|
|
|
$name = strtolower($node->getFirst()); |
|
71
|
224 |
|
|
|
72
|
121 |
|
$parentNode = ParentNodeAppender::findParent($node); |
|
73
|
121 |
|
|
|
74
|
121 |
|
if ($parentNode instanceof ClassLike |
|
75
|
120 |
|
&& $node instanceof NamedIdentifier |
|
76
|
6 |
|
&& $node->getOriginalNode() === $parentNode->name |
|
77
|
5 |
|
) { |
|
78
|
|
|
// The current node can either be the class like name or one of its elements, e.g. extends or implements. |
|
79
|
|
|
// In the first case, the node was original an Identifier. |
|
80
|
1 |
|
|
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
114 |
|
|
|
84
|
6 |
|
$useStatements = $this->nodes[(string) $namespaceName] ?? []; |
|
85
|
5 |
|
|
|
86
|
|
|
foreach ($useStatements as $use_) { |
|
87
|
|
|
foreach ($use_->uses as $useStatement) { |
|
88
|
1 |
|
if (false === ($useStatement instanceof UseUse)) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
108 |
|
if ($name === $useStatement->getAlias()->toLowerString()) { |
|
93
|
17 |
|
if ($this->isFunctionName($node, $parentNode)) { |
|
94
|
19 |
|
if (Use_::TYPE_FUNCTION === $use_->type) { |
|
95
|
|
|
return $useStatement->name; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
140 |
|
|
|
101
|
|
|
if ($parentNode instanceof ConstFetch && 1 === count($node->parts)) { |
|
102
|
|
|
if (Use_::TYPE_CONSTANT === $use_->type) { |
|
103
|
|
|
return $useStatement->name; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Match the alias |
|
110
|
|
|
return $useStatement->name; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (null !== $useStatement->alias) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getIterator(): iterable |
|
126
|
|
|
{ |
|
127
|
|
|
return new ArrayIterator($this->nodes); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function isFunctionName(Name $node, ?Node $parentNode): bool |
|
131
|
|
|
{ |
|
132
|
|
|
if (null === $parentNode || 1 !== count($node->parts)) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($parentNode instanceof FuncCall) { |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (false === ($parentNode instanceof Function_)) { |
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
/* @var Function_ $parentNode */ |
|
144
|
|
|
|
|
145
|
|
|
return $node instanceof NamedIdentifier && $node->getOriginalNode() === $parentNode->name; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|