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\NamespaceStmt; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Symbol\EnrichedReflector; |
18
|
|
|
use PhpParser\Node; |
19
|
|
|
use PhpParser\Node\Name; |
20
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
21
|
|
|
use PhpParser\NodeVisitorAbstract; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Prefixes the relevant namespaces. |
25
|
|
|
* |
26
|
|
|
* ``` |
27
|
|
|
* namespace Foo; |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* => |
31
|
|
|
* |
32
|
|
|
* ``` |
33
|
|
|
* namespace Humbug\Foo; |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* @private |
37
|
|
|
*/ |
38
|
|
|
final class NamespaceStmtPrefixer extends NodeVisitorAbstract |
39
|
|
|
{ |
40
|
|
|
private string $prefix; |
41
|
|
|
private EnrichedReflector $enrichedReflector; |
42
|
|
|
private NamespaceStmtCollection $namespaceStatements; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
string $prefix, |
46
|
|
|
EnrichedReflector $enrichedReflector, |
47
|
|
|
NamespaceStmtCollection $namespaceStatements |
48
|
|
|
) { |
49
|
|
|
$this->prefix = $prefix; |
50
|
|
|
$this->enrichedReflector = $enrichedReflector; |
51
|
|
|
$this->namespaceStatements = $namespaceStatements; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function enterNode(Node $node): Node |
55
|
|
|
{ |
56
|
|
|
return ($node instanceof Namespace_) |
57
|
|
|
? $this->prefixNamespaceStmt($node) |
58
|
|
|
: $node; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function prefixNamespaceStmt(Namespace_ $namespace): Node |
62
|
|
|
{ |
63
|
|
|
if ($this->shouldPrefixStmt($namespace)) { |
64
|
|
|
self::prefixStmt($namespace, $this->prefix); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->namespaceStatements->add($namespace); |
68
|
|
|
|
69
|
|
|
return $namespace; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function shouldPrefixStmt(Namespace_ $namespace): bool |
73
|
|
|
{ |
74
|
|
|
$name = $namespace->name; |
75
|
|
|
|
76
|
|
|
if ($this->enrichedReflector->isExcludedNamespace((string) $name)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$nameFirstPart = null === $name ? '' : $name->getFirst(); |
81
|
|
|
|
82
|
|
|
return $this->prefix !== $nameFirstPart; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private static function prefixStmt(Namespace_ $namespace, string $prefix): void |
86
|
|
|
{ |
87
|
|
|
$originalName = $namespace->name; |
88
|
|
|
|
89
|
|
|
$namespace->name = Name::concat($prefix, $originalName); |
90
|
|
|
|
91
|
|
|
NamespaceManipulator::setOriginalName($namespace, $originalName); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|