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
|
|
|
public function add(Namespace_ $node, Namespace_ $originalName) |
47
|
|
|
{ |
48
|
|
|
$this->nodes[] = $originalName; |
49
|
|
|
|
50
|
|
|
$this->mapping[(string) $node->name] = $originalName->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function findNamespaceForNode(Node $node): ?Name |
54
|
|
|
{ |
55
|
|
|
if (0 === count($this->nodes)) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (1 < count($this->nodes)) { |
60
|
|
|
return $this->getNodeNamespace($node); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->nodes[0]->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCurrentNamespaceName(): ?Name |
67
|
|
|
{ |
68
|
|
|
if (0 === count($this->nodes)) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return end($this->nodes)->name; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function count(): int |
79
|
|
|
{ |
80
|
|
|
return count($this->nodes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getNodeNamespace(Node $node): ?Name |
84
|
|
|
{ |
85
|
|
|
if (false === AppendParentNode::hasParent($node)) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$parentNode = AppendParentNode::getParent($node); |
90
|
|
|
|
91
|
|
|
if ($parentNode instanceof Namespace_) { |
92
|
|
|
return $this->mapping[(string) $parentNode->name]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->getNodeNamespace($parentNode); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function getIterator(): iterable |
102
|
|
|
{ |
103
|
|
|
return new ArrayIterator($this->nodes); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|