Skip to content

Commit 907d344

Browse files
authored
Move excluded (aka internal) user symbols to the SymbolsConfiguration (#603)
1 parent 790ab70 commit 907d344

File tree

9 files changed

+118
-149
lines changed

9 files changed

+118
-149
lines changed

src/Configuration/Configuration.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,6 @@ final class Configuration
3030
private Patcher $patcher;
3131
private SymbolsConfiguration $symbolsConfiguration;
3232

33-
/**
34-
* @var string[]
35-
*/
36-
private array $internalClasses;
37-
38-
/**
39-
* @var string[]
40-
*/
41-
private array $internalFunctions;
42-
43-
/**
44-
* @var string[]
45-
*/
46-
private array $internalConstants;
47-
4833
/**
4934
* @param string|null $path Absolute path to the configuration file loaded.
5035
* @param string $prefix The prefix applied.
@@ -55,20 +40,14 @@ final class Configuration
5540
* with the first argument being the file path and
5641
* the second its contents
5742
* @param SymbolsConfiguration $symbolsConfiguration
58-
* @param string[] $internalClasses
59-
* @param string[] $internalFunctions
60-
* @param string[] $internalConstants
6143
*/
6244
public function __construct(
6345
?string $path,
6446
string $prefix,
6547
array $filesWithContents,
6648
array $whitelistedFilesWithContents,
6749
Patcher $patcher,
68-
SymbolsConfiguration $symbolsConfiguration,
69-
array $internalClasses,
70-
array $internalFunctions,
71-
array $internalConstants
50+
SymbolsConfiguration $symbolsConfiguration
7251
) {
7352
self::validatePrefix($prefix);
7453

@@ -78,9 +57,6 @@ public function __construct(
7857
$this->patcher = $patcher;
7958
$this->symbolsConfiguration = $symbolsConfiguration;
8059
$this->whitelistedFilesWithContents = $whitelistedFilesWithContents;
81-
$this->internalClasses = $internalClasses;
82-
$this->internalFunctions = $internalFunctions;
83-
$this->internalConstants = $internalConstants;
8460
}
8561

8662
public function getPath(): ?string
@@ -119,30 +95,6 @@ public function getWhitelistedFilesWithContents(): array
11995
return $this->whitelistedFilesWithContents;
12096
}
12197

122-
/**
123-
* @return string[]
124-
*/
125-
public function getInternalClasses(): array
126-
{
127-
return $this->internalClasses;
128-
}
129-
130-
/**
131-
* @return string[]
132-
*/
133-
public function getInternalFunctions(): array
134-
{
135-
return $this->internalFunctions;
136-
}
137-
138-
/**
139-
* @return string[]
140-
*/
141-
public function getInternalConstants(): array
142-
{
143-
return $this->internalConstants;
144-
}
145-
14698
private static function validatePrefix(string $prefix): void
14799
{
148100
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {

src/Configuration/ConfigurationFactory.php

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public function create(?string $path = null, array $paths = []): Configuration
107107
self::retrieveFilesWithContents($whitelistedFiles),
108108
new PatcherChain($patchers),
109109
$symbolsConfiguration,
110-
...self::retrieveAllInternalSymbols($config),
111110
);
112111
}
113112

@@ -119,7 +118,7 @@ public function createWithPaths(Configuration $config, array $paths): Configurat
119118
$filesWithContents = self::retrieveFilesWithContents(
120119
chain(
121120
self::retrieveFilesFromPaths(
122-
array_unique($paths),
121+
array_unique($paths, SORT_STRING),
123122
),
124123
),
125124
);
@@ -134,9 +133,6 @@ public function createWithPaths(Configuration $config, array $paths): Configurat
134133
$config->getWhitelistedFilesWithContents(),
135134
$config->getPatcher(),
136135
$config->getSymbolsConfiguration(),
137-
$config->getInternalClasses(),
138-
$config->getInternalFunctions(),
139-
$config->getInternalConstants(),
140136
);
141137
}
142138

@@ -151,9 +147,6 @@ public function createWithPrefix(Configuration $config, string $prefix): Configu
151147
$config->getWhitelistedFilesWithContents(),
152148
$config->getPatcher(),
153149
$config->getSymbolsConfiguration(),
154-
$config->getInternalClasses(),
155-
$config->getInternalFunctions(),
156-
$config->getInternalConstants(),
157150
);
158151
}
159152

@@ -433,55 +426,6 @@ private static function retrieveFilesWithContents(iterable $files): array
433426
return $filesWithContents;
434427
}
435428

