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\Resolver; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\PhpParser\Node\NamedIdentifier; |
18
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender; |
19
|
|
|
use PhpParser\Node\Identifier; |
20
|
|
|
use PhpParser\Node\Name; |
21
|
|
|
use PhpParser\Node\Name\FullyQualified; |
22
|
|
|
use PhpParser\Node\Scalar\String_; |
23
|
|
|
use PhpParser\Node\Stmt\Function_; |
24
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
25
|
|
|
use function array_filter; |
26
|
|
|
use function implode; |
27
|
|
|
use function ltrim; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Attempts to resolve the identifier node into a fully qualified node. Returns a valid |
31
|
|
|
* (non fully-qualified) name node on failure. |
32
|
|
|
* |
33
|
|
|
* @private |
34
|
|
|
*/ |
35
|
|
|
final class IdentifierResolver |
36
|
|
|
{ |
37
|
|
|
private NameResolver $nameResolver; |
38
|
|
|
|
39
|
|
|
public function __construct(NameResolver $nameResolver) |
40
|
|
|
{ |
41
|
|
|
$this->nameResolver = $nameResolver; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function resolveIdentifier(Identifier $identifier): Name |
45
|
|
|
{ |
46
|
|
|
$parentNode = ParentNodeAppender::getParent($identifier); |
47
|
|
|
|
48
|
|
|
if ($parentNode instanceof Function_) { |
49
|
|
|
return $this->resolveFunctionIdentifier($identifier); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$name = NamedIdentifier::create($identifier); |
53
|
|
|
|
54
|
|
|
return $this->nameResolver |
55
|
|
|
->getNameContext() |
56
|
|
|
->getResolvedClassName($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function resolveString(String_ $string): Name |
60
|
|
|
{ |
61
|
|
|
$name = new FullyQualified( |
62
|
|
|
ltrim($string->value, '\\'), |
63
|
|
|
$string->getAttributes(), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $this->nameResolver |
67
|
|
|
->getNameContext() |
68
|
|
|
->getResolvedClassName($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function resolveFunctionIdentifier(Identifier $identifier): Name |
72
|
|
|
{ |
73
|
|
|
$nameParts = array_filter([ |
74
|
|
|
$this->nameResolver->getNameContext()->getNamespace(), |
75
|
|
|
$identifier->toString(), |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
return new FullyQualified( |
79
|
|
|
implode( |
80
|
|
|
'\\', |
81
|
|
|
$nameParts, |
82
|
|
|
), |
83
|
|
|
$identifier->getAttributes(), |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|