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; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\PhpParser\Node\ClassAliasFuncCall; |
18
|
|
|
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory; |
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver; |
20
|
|
|
use Humbug\PhpScoper\Whitelist; |
21
|
|
|
use PhpParser\Node; |
22
|
|
|
use PhpParser\Node\Name; |
23
|
|
|
use PhpParser\Node\Name\FullyQualified; |
24
|
|
|
use PhpParser\Node\Stmt; |
25
|
|
|
use PhpParser\Node\Stmt\Class_; |
26
|
|
|
use PhpParser\Node\Stmt\Expression; |
27
|
|
|
use PhpParser\Node\Stmt\Interface_; |
28
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
29
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
30
|
|
|
use PhpParser\NodeVisitorAbstract; |
31
|
|
|
use function array_reduce; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* In some contexts we need to resolve identifiers but they can no longer be |
35
|
|
|
* resolved on the fly. For those, we store the resolved identifier as an |
36
|
|
|
* attribute. |
37
|
|
|
* |
38
|
|
|
* @see ClassAliasStmtAppender |
39
|
|
|
* |
40
|
|
|
* @private |
41
|
|
|
*/ |
42
|
|
|
final class IdentifierNameAppender extends NodeVisitorAbstract |
43
|
|
|
{ |
44
|
|
|
private IdentifierResolver $identifierResolver; |
45
|
|
|
|
46
|
|
|
public function __construct(IdentifierResolver $identifierResolver) |
47
|
|
|
{ |
48
|
|
|
$this->identifierResolver = $identifierResolver; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function enterNode(Node $node): void |
52
|
|
|
{ |
53
|
|
|
if (!($node instanceof Class_ || $node instanceof Interface_)) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$name = $node->name; |
58
|
|
|
|
59
|
|
|
if (null === $name) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$resolvedName = $this->identifierResolver->resolveIdentifier($node->name); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$name->setAttribute('resolvedName', $resolvedName); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|