Skip to content

Commit 430aa2f

Browse files
authored
Make prefix non-nullable (#485)
1 parent 338f36f commit 430aa2f

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed

src/Configuration.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function array_reduce;
3030
use function array_unique;
3131
use function array_unshift;
32+
use function bin2hex;
3233
use function dirname;
3334
use function file_exists;
3435
use function gettype;
@@ -42,6 +43,7 @@
4243
use function is_readable;
4344
use function is_string;
4445
use function iterator_to_array;
46+
use function random_bytes;
4547
use function realpath as native_realpath;
4648
use function Safe\file_get_contents;
4749
use function Safe\preg_match;
@@ -73,7 +75,7 @@ final class Configuration
7375
];
7476

7577
private ?string $path;
76-
private ?string $prefix;
78+
private string $prefix;
7779
private array $filesWithContents;
7880
private array $patchers;
7981
private Whitelist $whitelist;
@@ -150,19 +152,22 @@ public static function load(string $path = null, array $paths = []): self
150152
}
151153

152154
/**
153-
* @param string|null $path Absolute path to the configuration file loaded.
154-
* @param string|null $prefix The prefix applied.
155-
* @param string[][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents
156-
* @param callable[] $patchers List of closures which can alter the content of the files being
157-
* scoped.
158-
* @param Whitelist $whitelist List of classes that will not be scoped.
159-
* returning a boolean which if `true` means the class should be scoped
160-
* (i.e. is ignored) or scoped otherwise.
161-
* @param string[] $whitelistedFiles List of absolute paths of files to completely ignore
155+
* @param string|null $path Absolute path to the configuration file loaded.
156+
* @param string $prefix The prefix applied.
157+
* @param string[][] $filesWithContents Array of tuple with the first argument being the
158+
* file path and the second its contents
159+
* @param callable[] $patchers List of closures which can alter the content of
160+
* the files being scoped.
161+
* @param Whitelist $whitelist List of classes that will not be scoped.
162+
* returning a boolean which if `true` means the
163+
* class should be scoped
164+
* (i.e. is ignored) or scoped otherwise.
165+
* @param string[] $whitelistedFiles List of absolute paths of files to completely
166+
* ignore
162167
*/
163168
private function __construct(
164169
?string $path,
165-
?string $prefix,
170+
string $prefix,
166171
array $filesWithContents,
167172
array $patchers,
168173
Whitelist $whitelist,
@@ -215,7 +220,7 @@ public function getPath(): ?string
215220
return $this->path;
216221
}
217222

218-
public function getPrefix(): ?string
223+
public function getPrefix(): string
219224
{
220225
return $this->prefix;
221226
}
@@ -266,24 +271,18 @@ private static function validateConfigKey(string $key): void
266271
}
267272
}
268273

269-
/**
270-
* If the prefix is set to null in the config file/argument then a random prefix is being used. However if set to
271-
* empty, the configuration will use a null prefix.
272-
*
273-
* TL:DR; setting the prefix is a big confusing because it is not properly split in "set prefix" & prefix strategy".
274-
*/
275-
private static function retrievePrefix(array $config): ?string
274+
private static function retrievePrefix(array $config): string
276275
{
277276
$prefix = $config[self::PREFIX_KEYWORD] ?? null;
278277

279278
if (null === $prefix) {
280-
return null;
279+
return self::generateRandomPrefix();
281280
}
282281

283282
$prefix = trim($prefix);
284283

285284
if ('' === $prefix) {
286-
return null;
285+
return self::generateRandomPrefix();
287286
}
288287

289288
if (1 === preg_match('/^[\p{L}\d_]+$/u', $prefix)) {
@@ -569,4 +568,9 @@ static function (array $files, SplFileInfo $fileInfo): array {
569568
[]
570569
);
571570
}
571+
572+
private static function generateRandomPrefix(): string
573+
{
574+
return '_PhpScoper'.bin2hex(random_bytes(6));
575+
}
572576
}

src/Console/ConfigLoader.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ private static function configurePrefix(Configuration $config, string $prefix):
196196
return $config->withPrefix($prefix);
197197
}
198198

199-
if (null === $config->getPrefix()) {
200-
return $config->withPrefix(self::generateRandomPrefix());
201-
}
202-
203199
return $config;
204200
}
205201

@@ -217,11 +213,6 @@ private static function configurePaths(
217213
return $config;
218214
}
219215

220-
private static function generateRandomPrefix(): string
221-
{
222-
return '_PhpScoper'.bin2hex(random_bytes(6));
223-
}
224-
225216
private function makeAbsolutePath(
226217
string $path,
227218
string $cwd

src/Console/ConsoleScoper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private function scopeFile(
169169
$scoppedContent = $this->scoper->scope(
170170
$inputFilePath,
171171
$inputContents,
172-
(string) $config->getPrefix(),
172+
$config->getPrefix(),
173173
$config->getPatchers(),
174174
$config->getWhitelist(),
175175
);

tests/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function test_it_can_be_created_without_a_file(): void
3535
$configuration->getWhitelist()
3636
);
3737
$this->assertNull($configuration->getPath());
38-
$this->assertNull($configuration->getPrefix());
38+
$this->assertMatchesRegularExpression('/_PhpScoper[a-z\d]{12}/', $configuration->getPrefix());
3939
$this->assertSame([], $configuration->getFilesWithContents());
4040
$this->assertEquals([new SymfonyPatcher()], $configuration->getPatchers());
4141
}

0 commit comments

Comments
 (0)