diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 723c8dea..127e853e 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -16,16 +16,10 @@ use Humbug\PhpScoper\Configuration\Throwable\InvalidConfigurationValue; use Humbug\PhpScoper\Patcher\Patcher; -use function Safe\preg_match; final class Configuration { - private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; - - /** - * @var non-empty-string - */ - private readonly string $prefix; + private readonly Prefix $prefix; /** * @param non-empty-string|null $path Absolute canonical path to the configuration file loaded. @@ -43,15 +37,15 @@ final class Configuration public function __construct( private ?string $path, private ?string $outputDir, - string $prefix, + string|Prefix $prefix, private array $filesWithContents, private array $excludedFilesWithContents, private Patcher $patcher, private SymbolsConfiguration $symbolsConfiguration ) { - self::validatePrefix($prefix); - - $this->prefix = $prefix; + $this->prefix = $prefix instanceof Prefix + ? $prefix + : new Prefix($prefix); } /** @@ -93,7 +87,7 @@ public function withPrefix(string $prefix): self */ public function getPrefix(): string { - return $this->prefix; + return $this->prefix->toString(); } /** @@ -150,15 +144,4 @@ public function getSymbolsConfiguration(): SymbolsConfiguration { return $this->symbolsConfiguration; } - - private static function validatePrefix(string $prefix): void - { - if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { - throw InvalidConfigurationValue::forInvalidPrefixPattern($prefix); - } - - if (preg_match('/\\\{2,}/', $prefix)) { - throw InvalidConfigurationValue::forInvalidNamespaceSeparator($prefix); - } - } } diff --git a/src/Configuration/Prefix.php b/src/Configuration/Prefix.php new file mode 100644 index 00000000..3ce61130 --- /dev/null +++ b/src/Configuration/Prefix.php @@ -0,0 +1,48 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\Configuration; + +use Stringable; + +final readonly class Prefix implements Stringable +{ + /** + * @var non-empty-string + */ + private string $value; + + public function __construct(string $prefix) + { + PrefixValidator::validate($prefix); + + $this->value = $prefix; + } + + /** + * @return non-empty-string + */ + public function __toString(): string + { + return $this->value; + } + + /** + * @return non-empty-string + */ + public function toString(): string + { + return (string) $this; + } +} diff --git a/src/Configuration/PrefixValidator.php b/src/Configuration/PrefixValidator.php new file mode 100644 index 00000000..9a816703 --- /dev/null +++ b/src/Configuration/PrefixValidator.php @@ -0,0 +1,39 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\Configuration; + +use Humbug\PhpScoper\Configuration\Throwable\InvalidConfigurationValue; +use function Safe\preg_match; + +final class PrefixValidator +{ + private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; + + /** + * @phpstan-assert non-empty-string $prefix + * + * @throws InvalidConfigurationValue + */ + public static function validate(string $prefix): void + { + if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { + throw InvalidConfigurationValue::forInvalidPrefixPattern($prefix); + } + + if (preg_match('/\\\{2,}/', $prefix)) { + throw InvalidConfigurationValue::forInvalidNamespaceSeparator($prefix); + } + } +} diff --git a/tests/Configuration/ConfigurationFactoryTest.php b/tests/Configuration/ConfigurationFactoryTest.php index 2902dafc..aa8f3f1d 100644 --- a/tests/Configuration/ConfigurationFactoryTest.php +++ b/tests/Configuration/ConfigurationFactoryTest.php @@ -127,7 +127,7 @@ public function test_it_can_create_a_complete_configuration(): void $configuration = $this->createConfigFromStandardFile(); self::assertSame($this->tmp.DIRECTORY_SEPARATOR.'scoper.inc.php', $configuration->getPath()); - self::assertSame('MyPrefix', $configuration->getPrefix()); + self::assertEquals(new Prefix('MyPrefix'), $configuration->getPrefix()); self::assertSame('dist', $configuration->getOutputDir()); self::assertSame([], $configuration->getFilesWithContents()); self::assertSame(