From ab3e0f6a59cc05bef2729f173202cfc557e3a24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:28:08 +0100 Subject: [PATCH] refactor: Make RequirmentAppFactory non-static (#1274) --- src/Console/Application.php | 9 +++- src/Console/Command/Compile.php | 23 ++++++--- .../AppRequirementsFactory.php | 2 +- .../RequirementCheckerFinder.php | 47 +++++++++++++++++++ src/RequirementChecker/RequirementsDumper.php | 23 +++++---- .../Benchmark/AppRequirementFactoryBench.php | 4 +- tests/Console/Command/CompileTest.php | 5 ++ .../AppRequirementsFactoryTest.php | 2 +- .../RequirementsDumperTest.php | 11 ++++- 9 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 src/RequirementChecker/RequirementCheckerFinder.php diff --git a/src/Console/Application.php b/src/Console/Application.php index 92043fe81..f3aee842c 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -28,6 +28,8 @@ use KevinGH\Box\Console\Command\Process; use KevinGH\Box\Console\Command\Validate; use KevinGH\Box\Console\Command\Verify; +use KevinGH\Box\RequirementChecker\AppRequirementsFactory; +use KevinGH\Box\RequirementChecker\RequirementsDumper; use function KevinGH\Box\get_box_version; use function sprintf; use function trim; @@ -93,7 +95,12 @@ public function getCommands(): array return [ new ComposerCheckVersion(), new ComposerVendorDir(), - new Compile($this->getHeader()), + new Compile( + $this->getHeader(), + new RequirementsDumper( + new AppRequirementsFactory(), + ), + ), new Diff(), new Info(), new Info('info:general'), diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 73af2b6ee..6c8b05e6a 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -109,8 +109,10 @@ final class Compile implements CommandAware private const DEBUG_DIR = '.box_dump'; - public function __construct(private readonly string $header) - { + public function __construct( + private readonly string $header, + private readonly RequirementsDumper $requirementsDumper, + ) { } public function getConfiguration(): CommandConfiguration @@ -268,7 +270,12 @@ private function createPhar( // file used for debugging purposes and the Composer dump autoloading will not work correctly otherwise. $main = self::registerMainScript($config, $box, $logger); - $check = self::registerRequirementsChecker($config, $box, $logger); + $check = self::registerRequirementsChecker( + $config, + $box, + $this->requirementsDumper, + $logger, + ); self::addFiles($config, $box, $logger, $io); @@ -548,8 +555,12 @@ private static function registerMainScript(Configuration $config, Box $box, Comp return $localMain; } - private static function registerRequirementsChecker(Configuration $config, Box $box, CompilerLogger $logger): bool - { + private static function registerRequirementsChecker( + Configuration $config, + Box $box, + RequirementsDumper $requirementsDumper, + CompilerLogger $logger, + ): bool { if (false === $config->checkRequirements()) { $logger->log( CompilerLogger::QUESTION_MARK_PREFIX, @@ -564,7 +575,7 @@ private static function registerRequirementsChecker(Configuration $config, Box $ 'Adding requirements checker', ); - $checkFiles = RequirementsDumper::dump( + $checkFiles = $requirementsDumper->dump( $config->getComposerJson(), $config->getComposerLock(), $config->getCompressionAlgorithm(), diff --git a/src/RequirementChecker/AppRequirementsFactory.php b/src/RequirementChecker/AppRequirementsFactory.php index f67696203..db83165f2 100644 --- a/src/RequirementChecker/AppRequirementsFactory.php +++ b/src/RequirementChecker/AppRequirementsFactory.php @@ -28,7 +28,7 @@ final class AppRequirementsFactory { private const SELF_PACKAGE = null; - public static function create( + public function create( ComposerJson $composerJson, ComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, diff --git a/src/RequirementChecker/RequirementCheckerFinder.php b/src/RequirementChecker/RequirementCheckerFinder.php new file mode 100644 index 000000000..2cca0688e --- /dev/null +++ b/src/RequirementChecker/RequirementCheckerFinder.php @@ -0,0 +1,47 @@ + + * 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\RequirementChecker; + +use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; +use Webmozart\Assert\Assert; + +final class RequirementCheckerFinder +{ + private const REQUIREMENT_CHECKER_PATH = __DIR__.'/../../res/requirement-checker'; + + /** + * @return iterable + */ + public function find(): iterable + { + Assert::directory( + self::REQUIREMENT_CHECKER_PATH, + 'Expected the requirement checker to have been dumped', + ); + + /** @var SplFileInfo[] $requirementCheckerFiles */ + $requirementCheckerFiles = Finder::create() + ->files() + ->in(self::REQUIREMENT_CHECKER_PATH); + + foreach ($requirementCheckerFiles as $file) { + yield [ + $file->getRelativePathname(), + $file->getContents(), + ]; + } + } +} diff --git a/src/RequirementChecker/RequirementsDumper.php b/src/RequirementChecker/RequirementsDumper.php index 5adf23ee1..016aa047a 100644 --- a/src/RequirementChecker/RequirementsDumper.php +++ b/src/RequirementChecker/RequirementsDumper.php @@ -14,7 +14,6 @@ namespace KevinGH\Box\RequirementChecker; -use Fidry\FileSystem\FS; use KevinGH\Box\Composer\Artifact\ComposerJson; use KevinGH\Box\Composer\Artifact\ComposerLock; use KevinGH\Box\Phar\CompressionAlgorithm; @@ -27,7 +26,7 @@ /** * @private */ -final class RequirementsDumper +final readonly class RequirementsDumper { private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP' */ - public static function dump( + public function dump( ?ComposerJson $composerJson, ?ComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, ): array { - Assert::directory(self::REQUIREMENT_CHECKER_PATH, 'Expected the requirement checker to have been dumped'); + Assert::directory( + self::REQUIREMENT_CHECKER_PATH, + 'Expected the requirement checker to have been dumped', + ); $filesWithContents = [ - self::dumpRequirementsConfig( + $this->dumpRequirementsConfig( $composerJson ?? new ComposerJson('', []), $composerLock ?? new ComposerLock('', []), $compressionAlgorithm, @@ -63,19 +70,19 @@ public static function dump( foreach ($requirementCheckerFiles as $file) { $filesWithContents[] = [ $file->getRelativePathname(), - FS::getFileContents($file->getPathname()), + $file->getContents(), ]; } return $filesWithContents; } - private static function dumpRequirementsConfig( + private function dumpRequirementsConfig( ComposerJson $composerJson, ComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, ): array { - $requirements = AppRequirementsFactory::create( + $requirements = $this->requirementsFactory->create( $composerJson, $composerLock, $compressionAlgorithm, diff --git a/tests/Benchmark/AppRequirementFactoryBench.php b/tests/Benchmark/AppRequirementFactoryBench.php index 45499a1ae..e84559ebf 100644 --- a/tests/Benchmark/AppRequirementFactoryBench.php +++ b/tests/Benchmark/AppRequirementFactoryBench.php @@ -28,6 +28,7 @@ { private const FIXTURES = __DIR__.'/../../fixtures/bench/requirement-checker'; + private AppRequirementsFactory $factory; private ComposerJson $composerJson; private ComposerLock $composerLock; @@ -35,6 +36,7 @@ public function setUp(): void { self::assertVendorsAreInstalled(); + $this->factory = new AppRequirementsFactory(); $this->composerJson = new ComposerJson( '', json_decode( @@ -55,7 +57,7 @@ public function setUp(): void #[BeforeMethods('setUp')] public function bench(): void { - AppRequirementsFactory::create( + $this->factory->create( $this->composerJson, $this->composerLock, CompressionAlgorithm::BZ2, diff --git a/tests/Console/Command/CompileTest.php b/tests/Console/Command/CompileTest.php index 23f0d095e..c603dbeef 100644 --- a/tests/Console/Command/CompileTest.php +++ b/tests/Console/Command/CompileTest.php @@ -27,6 +27,8 @@ use KevinGH\Box\Console\Application; use KevinGH\Box\Console\DisplayNormalizer as BoxDisplayNormalizer; use KevinGH\Box\Console\MessageRenderer; +use KevinGH\Box\RequirementChecker\AppRequirementsFactory; +use KevinGH\Box\RequirementChecker\RequirementsDumper; use KevinGH\Box\Test\FileSystemTestCase; use KevinGH\Box\Test\RequiresPharReadonlyOff; use KevinGH\Box\VarDumperNormalizer; @@ -181,6 +183,9 @@ protected function getCommand(): Command { return new Compile( (new Application())->getHeader(), + new RequirementsDumper( + new AppRequirementsFactory(), + ), ); } diff --git a/tests/RequirementChecker/AppRequirementsFactoryTest.php b/tests/RequirementChecker/AppRequirementsFactoryTest.php index a50b0c865..a47093811 100644 --- a/tests/RequirementChecker/AppRequirementsFactoryTest.php +++ b/tests/RequirementChecker/AppRequirementsFactoryTest.php @@ -49,7 +49,7 @@ public function test_it_can_generate_and_serialized_requirements_from_a_composer : json_decode($composerLockContents, true, flags: JSON_THROW_ON_ERROR), ); - $actual = AppRequirementsFactory::create( + $actual = (new AppRequirementsFactory())->create( $composerJson, $composerLock, $compressionAlgorithm, diff --git a/tests/RequirementChecker/RequirementsDumperTest.php b/tests/RequirementChecker/RequirementsDumperTest.php index 4da4a8caf..d99e30819 100644 --- a/tests/RequirementChecker/RequirementsDumperTest.php +++ b/tests/RequirementChecker/RequirementsDumperTest.php @@ -30,6 +30,15 @@ #[CoversClass(RequirementsDumper::class)] class RequirementsDumperTest extends TestCase { + private RequirementsDumper $dumper; + + protected function setUp(): void + { + $this->dumper = new RequirementsDumper( + new AppRequirementsFactory(), + ); + } + #[DataProvider('jsonAndLockContentsProvider')] public function test_it_dumps_the_requirement_checker_files( ComposerJson $composerJson, @@ -37,7 +46,7 @@ public function test_it_dumps_the_requirement_checker_files( CompressionAlgorithm $compressionAlgorithm, string $expectedRequirement, ): void { - $checkFiles = RequirementsDumper::dump( + $checkFiles = $this->dumper->dump( $composerJson, $composerLock, $compressionAlgorithm,