436-
/**
437-
* @return array{string[], string[], string[]}
438-
*/
439-
private static function retrieveAllInternalSymbols(array $config): array
440-
{
441-
return [
442-
self::retrieveInternalSymbols($config, ConfigurationKeys::CLASSES_INTERNAL_SYMBOLS_KEYWORD),
443-
self::retrieveInternalSymbols($config, ConfigurationKeys::FUNCTIONS_INTERNAL_SYMBOLS_KEYWORD),
444-
self::retrieveInternalSymbols($config, ConfigurationKeys::CONSTANTS_INTERNAL_SYMBOLS_KEYWORD),
445-
];
446-
}
447-
448-
/**
449-
* @return string[]
450-
*/
451-
private static function retrieveInternalSymbols(array $config, string $key): array
452-
{
453-
if (!array_key_exists($key, $config)) {
454-
return [];
455-
}
456-
457-
$symbols = $config[$key];
458-
459-
if (!is_array($symbols)) {
460-
throw new InvalidArgumentException(
461-
sprintf(
462-
'Expected "%s" to be an array of strings, got "%s" instead.',
463-
$key,
464-
gettype($symbols),
465-
),
466-
);
467-
}
468-
469-
foreach ($symbols as $index => $symbol) {
470-
if (!is_string($symbol)) {
471-
throw new InvalidArgumentException(
472-
sprintf(
473-
'Expected "%s" to be an array of strings, got "%s" for the element with the index "%s".',
474-
$key,
475-
gettype($symbol),
476-
$index,
477-
),
478-
);
479-
}
480-
}
481-
482-
return array_values(array_unique($symbols, SORT_STRING));
483-
}
484-
485429
private static function generateRandomPrefix(): string
486430
{
487431
return '_PhpScoper'.bin2hex(random_bytes(6));

src/Configuration/SymbolsConfiguration.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ final class SymbolsConfiguration
3030
private SymbolRegistry $exposedFunctions;
3131
private SymbolRegistry $exposedConstants;
3232

33+
/**
34+
* @var string[]
35+
*/
36+
private array $excludedClassNames;
37+
38+
/**
39+
* @var string[]
40+
*/
41+
private array $excludedFunctionNames;
42+
43+
/**
44+
* @var string[]
45+
*/
46+
private array $excludedConstantNames;
47+
48+
/**
49+
* @param string[] $excludedClassNames
50+
* @param string[] $excludedFunctionNames
51+
* @param string[] $excludedConstantNames
52+
*/
3353
public static function create(
3454
bool $exposeGlobalConstants = false,
3555
bool $exposeGlobalClasses = false,
@@ -40,7 +60,10 @@ public static function create(
4060
?NamespaceRegistry $exposedNamespaces = null,
4161
SymbolRegistry $exposedClasses = null,
4262
SymbolRegistry $exposedFunctions = null,
43-
SymbolRegistry $exposedConstants = null
63+
SymbolRegistry $exposedConstants = null,
64+
array $excludedClassNames = [],
65+
array $excludedFunctionNames = [],
66+
array $excludedConstantNames = []
4467
): self {
4568
return new self(
4669
$exposeGlobalConstants,
@@ -51,9 +74,17 @@ public static function create(
5174
$exposedClasses ?? SymbolRegistry::create(),
5275
$exposedFunctions ?? SymbolRegistry::create(),
5376
$exposedConstants ?? SymbolRegistry::createForConstants(),
77+
$excludedClassNames,
78+
$excludedFunctionNames,
79+
$excludedConstantNames,
5480
);
5581
}
5682

83+
/**
84+
* @param string[] $excludedClassNames
85+
* @param string[] $excludedFunctionNames
86+
* @param string[] $excludedConstantNames
87+
*/
5788
private function __construct(
5889
bool $exposeGlobalConstants,
5990
bool $exposeGlobalClasses,
@@ -62,7 +93,10 @@ private function __construct(
6293
NamespaceRegistry $exposedNamespaces,
6394
SymbolRegistry $exposedClasses,
6495
SymbolRegistry $exposedFunctions,
65-
SymbolRegistry $exposedConstants
96+
SymbolRegistry $exposedConstants,
97+
array $excludedClassNames,
98+
array $excludedFunctionNames,
99+
array $excludedConstantNames
66100
) {
67101
$this->exposeGlobalConstants = $exposeGlobalConstants;
68102
$this->exposeGlobalClasses = $exposeGlobalClasses;
@@ -72,6 +106,9 @@ private function __construct(
72106
$this->exposedClasses = $exposedClasses;
73107
$this->exposedFunctions = $exposedFunctions;
74108
$this->exposedConstants = $exposedConstants;
109+
$this->excludedClassNames = $excludedClassNames;
110+
$this->excludedFunctionNames = $excludedFunctionNames;
111+
$this->excludedConstantNames = $excludedConstantNames;
75112
}
76113

77114
public function shouldExposeGlobalConstants(): bool
@@ -113,4 +150,28 @@ public function getExposedConstants(): SymbolRegistry
113150
{
114151
return $this->exposedConstants;
115152
}
153+
154+
/**
155+
* @return string[]
156+
*/
157+
public function getExcludedClassNames(): array
158+
{
159+
return $this->excludedClassNames;
160+
}
161+
162+
/**
163+
* @return string[]
164+
*/
165+
public function getExcludedFunctionNames(): array
166+
{
167+
return $this->excludedFunctionNames;
168+
}
169+
170+
/**
171+
* @return string[]
172+
*/
173+
public function getExcludedConstantNames(): array
174+
{
175+
return $this->excludedConstantNames;
176+
}
116177
}

src/Configuration/SymbolsConfigurationFactory.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function createSymbolsConfiguration(array $config): SymbolsConfiguration
125125
$legacyExposedSymbolsPatterns,
126126
),
127127
),
128+
...self::retrieveAllExcludedSymbols($config),
128129
);
129130
}
130131

