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 $prefix; |
34
|
|
|
private $whitelist; |
35
|
|
|
private $reflector; |
36
|
|
|
|
37
|
10 |
|
public function __construct(string $prefix, Whitelist $whitelist, Reflector $reflector) |
38
|
|
|
{ |
39
|
10 |
|
$this->prefix = $prefix; |
40
|
10 |
|
$this->reflector = $reflector; |
41
|
10 |
|
$this->whitelist = $whitelist; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
10 |
|
public function enterNode(Node $node) |
48
|
|
|
{ |
49
|
10 |
|
if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) { |
50
|
|
|
$node->name = Name::concat($this->prefix, $node->name, $node->name->getAttributes()); |
51
|
|
|
} |
52
|
|
|
|
53
|
10 |
|
return $node; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function shouldPrefixUseStmt(UseUse $use): bool |
57
|
|
|
{ |
58
|
|
|
$useType = $this->findUseType($use); |
59
|
|
|
|
60
|
|
|
// If is already from the prefix namespace |
61
|
|
|
if ($this->prefix === $use->name->getFirst()) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// If is whitelisted |
66
|
|
|
if ($this->whitelist->belongsToWhitelistedNamespace((string) $use->name)) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (Use_::TYPE_FUNCTION === $useType) { |
71
|
|
|
return false === $this->reflector->isFunctionInternal((string) $use->name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (Use_::TYPE_CONSTANT === $useType) { |
75
|
|
|
return |
76
|
|
|
false === $this->whitelist->isSymbolWhitelisted((string) $use->name) |
77
|
|
|
&& false === $this->reflector->isConstantInternal((string) $use->name) |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal((string) $use->name); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Finds the type of the use statement. |
86
|
|
|
* |
87
|
|
|
* @param UseUse $use |
88
|
|
|
* |
89
|
|
|
* @return int See \PhpParser\Node\Stmt\Use_ type constants. |
90
|
|
|
*/ |
91
|
|
|
private function findUseType(UseUse $use): int |
92
|
|
|
{ |
93
|
|
|
if (Use_::TYPE_UNKNOWN === $use->type) { |
94
|
|
|
/** @var Use_ $parentNode */ |
95
|
|
|
$parentNode = ParentNodeAppender::getParent($use); |
96
|
|
|
|
97
|
|
|
return $parentNode->type; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $use->type; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|