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; |
16
|
|
|
|
17
|
|
|
use PhpParser\Node; |
18
|
|
|
use PhpParser\Node\Name; |
19
|
|
|
use PhpParser\Node\Stmt\GroupUse; |
20
|
|
|
use PhpParser\Node\Stmt\UseUse; |
21
|
|
|
use PhpParser\NodeVisitorAbstract; |
22
|
|
|
|
23
|
|
|
final class SingleLevelUseAliasVisitor extends NodeVisitorAbstract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $prefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $aliases; |
34
|
|
|
|
35
|
|
|
public function __construct(string $prefix) |
36
|
|
|
{ |
37
|
|
|
$this->prefix = $prefix; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function beforeTraverse(array $nodes) |
44
|
|
|
{ |
45
|
|
|
$this->aliases = []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function enterNode(Node $node) |
52
|
|
|
{ |
53
|
|
|
/* Collate all single level aliases */ |
54
|
|
|
if ($node instanceof UseUse |
55
|
|
|
&& (!$node->hasAttribute('parent') |
56
|
|
|
|| false === ($node->getAttribute('parent') instanceof GroupUse)) |
57
|
|
|
&& $this->prefix !== $node->name->getFirst() |
58
|
|
|
&& 1 === count($node->name->parts) |
|
|
|
|
59
|
|
|
&& $node->alias !== $node->name->getFirst() |
60
|
|
|
) { |
61
|
|
|
$this->aliases[$node->alias] = $node; |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->scopeUseStmtIfUsedInAnyNameAsAliasedNamespace($node); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Node $node |
71
|
|
|
*/ |
72
|
|
|
private function scopeUseStmtIfUsedInAnyNameAsAliasedNamespace(Node $node) |
73
|
|
|
{ |
74
|
|
|
if ($node instanceof Name |
75
|
|
|
&& 1 < count($node->parts) |
|
|
|
|
76
|
|
|
&& in_array($node->getFirst(), array_keys($this->aliases)) |
77
|
|
|
) { |
78
|
|
|
$nodeToPrefix = $this->aliases[$node->getFirst()]; |
79
|
|
|
$nodeToPrefix->name = Name::concat($this->prefix, $nodeToPrefix->name); |
80
|
|
|
unset($this->aliases[$node->getFirst()]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.