From 718f2a160992a160ffbd32a746242f7e51d729ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:53:22 +0100 Subject: [PATCH] refactor: Move the creation of the PHP-Scoper configuration into a dedicated class (#1194) refactor: Move the creation of the PHP-Scoper configuration into a dedicated class --- src/Configuration/Configuration.php | 26 ++------------- src/PhpScoper/ConfigurationFactory.php | 46 ++++++++++++++++++++++++++ tests/Compactor/PhpTest.php | 7 ++-- 3 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/PhpScoper/ConfigurationFactory.php diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 9d28de0fa..4ebc30f1c 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -19,7 +19,6 @@ use DateTimeZone; use Fidry\FileSystem\FS; use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; -use Humbug\PhpScoper\Container; use InvalidArgumentException; use KevinGH\Box\Annotation\CompactedFormatter; use KevinGH\Box\Annotation\DocblockAnnotationParser; @@ -34,6 +33,7 @@ use KevinGH\Box\MapFile; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; +use KevinGH\Box\PhpScoper\ConfigurationFactory as PhpScoperConfigurationFactory; use KevinGH\Box\PhpScoper\SerializableScoper; use Phar; use phpDocumentor\Reflection\DocBlockFactory; @@ -45,7 +45,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -use Throwable; use Webmozart\Assert\Assert; use function array_diff; use function array_filter; @@ -2546,7 +2545,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, $configFilePath = Path::makeAbsolute(self::PHP_SCOPER_CONFIG, $basePath); $configFilePath = file_exists($configFilePath) ? $configFilePath : null; - return self::createPhpScoperConfig($configFilePath); + return PhpScoperConfigurationFactory::create($configFilePath); } $configFile = $raw->{self::PHP_SCOPER_KEY}; @@ -2558,26 +2557,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, Assert::file($configFilePath); Assert::readable($configFilePath); - return self::createPhpScoperConfig($configFilePath); - } - - private static function createPhpScoperConfig(?string $filePath): PhpScoperConfiguration - { - $configFactory = (new Container())->getConfigurationFactory(); - - try { - return $configFactory->create($filePath); - } catch (Throwable $throwable) { - throw new InvalidArgumentException( - sprintf( - 'Could not create a PHP-Scoper config from the file "%s": %s', - $filePath, - $throwable->getMessage(), - ), - $throwable->getCode(), - $throwable, - ); - } + return PhpScoperConfigurationFactory::create($configFilePath); } /** diff --git a/src/PhpScoper/ConfigurationFactory.php b/src/PhpScoper/ConfigurationFactory.php new file mode 100644 index 000000000..35428a04f --- /dev/null +++ b/src/PhpScoper/ConfigurationFactory.php @@ -0,0 +1,46 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\PhpScoper; + +use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; +use Humbug\PhpScoper\Container; +use InvalidArgumentException; +use KevinGH\Box\NotInstantiable; +use Throwable; +use function sprintf; + +final class ConfigurationFactory +{ + use NotInstantiable; + + public static function create(?string $filePath): PhpScoperConfiguration + { + $configFactory = (new Container())->getConfigurationFactory(); + + try { + return $configFactory->create($filePath); + } catch (Throwable $throwable) { + throw new InvalidArgumentException( + sprintf( + 'Could not create a PHP-Scoper config from the file "%s": %s', + $filePath, + $throwable->getMessage(), + ), + $throwable->getCode(), + $throwable, + ); + } + } +} diff --git a/tests/Compactor/PhpTest.php b/tests/Compactor/PhpTest.php index 850a7dff8..5b2ed9871 100644 --- a/tests/Compactor/PhpTest.php +++ b/tests/Compactor/PhpTest.php @@ -58,8 +58,11 @@ public function test_it_supports_php_files(string $file, bool $supports): void /** * @dataProvider phpContentProvider */ - public function test_it_compacts_php_files(DocblockAnnotationParser $annotationParser, string $content, string $expected): void - { + public function test_it_compacts_php_files( + DocblockAnnotationParser $annotationParser, + string $content, + string $expected + ): void { $file = 'foo.php'; $actual = (new Php($annotationParser))->compact($file, $content);