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