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\UseStmt; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender; |
18
|
|
|
use Humbug\PhpScoper\Reflector; |
19
|
|
|
use Humbug\PhpScoper\Whitelist; |
20
|
|
|
use PhpParser\Node; |
21
|
|
|
use PhpParser\Node\Name; |
22
|
|
|
use PhpParser\Node\Stmt\Use_; |
23
|
|
|
use PhpParser\Node\Stmt\UseUse; |
24
|
|
|
use PhpParser\NodeVisitorAbstract; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prefixes the use statements. |
28
|
|
|
* |
29
|
|
|
* @private |
30
|
|
|
*/ |
31
|
|
|
final class UseStmtPrefixer extends NodeVisitorAbstract |
32
|
|
|
{ |
33
|
|
|
private string $prefix; |
34
|
|
|
private Whitelist $whitelist; |
35
|
|
|
private Reflector $reflector; |
36
|
|
|
|
37
|
549 |
|
public function __construct( |
38
|
|
|
string $prefix, |
39
|
549 |
|
Whitelist $whitelist, |
40
|
549 |
|
Reflector $reflector |
41
|
549 |
|
) { |
42
|
|
|
$this->prefix = $prefix; |
43
|
|
|
$this->reflector = $reflector; |
44
|
|
|
$this->whitelist = $whitelist; |
45
|
|
|
} |
46
|
|
|
|
47
|
548 |
|
public function enterNode(Node $node): Node |
48
|
|
|
{ |
49
|
548 |
|
if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) { |
50
|
202 |
|
self::prefixStmt($node, $this->prefix); |
51
|
|
|
} |
52
|
|
|
|
53
|
548 |
|
return $node; |
54
|
|
|
} |
55
|
|
|
|
56
|
264 |
|
private function shouldPrefixUseStmt(UseUse $use): bool |
57
|
|
|
{ |
58
|
264 |
|
$useType = self::findUseType($use); |
59
|
|
|
$nameString = $use->name->toString(); |
60
|
|
|
|
61
|
264 |
|
$alreadyPrefixed = $this->prefix === $use->name->getFirst(); |
62
|
12 |
|
|
63
|
|
|
if ($alreadyPrefixed) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
253 |
|
|
67
|
6 |
|
if ($this->whitelist->belongsToExcludedNamespace($nameString)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
247 |
|
|
71
|
21 |
|
if (Use_::TYPE_FUNCTION === $useType) { |
72
|
|
|
return !$this->reflector->isFunctionInternal($nameString); |
73
|
|
|
} |
74
|
227 |
|
|
75
|
|
|
if (Use_::TYPE_CONSTANT === $useType) { |
76
|
26 |
|
return !$this->isExposedConstant($nameString); |
77
|
26 |
|
} |
78
|
|
|
|
79
|
|
|
return Use_::TYPE_NORMAL !== $useType |
80
|
|
|
|| !$this->reflector->isClassInternal($nameString); |
81
|
202 |
|
} |
82
|
|
|
|
83
|
|
|
private static function prefixStmt(UseUse $use, string $prefix): void |
84
|
|
|
{ |
85
|
|
|
$previousName = $use->name; |
86
|
|
|
|
87
|
|
|
$prefixedName = Name::concat( |
88
|
|
|
$prefix, |
89
|
|
|
$use->name, |
90
|
|
|
$use->name->getAttributes(), |
91
|
264 |
|
); |
92
|
|
|
|
93
|
264 |
|
if (null === $prefixedName) { |
94
|
|
|
return; |
95
|
259 |
|
} |
96
|
|
|
|
97
|
259 |
|
// Unlike the new (prefixed name), the previous name will not be |
98
|
|
|
// traversed hence we need to manually set its parent attribute |
99
|
|
|
ParentNodeAppender::setParent($previousName, $use); |
100
|
5 |
|
UseStmtManipulator::setOriginalName($use, $previousName); |
101
|
|
|
|
102
|
|
|
$use->name = $prefixedName; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Finds the type of the use statement. |
107
|
|
|
* |
108
|
|
|
* @param UseUse $use |
109
|
|
|
* |
110
|
|
|
* @return int See \PhpParser\Node\Stmt\Use_ type constants. |
111
|
|
|
*/ |
112
|
|
|
private static function findUseType(UseUse $use): int |
113
|
|
|
{ |
114
|
|
|
if (Use_::TYPE_UNKNOWN === $use->type) { |
115
|
|
|
/** @var Use_ $parentNode */ |
116
|
|
|
$parentNode = ParentNodeAppender::getParent($use); |
117
|
|
|
|
118
|
|
|
return $parentNode->type; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $use->type; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function isExposedConstant(string $name): bool |
125
|
|
|
{ |
126
|
|
|
return $this->reflector->isConstantInternal($name) |
127
|
|
|
|| $this->whitelist->isExposedConstantFromGlobalNamespace($name) |
128
|
|
|
|| $this->whitelist->isSymbolExposed($name, true); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|