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