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 Countable; |
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender; |
20
|
|
|
use IteratorAggregate; |
21
|
|
|
use PhpParser\Node; |
22
|
|
|
use PhpParser\Node\Name; |
23
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
24
|
|
|
use function count; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Utility class collecting all the namespaces for the scoped files allowing to easily find the namespace to which |
28
|
|
|
* belongs a node. |
29
|
|
|
* |
30
|
|
|
* @private |
31
|
|
|
*/ |
32
|
|
|
final class NamespaceStmtCollection implements IteratorAggregate, Countable |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Namespace_[] |
36
|
|
|
*/ |
37
|
|
|
private $nodes = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var (Name|null)[] Associative array with the potentially prefixed namespace names as keys and their original name |
41
|
|
|
* as value. |
42
|
|
|
*/ |
43
|
|
|
private $mapping = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Namespace_ $node New namespace, may have been prefixed. |
47
|
|
|
* @param Namespace_ $originalName Original unchanged namespace. |
48
|
|
|
*/ |
49
|
545 |
|
public function add(Namespace_ $node, Namespace_ $originalName): void |
50
|
|
|
{ |
51
|
545 |
|
$this->nodes[] = $originalName; |
52
|
|
|
|
53
|
545 |
|
$this->mapping[(string) $node->name] = $originalName->name; |
54
|
|
|
} |
55
|
|
|
|
56
|
427 |
|
public function findNamespaceForNode(Node $node): ?Name |
57
|
|
|
{ |
58
|
427 |
|
if (0 === count($this->nodes)) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Shortcut if there is only one namespace |
63
|
427 |
|
if (1 === count($this->nodes)) { |
64
|
426 |
|
return $this->nodes[0]->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
189 |
|
return $this->getNodeNamespace($node); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
public function findNamespaceByName(string $name): ?Name |
71
|
|
|
{ |
72
|
4 |
|
foreach ($this->nodes as $node) { |
73
|
4 |
|
if ((string) $node->name === $name) { |
74
|
4 |
|
return $node->name; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
264 |
|
public function getCurrentNamespaceName(): ?Name |
82
|
|
|
{ |
83
|
264 |
|
$lastNode = end($this->nodes); |
84
|
|
|
|
85
|
264 |
|
return false === $lastNode ? null : $lastNode->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function count(): int |
92
|
|
|
{ |
93
|
|
|
return count($this->nodes); |
94
|
|
|
} |
95
|
|
|
|
96
|
189 |
|
private function getNodeNamespace(Node $node): ?Name |
97
|
|
|
{ |
98
|
189 |
|
if (false === ParentNodeAppender::hasParent($node)) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
189 |
|
$parentNode = ParentNodeAppender::getParent($node); |
103
|
|
|
|
104
|
189 |
|
if ($parentNode instanceof Namespace_) { |
105
|
189 |
|
return $this->mapping[(string) $parentNode->name]; |
106
|
|
|
} |
107
|
|
|
|
108
|
189 |
|
return $this->getNodeNamespace($parentNode); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
|
|
public function getIterator(): iterable |
115
|
|
|
{ |
116
|
|
|
return new ArrayIterator($this->nodes); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|