-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathClassIdentifierRecorder.php
More file actions
119 lines (98 loc) · 3.61 KB
/
ClassIdentifierRecorder.php
File metadata and controls
119 lines (98 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory;
use Humbug\PhpScoper\PhpParser\NodeVisitor\AttributeAppender\ParentNodeAppender;
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\NodeVisitorAbstract;
use function array_filter;
use function array_merge;
/**
* Records the classes that need to be aliased.
*
* @private
*/
final class ClassIdentifierRecorder extends NodeVisitorAbstract
{
public function __construct(
private readonly string $prefix,
private readonly IdentifierResolver $identifierResolver,
private readonly SymbolsRegistry $symbolsRegistry,
private readonly EnrichedReflector $enrichedReflector,
) {
}
public function enterNode(Node $node): Node
{
if (!($node instanceof Identifier) || !ParentNodeAppender::hasParent($node)) {
return $node;
}
$parent = ParentNodeAppender::getParent($node);
$isClassOrInterface = $parent instanceof Class_ || $parent instanceof Interface_;
if (!$isClassOrInterface) {
return $node;
}
if (null === $parent->name) {
throw UnexpectedParsingScenario::create();
}
$resolvedName = $this->identifierResolver->resolveIdentifier($node);
if (!($resolvedName instanceof FullyQualified)) {
throw UnexpectedParsingScenario::create();
}
if ($this->shouldBeAliased($resolvedName->toString())) {
$this->symbolsRegistry->recordClass(
$resolvedName,
FullyQualifiedFactory::concat($this->prefix, $resolvedName),
self::getDependencies($parent),
);
}
return $node;
}
/**
* @return FullyQualified[]
*/
private static function getDependencies(Class_|Interface_ $node): array
{
return match(true) {
$node instanceof Class_ => self::getClassDependencies($node),
$node instanceof Interface_ => $node->extends,
};
}
private static function getClassDependencies(Class_ $class_): array
{
$dependencies = [];
if (null !== $class_->extends) {
$dependencies[] = [$class_->extends];
}
$dependencies[] = $class_->implements;
return [...$dependencies];
}
private function shouldBeAliased(string $resolvedName): bool
{
if ($this->enrichedReflector->isExposedClass($resolvedName)) {
return true;
}
// Excluded global classes (for which we found a declaration) need to be
// aliased since otherwise any usage will not point to the prefixed
// version (since it's an alias) but the declaration will now declare
// a prefixed version (due to the namespace).
return $this->enrichedReflector->belongsToGlobalNamespace($resolvedName)
&& $this->enrichedReflector->isClassExcluded($resolvedName);
}
}