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; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection; |
18
|
|
|
use PhpParser\Node; |
19
|
|
|
use PhpParser\Node\Name; |
20
|
|
|
use PhpParser\Node\Stmt\Class_; |
21
|
|
|
use PhpParser\Node\Stmt\Interface_; |
22
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
23
|
|
|
use PhpParser\NodeVisitorAbstract; |
24
|
|
|
use function Humbug\PhpScoper\clone_node; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prefixes the relevant namespaces. |
28
|
|
|
* |
29
|
|
|
* ``` |
30
|
|
|
* namespace Foo; |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* => |
34
|
|
|
* |
35
|
|
|
* ``` |
36
|
|
|
* namespace Humbug\Foo; |
37
|
|
|
* ``` |
38
|
|
|
*/ |
39
|
|
|
final class NamespaceStmtPrefixer extends NodeVisitorAbstract |
40
|
|
|
{ |
41
|
|
|
private $prefix; |
42
|
|
|
private $namespaceStatements; |
43
|
|
|
|
44
|
338 |
|
public function __construct(string $prefix, NamespaceStmtCollection $namespaceStatements) |
45
|
|
|
{ |
46
|
338 |
|
$this->prefix = $prefix; |
47
|
338 |
|
$this->namespaceStatements = $namespaceStatements; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
337 |
|
public function enterNode(Node $node): Node |
54
|
|
|
{ |
55
|
337 |
|
return ($node instanceof Namespace_) |
56
|
335 |
|
? $this->prefixNamespaceStmt($node) |
57
|
337 |
|
: $node |
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
|
61
|
335 |
|
private function prefixNamespaceStmt(Namespace_ $namespace): Node |
62
|
|
|
{ |
63
|
335 |
|
$originalNamespace = $namespace; |
64
|
|
|
|
65
|
335 |
|
if ($this->shouldPrefixStmt($namespace)) { |
66
|
332 |
|
$originalNamespace = clone_node($namespace); |
67
|
|
|
|
68
|
332 |
|
$namespace->name = Name::concat($this->prefix, $namespace->name); |
69
|
|
|
} |
70
|
|
|
|
71
|
335 |
|
$this->namespaceStatements->add($namespace, $originalNamespace); |
72
|
|
|
|
73
|
335 |
|
return $namespace; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function isWhitelistedNode(Node $node) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (($node instanceof Class_ || $node instanceof Interface_)) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Check nodes in the global namespaces. |
83
|
|
|
if ($node instanceof Namespace_ && null === $node->name) { |
84
|
|
|
foreach ($node->stmts as $statement) { |
85
|
|
|
if ($this->isWhitelistedNode($statement)) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
335 |
|
private function shouldPrefixStmt(Namespace_ $namespace): bool |
95
|
|
|
{ |
96
|
335 |
|
return null === $namespace->name || (null !== $namespace->name && $this->prefix !== $namespace->name->getFirst()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|