Skip to content
New issue

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

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

Already on GitHub? # to your account

Refactor some code #754

Merged
merged 1 commit into from
Nov 12, 2022
Merged
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
16 changes: 16 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
@@ -93,6 +93,22 @@ public function getPrefix(): string
return $this->prefix;
}

/**
* @param array<string, array{string, string}> $filesWithContents
*/
public function withFilesWithContents(array $filesWithContents): self
{
return new self(
$this->path,
$this->outputDir,
$this->prefix,
$filesWithContents,
$this->excludedFilesWithContents,
$this->patcher,
$this->symbolsConfiguration,
);
}

/**
* @return array<string, array{string, string}>
*/
29 changes: 6 additions & 23 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -50,7 +50,6 @@
use function sprintf;
use function trim;
use const DIRECTORY_SEPARATOR;
use const SORT_STRING;

final class ConfigurationFactory
{
@@ -117,38 +116,22 @@ public function createWithPaths(Configuration $config, array $paths): Configurat
$filesWithContents = self::retrieveFilesWithContents(
chain(
self::retrieveFilesFromPaths(
array_unique($paths, SORT_STRING),
array_unique($paths),
),
),
);

return new Configuration(
$config->getPath(),
$config->getOutputDir(),
$config->getPrefix(),
[
...$config->getFilesWithContents(),
...$filesWithContents,
],
$config->getExcludedFilesWithContents(),
$config->getPatcher(),
$config->getSymbolsConfiguration(),
);
return $config->withFilesWithContents([
...$config->getFilesWithContents(),
...$filesWithContents,
]);
}

public function createWithPrefix(Configuration $config, string $prefix): Configuration
{
$prefix = self::retrievePrefix([ConfigurationKeys::PREFIX_KEYWORD => $prefix]);

return new Configuration(
$config->getPath(),
$config->getOutputDir(),
$prefix,
$config->getFilesWithContents(),
$config->getExcludedFilesWithContents(),
$config->getPatcher(),
$config->getSymbolsConfiguration(),
);
return $config->withPrefix($prefix);
}

private function loadConfigFile(string $path): array
20 changes: 8 additions & 12 deletions src/Console/ConfigLoader.php
Original file line number Diff line number Diff line change
@@ -197,14 +197,12 @@ private function loadConfiguration(

private function configurePrefix(Configuration $config, string $prefix): Configuration
{
if ('' !== $prefix) {
return $this->configFactory->createWithPrefix(
return '' !== $prefix
? $this->configFactory->createWithPrefix(
$config,
$prefix,
);
}

return $config;
)
: $config;
}

private function configurePaths(
@@ -213,14 +211,12 @@ private function configurePaths(
): Configuration {
// Use the current working directory as the path if no file has been
// found
if (0 === count($config->getFilesWithContents())) {
return $this->configFactory->createWithPaths(
return 0 === count($config->getFilesWithContents())
? $this->configFactory->createWithPaths(
$config,
[$cwd],
);
}

return $config;
)
: $config;
}

/**