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