diff --git a/requirement-checker/src/Checker.php b/requirement-checker/src/Checker.php index e70c49adf..038fcea27 100644 --- a/requirement-checker/src/Checker.php +++ b/requirement-checker/src/Checker.php @@ -14,6 +14,7 @@ namespace KevinGH\RequirementChecker; +use InvalidArgumentException; use function count; /** @@ -137,9 +138,10 @@ private static function retrieveRequirements(): RequirementCollection foreach ($config as $constraint) { $requirements->addRequirement( - 'php' === $constraint['type'] - ? new IsPhpVersionFulfilled($constraint['condition']) - : new IsExtensionFulfilled($constraint['condition']), + self::createCondition( + $constraint['type'], + $constraint['condition'] + ), $constraint['message'], $constraint['helpMessage'] ); @@ -147,4 +149,27 @@ private static function retrieveRequirements(): RequirementCollection return $requirements; } + + /** + * @param string $type + * @param string $condition + */ + private static function createCondition($type, $condition): IsFulfilled + { + switch ($type) { + case 'php': + return new IsPhpVersionFulfilled($condition); + + case 'extension': + return new IsExtensionFulfilled($condition); + + default: + throw new InvalidArgumentException( + sprintf( + 'Unknown requirement type "%s".', + $type, + ), + ); + } + } } diff --git a/res/requirement-checker/src/Checker.php b/res/requirement-checker/src/Checker.php index c6a70f52c..a8c2914c3 100644 --- a/res/requirement-checker/src/Checker.php +++ b/res/requirement-checker/src/Checker.php @@ -3,6 +3,7 @@ declare (strict_types=1); namespace HumbugBox420\KevinGH\RequirementChecker; +use InvalidArgumentException; use function count; final class Checker { @@ -80,8 +81,19 @@ private static function retrieveRequirements() : RequirementCollection $config = (require self::$requirementsConfig); $requirements = new RequirementCollection(); foreach ($config as $constraint) { - $requirements->addRequirement('php' === $constraint['type'] ? new IsPhpVersionFulfilled($constraint['condition']) : new IsExtensionFulfilled($constraint['condition']), $constraint['message'], $constraint['helpMessage']); + $requirements->addRequirement(self::createCondition($constraint['type'], $constraint['condition']), $constraint['message'], $constraint['helpMessage']); } return $requirements; } + private static function createCondition($type, $condition) : IsFulfilled + { + switch ($type) { + case 'php': + return new IsPhpVersionFulfilled($condition); + case 'extension': + return new IsExtensionFulfilled($condition); + default: + throw new InvalidArgumentException(\sprintf('Unknown requirement type "%s".', $type)); + } + } } diff --git a/src/RequirementChecker/AppRequirementsFactory.php b/src/RequirementChecker/AppRequirementsFactory.php index 88e1a3934..743674cc5 100644 --- a/src/RequirementChecker/AppRequirementsFactory.php +++ b/src/RequirementChecker/AppRequirementsFactory.php @@ -30,7 +30,7 @@ final class AppRequirementsFactory private const SELF_PACKAGE = '__APPLICATION__'; /** - * @return list Serialized configured requirements + * @return list Configured requirements */ public static function create( DecodedComposerJson $composerJson, @@ -103,7 +103,7 @@ private static function configureExtensionRequirements( foreach ($extensionRequirements as $extension => $packages) { foreach ($packages as $package) { - $requirements[] = Requirement::forExtension( + $requirements[] = Requirement::forRequiredExtension( $extension, self::SELF_PACKAGE === $package ? null : $package, ); @@ -117,7 +117,7 @@ private static function configureExtensionRequirements( * Collects the extension required. It also accounts for the polyfills, i.e. if the polyfill * `symfony/polyfill-mbstring` is provided then the extension `ext-mbstring` will not be required. * - * @return array{array, array} Associative array containing the list of extensions required + * @return array> Associative array containing the list of extensions required */ private static function collectExtensionRequirements( DecodedComposerJson $composerJson, @@ -148,7 +148,7 @@ private static function collectExtensionRequirements( /** * @param array> $requirements The key is the extension name and the value the list of sources (app literal string or the package name). * - * @return array{array, array} + * @return array{array, array>} */ private static function collectComposerJsonExtensionRequirements( DecodedComposerJson $composerJson, @@ -176,7 +176,7 @@ private static function collectComposerJsonExtensionRequirements( /** * @param array> $requirements The key is the extension name and the value the list of sources (app literal string or the package name). * - * @return array{array, array} + * @return array{array, array>} */ private static function collectComposerLockExtensionRequirements( DecodedComposerLock $composerLock, diff --git a/src/RequirementChecker/Requirement.php b/src/RequirementChecker/Requirement.php index 601c9ac4a..e57c1c33b 100644 --- a/src/RequirementChecker/Requirement.php +++ b/src/RequirementChecker/Requirement.php @@ -55,7 +55,7 @@ public static function forPHP(string $requiredPhpVersion, ?string $packageName): ); } - public static function forExtension(string $extension, ?string $packageName): self + public static function forRequiredExtension(string $extension, ?string $packageName): self { // TODO: review the message & help message return new self( diff --git a/tests/RequirementChecker/AppRequirementsFactoryTest.php b/tests/RequirementChecker/AppRequirementsFactoryTest.php index 555aaac71..b2c32e025 100644 --- a/tests/RequirementChecker/AppRequirementsFactoryTest.php +++ b/tests/RequirementChecker/AppRequirementsFactoryTest.php @@ -72,7 +72,7 @@ public static function lockContentsProvider(): iterable null, CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -81,7 +81,7 @@ public static function lockContentsProvider(): iterable null, CompressionAlgorithm::BZ2, [ - Requirement::forExtension('bz2', null), + Requirement::forRequiredExtension('bz2', null), ], ]; @@ -90,7 +90,7 @@ public static function lockContentsProvider(): iterable '{}', CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -99,7 +99,7 @@ public static function lockContentsProvider(): iterable '{}', CompressionAlgorithm::BZ2, [ - Requirement::forExtension('bz2', null), + Requirement::forRequiredExtension('bz2', null), ], ]; @@ -108,7 +108,7 @@ public static function lockContentsProvider(): iterable '{}', CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -117,7 +117,7 @@ public static function lockContentsProvider(): iterable '{}', CompressionAlgorithm::BZ2, [ - Requirement::forExtension('bz2', null), + Requirement::forRequiredExtension('bz2', null), ], ]; @@ -136,7 +136,7 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -154,7 +154,7 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -181,7 +181,7 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -199,8 +199,8 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::GZ, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('zlib', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('zlib', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -218,8 +218,8 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::GZ, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('zlib', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('zlib', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -245,8 +245,8 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::GZ, [ Requirement::forPHP('^7.1', null), - Requirement::forExtension('zlib', null), - Requirement::forExtension('phar', null), + Requirement::forRequiredExtension('zlib', null), + Requirement::forRequiredExtension('phar', null), ], ]; @@ -316,7 +316,7 @@ public static function lockContentsProvider(): iterable null, CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -333,7 +333,7 @@ public static function lockContentsProvider(): iterable JSON, CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -358,7 +358,7 @@ public static function lockContentsProvider(): iterable JSON, CompressionAlgorithm::GZ, [ - Requirement::forExtension('zlib', null), + Requirement::forRequiredExtension('zlib', null), ], ]; @@ -379,10 +379,10 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('>=5.3', null), - Requirement::forExtension('mbstring', null), - Requirement::forExtension('openssl', null), - Requirement::forExtension('pcre', null), - Requirement::forExtension('pdo_sqlite3', null), + Requirement::forRequiredExtension('mbstring', null), + Requirement::forRequiredExtension('openssl', null), + Requirement::forRequiredExtension('pcre', null), + Requirement::forRequiredExtension('pdo_sqlite3', null), ], ]; @@ -428,10 +428,10 @@ public static function lockContentsProvider(): iterable [ Requirement::forPHP('>=5.3', 'beberlei/assert'), Requirement::forPHP('^5.3.2 || ^7.0', 'composer/ca-bundle'), - Requirement::forExtension('mbstring', 'beberlei/assert'), - Requirement::forExtension('openssl', 'composer/ca-bundle'), - Requirement::forExtension('openssl', 'acme/foo'), - Requirement::forExtension('pcre', 'composer/ca-bundle'), + Requirement::forRequiredExtension('mbstring', 'beberlei/assert'), + Requirement::forRequiredExtension('openssl', 'composer/ca-bundle'), + Requirement::forRequiredExtension('openssl', 'acme/foo'), + Requirement::forRequiredExtension('pcre', 'composer/ca-bundle'), ], ]; @@ -608,10 +608,10 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('>=5.3', null), - Requirement::forExtension('mbstring', 'beberlei/assert'), - Requirement::forExtension('openssl', 'composer/ca-bundle'), - Requirement::forExtension('openssl', 'acme/foo'), - Requirement::forExtension('pcre', 'composer/ca-bundle'), + Requirement::forRequiredExtension('mbstring', 'beberlei/assert'), + Requirement::forRequiredExtension('openssl', 'composer/ca-bundle'), + Requirement::forRequiredExtension('openssl', 'acme/foo'), + Requirement::forRequiredExtension('pcre', 'composer/ca-bundle'), ], ]; @@ -746,12 +746,12 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.3', null), - Requirement::forExtension('mbstring', null), - Requirement::forExtension('mbstring', 'beberlei/assert'), - Requirement::forExtension('mbstring', 'composer/ca-bundle'), - Requirement::forExtension('json', null), - Requirement::forExtension('json', 'beberlei/assert'), - Requirement::forExtension('json', 'composer/ca-bundle'), + Requirement::forRequiredExtension('mbstring', null), + Requirement::forRequiredExtension('mbstring', 'beberlei/assert'), + Requirement::forRequiredExtension('mbstring', 'composer/ca-bundle'), + Requirement::forRequiredExtension('json', null), + Requirement::forRequiredExtension('json', 'beberlei/assert'), + Requirement::forRequiredExtension('json', 'composer/ca-bundle'), ], ]; @@ -773,7 +773,7 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.3', null), - Requirement::forExtension('json', null), + Requirement::forRequiredExtension('json', null), ], ]; @@ -833,9 +833,9 @@ public static function lockContentsProvider(): iterable CompressionAlgorithm::NONE, [ Requirement::forPHP('^7.3', null), - Requirement::forExtension('json', null), - Requirement::forExtension('json', 'beberlei/assert'), - Requirement::forExtension('json', 'composer/ca-bundle'), + Requirement::forRequiredExtension('json', null), + Requirement::forRequiredExtension('json', 'beberlei/assert'), + Requirement::forRequiredExtension('json', 'composer/ca-bundle'), ], ]; diff --git a/tests/RequirementChecker/DecodedComposerJsonTest.php b/tests/RequirementChecker/DecodedComposerJsonTest.php index d85dc2f5d..ac8dee87d 100644 --- a/tests/RequirementChecker/DecodedComposerJsonTest.php +++ b/tests/RequirementChecker/DecodedComposerJsonTest.php @@ -105,7 +105,7 @@ public static function composerJsonProvider(): iterable ], ]; - yield 'packages and extensions required' => [ + yield 'nominal' => [ <<<'JSON' { "require": { diff --git a/tests/RequirementChecker/RequirementTest.php b/tests/RequirementChecker/RequirementTest.php index bbf2f9f84..011327dde 100644 --- a/tests/RequirementChecker/RequirementTest.php +++ b/tests/RequirementChecker/RequirementTest.php @@ -53,7 +53,7 @@ public function test_it_can_be_created_for_a_php_version_for_a_package(): void public function test_it_can_be_created_for_an_extension_constraint(): void { - $requirement = Requirement::forExtension('mbstring', null); + $requirement = Requirement::forRequiredExtension('mbstring', null); $expected = [ 'type' => 'extension', @@ -67,7 +67,7 @@ public function test_it_can_be_created_for_an_extension_constraint(): void public function test_it_can_be_created_for_an_extension_constraint_for_a_package(): void { - $requirement = Requirement::forExtension('mbstring', 'box/test'); + $requirement = Requirement::forRequiredExtension('mbstring', 'box/test'); $expected = [ 'type' => 'extension',