@@ -242,6 +243,18 @@ private function retrieveElements(array $config, string $key): array
242243
];
243244
}
244245

246+
/**
247+
* @return array{string[], string[], string[]}
248+
*/
249+
private static function retrieveAllExcludedSymbols(array $config): array
250+
{
251+
return [
252+
self::retrieveExcludedSymbols($config, ConfigurationKeys::CLASSES_INTERNAL_SYMBOLS_KEYWORD),
253+
self::retrieveExcludedSymbols($config, ConfigurationKeys::FUNCTIONS_INTERNAL_SYMBOLS_KEYWORD),
254+
self::retrieveExcludedSymbols($config, ConfigurationKeys::CONSTANTS_INTERNAL_SYMBOLS_KEYWORD),
255+
];
256+
}
257+
245258
/**
246259
* @deprecated
247260
*
@@ -398,4 +411,20 @@ private static function lowerCaseConstantName(string $name): string
398411

399412
return implode('\\', $parts);
400413
}
414+
415+
/**
416+
* @return string[]
417+
*/
418+
private static function retrieveExcludedSymbols(array $config, string $key): array
419+
{
420+
if (!array_key_exists($key, $config)) {
421+
return [];
422+
}
423+
424+
$symbols = $config[$key];
425+
426+
self::assertIsArrayOfStrings($symbols, $key);
427+
428+
return $symbols;
429+
}
401430
}

src/Scoper/ScoperFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ public function createScoper(
4545
): Scoper
4646
{
4747
$prefix = $configuration->getPrefix();
48+
$symbolsConfiguration = $configuration->getSymbolsConfiguration();
4849

4950
$configuredReflector = $this->reflector->withSymbols(
50-
$configuration->getInternalClasses(),
51-
$configuration->getInternalFunctions(),
52-
$configuration->getInternalConstants(),
51+
$symbolsConfiguration->getExcludedClassNames(),
52+
$symbolsConfiguration->getExcludedFunctionNames(),
53+
$symbolsConfiguration->getExcludedConstantNames(),
5354
);
5455

5556
$enrichedReflector = new EnrichedReflector(
5657
$configuredReflector,
57-
$configuration->getSymbolsConfiguration(),
58+
$symbolsConfiguration,
5859
);
5960

6061
$autoloadPrefixer = new AutoloadPrefixer(

src/Symbol/NamespaceRegistry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function Safe\substr;
1515
use function strpos;
1616
use function strtolower;
17+
use const SORT_STRING;
1718

1819
final class NamespaceRegistry
1920
{
@@ -38,8 +39,9 @@ public static function create(
3839
return new self(
3940
array_unique(
4041
array_map('strtolower', $namespaceNames),
42+
SORT_STRING,
4143
),
42-
array_unique($namespaceRegexes),
44+
array_unique($namespaceRegexes, SORT_STRING),
4345
);
4446
}
4547

0 commit comments

Comments
 (0)