Skip to content

Commit 5478f21

Browse files
authored
refactor: Introduce a Prefix value object (humbug#1048)
1 parent fbbcac0 commit 5478f21

File tree

4 files changed

+94
-24
lines changed

4 files changed

+94
-24
lines changed

src/Configuration/Configuration.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616

1717
use Humbug\PhpScoper\Configuration\Throwable\InvalidConfigurationValue;
1818
use Humbug\PhpScoper\Patcher\Patcher;
19-
use function Safe\preg_match;
2019

2120
final class Configuration
2221
{
23-
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u';
24-
25-
/**
26-
* @var non-empty-string
27-
*/
28-
private readonly string $prefix;
22+
private readonly Prefix $prefix;
2923

3024
/**
3125
* @param non-empty-string|null $path Absolute canonical path to the configuration file loaded.
@@ -43,15 +37,15 @@ final class Configuration
4337
public function __construct(
4438
private ?string $path,
4539
private ?string $outputDir,
46-
string $prefix,
40+
string|Prefix $prefix,
4741
private array $filesWithContents,
4842
private array $excludedFilesWithContents,
4943
private Patcher $patcher,
5044
private SymbolsConfiguration $symbolsConfiguration
5145
) {
52-
self::validatePrefix($prefix);
53-
54-
$this->prefix = $prefix;
46+
$this->prefix = $prefix instanceof Prefix
47+
? $prefix
48+
: new Prefix($prefix);
5549
}
5650

5751
/**
@@ -93,7 +87,7 @@ public function withPrefix(string $prefix): self
9387
*/
9488
public function getPrefix(): string
9589
{
96-
return $this->prefix;
90+
return $this->prefix->toString();
9791
}
9892

9993
/**
@@ -150,15 +144,4 @@ public function getSymbolsConfiguration(): SymbolsConfiguration
150144
{
151145
return $this->symbolsConfiguration;
152146
}
153-
154-
private static function validatePrefix(string $prefix): void
155-
{
156-
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
157-
throw InvalidConfigurationValue::forInvalidPrefixPattern($prefix);
158-
}
159-
160-
if (preg_match('/\\\{2,}/', $prefix)) {
161-
throw InvalidConfigurationValue::forInvalidNamespaceSeparator($prefix);
162-
}
163-
}
164147
}

src/Configuration/Prefix.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Configuration;
16+
17+
use Stringable;
18+
19+
final readonly class Prefix implements Stringable
20+
{
21+
/**
22+
* @var non-empty-string
23+
*/
24+
private string $value;
25+
26+
public function __construct(string $prefix)
27+
{
28+
PrefixValidator::validate($prefix);
29+
30+
$this->value = $prefix;
31+
}
32+
33+
/**
34+
* @return non-empty-string
35+
*/
36+
public function __toString(): string
37+
{
38+
return $this->value;
39+
}
40+
41+
/**
42+
* @return non-empty-string
43+
*/
44+
public function toString(): string
45+
{
46+
return (string) $this;
47+
}
48+
}

src/Configuration/PrefixValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Configuration;
16+
17+
use Humbug\PhpScoper\Configuration\Throwable\InvalidConfigurationValue;
18+
use function Safe\preg_match;
19+
20+
final class PrefixValidator
21+
{
22+
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u';
23+
24+
/**
25+
* @phpstan-assert non-empty-string $prefix
26+
*
27+
* @throws InvalidConfigurationValue
28+
*/
29+
public static function validate(string $prefix): void
30+
{
31+
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
32+
throw InvalidConfigurationValue::forInvalidPrefixPattern($prefix);
33+
}
34+
35+
if (preg_match('/\\\{2,}/', $prefix)) {
36+
throw InvalidConfigurationValue::forInvalidNamespaceSeparator($prefix);
37+
}
38+
}
39+
}

tests/Configuration/ConfigurationFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function test_it_can_create_a_complete_configuration(): void
127127
$configuration = $this->createConfigFromStandardFile();
128128

129129
self::assertSame($this->tmp.DIRECTORY_SEPARATOR.'scoper.inc.php', $configuration->getPath());
130-
self::assertSame('MyPrefix', $configuration->getPrefix());
130+
self::assertEquals(new Prefix('MyPrefix'), $configuration->getPrefix());
131131
self::assertSame('dist', $configuration->getOutputDir());
132132
self::assertSame([], $configuration->getFilesWithContents());
133133
self::assertSame(

0 commit comments

Comments
 (0)