|
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\NodeVisitor\AppendParentNode; |
|
18
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\NamespaceStmtCollection; |
|
19
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\UseStmtCollection; |
|
20
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\NameStmtPrefixer; |
|
21
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
|
22
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
23
|
|
|
use PhpParser\Node\Name; |
|
24
|
|
|
use PhpParser\Node\Name\FullyQualified; |
|
25
|
|
|
use function in_array; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Attempts to resolve the node name into a fully qualified node. Returns a valid (non fully-qualified) name node on |
|
29
|
|
|
* failure. |
|
30
|
|
|
* |
|
31
|
|
|
* @private |
|
32
|
|
|
*/ |
|
33
|
|
|
final class FullyQualifiedNameResolver |
|
34
|
|
|
{ |
|
35
|
|
|
private $namespaceStatements; |
|
36
|
|
|
private $useStatements; |
|
37
|
|
|
|
|
38
|
371 |
|
public function __construct(NamespaceStmtCollection $namespaceStatements, UseStmtCollection $useStatements) |
|
39
|
|
|
{ |
|
40
|
371 |
|
$this->namespaceStatements = $namespaceStatements; |
|
41
|
371 |
|
$this->useStatements = $useStatements; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
275 |
|
public function resolveName(Name $node): ResolvedValue |
|
45
|
|
|
{ |
|
46
|
275 |
|
if ($node instanceof FullyQualified) { |
|
47
|
140 |
|
return new ResolvedValue($node, null, null); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
190 |
|
$namespaceName = $this->namespaceStatements->findNamespaceForNode($node); |
|
51
|
|
|
|
|
52
|
190 |
|
$useName = $this->useStatements->findStatementForNode($namespaceName, $node); |
|
53
|
|
|
|
|
54
|
190 |
|
return new ResolvedValue( |
|
55
|
190 |
|
$this->resolveNodeName($node, $namespaceName, $useName), |
|
56
|
190 |
|
$namespaceName, |
|
57
|
190 |
|
$useName |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
190 |
|
private function resolveNodeName(Name $name, ?Name $namespace, ?Name $use): Name |
|
62
|
|
|
{ |
|
63
|
190 |
|
if (null !== $use) { |
|
64
|
97 |
|
return FullyQualified::concat($use, $name->slice(1), $name->getAttributes()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
123 |
|
if (null === $namespace) { |
|
68
|
62 |
|
return new FullyQualified($name, $name->getAttributes()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
77 |
|
if (in_array((string) $name, NameStmtPrefixer::PHP_FUNCTION_KEYWORDS, true)) { |
|
72
|
1 |
|
return $name; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
77 |
|
$parentNode = AppendParentNode::getParent($name); |
|
76
|
|
|
|
|
77
|
|
|
if ( |
|
78
|
77 |
|
($parentNode instanceof ConstFetch || $parentNode instanceof FuncCall) |
|
79
|
77 |
|
&& 1 === count($name->parts) |
|
80
|
|
|
) { |
|
81
|
|
|
// Ambiguous name, cannot determine the FQ name |
|
82
|
58 |
|
return $name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
28 |
|
return FullyQualified::concat($namespace, $name, $name->getAttributes()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|