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\UseStmt; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\NodeVisitor\AppendParentNode; |
18
|
|
|
use Humbug\PhpScoper\Reflector; |
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
|
|
|
final class UseStmtPrefixer extends NodeVisitorAbstract |
29
|
|
|
{ |
30
|
|
|
private $prefix; |
31
|
|
|
private $reflector; |
32
|
|
|
|
33
|
342 |
|
public function __construct(string $prefix, Reflector $reflector) |
34
|
|
|
{ |
35
|
342 |
|
$this->prefix = $prefix; |
36
|
342 |
|
$this->reflector = $reflector; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
341 |
|
public function enterNode(Node $node) |
43
|
|
|
{ |
44
|
341 |
|
if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) { |
45
|
157 |
|
$node->name = Name::concat($this->prefix, $node->name); |
46
|
|
|
} |
47
|
|
|
|
48
|
341 |
|
return $node; |
49
|
|
|
} |
50
|
|
|
|
51
|
196 |
|
private function shouldPrefixUseStmt(UseUse $use): bool |
52
|
|
|
{ |
53
|
196 |
|
$useType = $this->findUseType($use); |
54
|
|
|
|
55
|
|
|
// If is already from the prefix namespace |
56
|
196 |
|
if ($this->prefix === $use->name->getFirst()) { |
57
|
11 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Is not from the Composer namespace |
61
|
185 |
|
if ('Composer' === $use->name->getFirst()) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
185 |
|
if (Use_::TYPE_FUNCTION === $useType) { |
66
|
16 |
|
return false === $this->reflector->isFunctionInternal((string) $use->name); |
67
|
|
|
} |
68
|
|
|
|
69
|
170 |
|
if (Use_::TYPE_CONSTANT === $useType) { |
70
|
17 |
|
return false === $this->reflector->isConstantInternal((string) $use->name); |
71
|
|
|
} |
72
|
|
|
|
73
|
154 |
|
return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal((string) $use->name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Finds the type of the use statement. |
78
|
|
|
* |
79
|
|
|
* @param UseUse $use |
80
|
|
|
* |
81
|
|
|
* @return int See \PhpParser\Node\Stmt\Use_ type constants. |
82
|
|
|
*/ |
83
|
196 |
|
private function findUseType(UseUse $use): int |
84
|
|
|
{ |
85
|
196 |
|
if (Use_::TYPE_UNKNOWN === $use->type) { |
86
|
|
|
/** @var Use_ $parentNode */ |
87
|
191 |
|
$parentNode = AppendParentNode::getParent($use); |
88
|
|
|
|
89
|
191 |
|
return $parentNode->type; |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
return $use->type; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|