-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathEnrichedReflector.php
166 lines (144 loc) · 5.36 KB
/
EnrichedReflector.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?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\Symbol;
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
use function ltrim;
use function str_contains;
/**
* Combines the API or the "traditional" reflector which is about to tell
* if a symbol is internal or not with the more PHP-Scoper specific exposed
* API.
*
* To recap, the configurations to take into account:
* - excluded & exposed namespaces (exclusion takes priority)
* - internal symbols
* - exposed symbols
* - whether symbols from the global namespace should be exposed or not
*/
final readonly class EnrichedReflector
{
public function __construct(
private Reflector $reflector,
private SymbolsConfiguration $symbolsConfiguration,
) {
}
public function belongsToExcludedNamespace(string $name): bool
{
return $this->symbolsConfiguration
->getExcludedNamespaces()
->belongsToRegisteredNamespace($name);
}
private function belongsToExposedNamespace(string $name): bool
{
return $this->symbolsConfiguration
->getExposedNamespaces()
->belongsToRegisteredNamespace($name);
}
public function isFunctionInternal(string $name): bool
{
return $this->reflector->isFunctionInternal($name);
}
public function isFunctionExcluded(string $name): bool
{
return $this->reflector->isFunctionInternal($name)
|| $this->belongsToExcludedNamespace($name);
}
public function isClassInternal(string $name): bool
{
return $this->reflector->isClassInternal($name);
}
public function isClassExcluded(string $name): bool
{
return $this->reflector->isClassInternal($name)
|| $this->belongsToExcludedNamespace($name);
}
public function isConstantInternal(string $name): bool
{
return $this->reflector->isConstantInternal($name);
}
public function isExposedFunction(string $resolvedName): bool
{
return !$this->isFunctionExcluded($resolvedName)
&& (
$this->isExposedFunctionFromGlobalNamespaceWithoutExclusionCheck($resolvedName)
|| $this->symbolsConfiguration
->getExposedFunctions()
->matches($resolvedName)
|| $this->belongsToExposedNamespace($resolvedName)
);
}
public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool
{
return !$this->isFunctionExcluded($resolvedName)
&& $this->isExposedFunctionFromGlobalNamespaceWithoutExclusionCheck($resolvedName);
}
public function isExposedClass(string $resolvedName): bool
{
return !$this->isClassExcluded($resolvedName)
&& (
$this->isExposedClassFromGlobalNamespaceWithoutExclusionCheck($resolvedName)
|| $this->symbolsConfiguration
->getExposedClasses()
->matches($resolvedName)
|| $this->belongsToExposedNamespace($resolvedName)
);
}
public function isExposedClassFromGlobalNamespace(string $resolvedName): bool
{
return !$this->isClassExcluded($resolvedName)
&& $this->isExposedClassFromGlobalNamespaceWithoutExclusionCheck($resolvedName);
}
public function isExposedConstant(string $name): bool
{
// Special case: internal constants must be treated as exposed symbols.
//
// Example: when declaring a new internal constant for compatibility
// reasons, it must remain un-prefixed.
return !$this->belongsToExcludedNamespace($name)
&& (
$this->reflector->isConstantInternal($name)
|| $this->isExposedConstantFromGlobalNamespace($name)
|| $this->symbolsConfiguration
->getExposedConstants()
->matches($name)
|| $this->belongsToExposedNamespace($name)
);
}
public function isExposedConstantFromGlobalNamespace(string $constantName): bool
{
return $this->symbolsConfiguration->shouldExposeGlobalConstants()
&& $this->belongsToGlobalNamespace($constantName);
}
public function isExcludedNamespace(string $name): bool
{
return $this->symbolsConfiguration
->getExcludedNamespaces()
->isRegisteredNamespace($name);
}
private function isExposedFunctionFromGlobalNamespaceWithoutExclusionCheck(string $functionName): bool
{
return $this->symbolsConfiguration->shouldExposeGlobalFunctions()
&& $this->belongsToGlobalNamespace($functionName);
}
private function isExposedClassFromGlobalNamespaceWithoutExclusionCheck(string $className): bool
{
return $this->symbolsConfiguration->shouldExposeGlobalClasses()
&& $this->belongsToGlobalNamespace($className);
}
public function belongsToGlobalNamespace(string $symbolName): bool
{
return !str_contains(
ltrim($symbolName, '\\'),
'\\',
);
}
}