Skip to content

Commit a772285

Browse files
authored
Add Configuration::withPatcher() factory (#749)
1 parent e76bd30 commit a772285

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

src/Configuration/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ public function getExcludedFilesWithContents(): array
109109
return $this->excludedFilesWithContents;
110110
}
111111

112+
public function withPatcher(Patcher $patcher): self
113+
{
114+
return new self(
115+
$this->path,
116+
$this->outputDir,
117+
$this->prefix,
118+
$this->filesWithContents,
119+
$this->excludedFilesWithContents,
120+
$patcher,
121+
$this->symbolsConfiguration,
122+
);
123+
}
124+
112125
public function getPatcher(): Patcher
113126
{
114127
return $this->patcher;

tests/Configuration/ConfigurationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ public function test_it_can_create_a_new_instance_with_a_different_prefix(): voi
7676
self::assertStateIs($newConfig, ...$expectedNewConfigValues);
7777
}
7878

79+
public function test_it_can_create_a_new_instance_with_a_different_patcher(): void
80+
{
81+
$values = [
82+
'/path/to/config',
83+
'/path/to/outputDir',
84+
'initialPrefix',
85+
['/path/to/fileA' => ['/path/to/fileA', 'fileAContent']],
86+
['/path/to/fileB' => ['/path/to/fileB', 'fileBContent']],
87+
new FakePatcher(),
88+
SymbolsConfiguration::create(),
89+
];
90+
91+
$config = new Configuration(...$values);
92+
93+
// Sanity check
94+
self::assertStateIs($config, ...$values);
95+
96+
$newPatcher = new FakePatcher();
97+
$newConfig = $config->withPatcher($newPatcher);
98+
99+
$expectedNewConfigValues = $values;
100+
$expectedNewConfigValues[5] = $newPatcher;
101+
102+
self::assertStateIs($config, ...$values);
103+
self::assertStateIs($newConfig, ...$expectedNewConfigValues);
104+
}
105+
79106
public static function prefixProvider(): iterable
80107
{
81108
yield [

vendor-hotfix/Configuration.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,17 +2764,7 @@ private static function configurePhpScoperPrefix(PhpScoperConfiguration $phpScop
27642764
return $phpScoperConfig;
27652765
}
27662766

2767-
// TODO: provide easier way to change the prefix
2768-
// https://github.com/humbug/php-scoper/issues/616
2769-
return new PhpScoperConfiguration(
2770-
$phpScoperConfig->getPath(),
2771-
$phpScoperConfig->getOutputDir(),
2772-
unique_id('_HumbugBox'),
2773-
$phpScoperConfig->getFilesWithContents(),
2774-
$phpScoperConfig->getExcludedFilesWithContents(),
2775-
$phpScoperConfig->getPatcher(),
2776-
$phpScoperConfig->getSymbolsConfiguration(),
2777-
);
2767+
return $phpScoperConfig->withPrefix(unique_id('_HumbugBox'));
27782768
}
27792769

27802770
private static function checkIfDefaultValue(

vendor-hotfix/SerializableScoper.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,8 @@ public function __construct(
3939
PhpScoperConfiguration $scoperConfig,
4040
string ...$excludedFilePaths,
4141
) {
42-
$this->scoperConfig = new PhpScoperConfiguration(
43-
$scoperConfig->getPath(),
44-
$scoperConfig->getOutputDir(),
45-
$scoperConfig->getPrefix(),
46-
$scoperConfig->getFilesWithContents(),
47-
$scoperConfig->getExcludedFilesWithContents(),
48-
PatcherFactory::createSerializablePatchers($scoperConfig->getPatcher()),
49-
$scoperConfig->getSymbolsConfiguration(),
42+
$this->scoperConfig = $scoperConfig->withPatcher(
43+
PatcherFactory::createSerializablePatchers($scoperConfig->getPatcher())
5044
);
5145
$this->excludedFilePaths = $excludedFilePaths;
5246
$this->symbolsRegistry = new SymbolsRegistry();

0 commit comments

Comments
 (0)