Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Introduce a Prefix value object #1048

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -93,7 +87,7 @@ public function withPrefix(string $prefix): self
*/
public function getPrefix(): string
{
return $this->prefix;
return $this->prefix->toString();
}

/**
Expand Down Expand Up @@ -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);
}
}
}
48 changes: 48 additions & 0 deletions src/Configuration/Prefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* 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;
}
}
39 changes: 39 additions & 0 deletions src/Configuration/PrefixValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* 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);
}
}
}
2 changes: 1 addition & 1 deletion tests/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading