Skip to content

Commit

Permalink
refactor: Refactor elements of the requirement checker (#875)
Browse files Browse the repository at this point in the history
Minor refactoring to prepare for #872.
  • Loading branch information
theofidry authored Feb 12, 2023
1 parent 4992467 commit e76d896
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 53 deletions.
31 changes: 28 additions & 3 deletions requirement-checker/src/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace KevinGH\RequirementChecker;

use InvalidArgumentException;
use function count;

/**
Expand Down Expand Up @@ -137,14 +138,38 @@ 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']
);
}

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,
),
);
}
}
}
14 changes: 13 additions & 1 deletion res/requirement-checker/src/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare (strict_types=1);
namespace HumbugBox420\KevinGH\RequirementChecker;

use InvalidArgumentException;
use function count;
final class Checker
{
Expand Down Expand Up @@ -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));
}
}
}
10 changes: 5 additions & 5 deletions src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class AppRequirementsFactory
private const SELF_PACKAGE = '__APPLICATION__';

/**
* @return list<Requirement> Serialized configured requirements
* @return list<Requirement> Configured requirements
*/
public static function create(
DecodedComposerJson $composerJson,
Expand Down Expand Up @@ -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,
);
Expand All @@ -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<string, true>, array<string, string>} Associative array containing the list of extensions required
* @return array<string, list<string>> Associative array containing the list of extensions required
*/
private static function collectExtensionRequirements(
DecodedComposerJson $composerJson,
Expand Down Expand Up @@ -148,7 +148,7 @@ private static function collectExtensionRequirements(
/**
* @param array<string, list<string>> $requirements The key is the extension name and the value the list of sources (app literal string or the package name).
*
* @return array{array<string, true>, array<string, string>}
* @return array{array<string, true>, array<string, list<string>>}
*/
private static function collectComposerJsonExtensionRequirements(
DecodedComposerJson $composerJson,
Expand Down Expand Up @@ -176,7 +176,7 @@ private static function collectComposerJsonExtensionRequirements(
/**
* @param array<string, list<string>> $requirements The key is the extension name and the value the list of sources (app literal string or the package name).
*
* @return array{array<string, true>, array<string, string>}
* @return array{array<string, true>, array<string, list<string>>}
*/
private static function collectComposerLockExtensionRequirements(
DecodedComposerLock $composerLock,
Expand Down
2 changes: 1 addition & 1 deletion src/RequirementChecker/Requirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
80 changes: 40 additions & 40 deletions tests/RequirementChecker/AppRequirementsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function lockContentsProvider(): iterable
null,
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -81,7 +81,7 @@ public static function lockContentsProvider(): iterable
null,
CompressionAlgorithm::BZ2,
[
Requirement::forExtension('bz2', null),
Requirement::forRequiredExtension('bz2', null),
],
];

Expand All @@ -90,7 +90,7 @@ public static function lockContentsProvider(): iterable
'{}',
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -99,7 +99,7 @@ public static function lockContentsProvider(): iterable
'{}',
CompressionAlgorithm::BZ2,
[
Requirement::forExtension('bz2', null),
Requirement::forRequiredExtension('bz2', null),
],
];

Expand All @@ -108,7 +108,7 @@ public static function lockContentsProvider(): iterable
'{}',
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -117,7 +117,7 @@ public static function lockContentsProvider(): iterable
'{}',
CompressionAlgorithm::BZ2,
[
Requirement::forExtension('bz2', null),
Requirement::forRequiredExtension('bz2', null),
],
];

Expand All @@ -136,7 +136,7 @@ public static function lockContentsProvider(): iterable
CompressionAlgorithm::NONE,
[
Requirement::forPHP('^7.1', null),
Requirement::forExtension('phar', null),
Requirement::forRequiredExtension('phar', null),
],
];

Expand All @@ -154,7 +154,7 @@ public static function lockContentsProvider(): iterable
CompressionAlgorithm::NONE,
[
Requirement::forPHP('^7.1', null),
Requirement::forExtension('phar', null),
Requirement::forRequiredExtension('phar', null),
],
];

Expand All @@ -181,7 +181,7 @@ public static function lockContentsProvider(): iterable
CompressionAlgorithm::NONE,
[
Requirement::forPHP('^7.1', null),
Requirement::forExtension('phar', null),
Requirement::forRequiredExtension('phar', null),
],
];

Expand All @@ -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),
],
];

Expand All @@ -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),
],
];

Expand All @@ -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),
],
];

Expand Down Expand Up @@ -316,7 +316,7 @@ public static function lockContentsProvider(): iterable
null,
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -333,7 +333,7 @@ public static function lockContentsProvider(): iterable
JSON,
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -358,7 +358,7 @@ public static function lockContentsProvider(): iterable
JSON,
CompressionAlgorithm::GZ,
[
Requirement::forExtension('zlib', null),
Requirement::forRequiredExtension('zlib', null),
],
];

Expand All @@ -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),
],
];

Expand Down Expand Up @@ -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'),
],
];

Expand Down Expand Up @@ -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'),
],
];

Expand Down Expand Up @@ -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'),
],
];

Expand All @@ -773,7 +773,7 @@ public static function lockContentsProvider(): iterable
CompressionAlgorithm::NONE,
[
Requirement::forPHP('^7.3', null),
Requirement::forExtension('json', null),
Requirement::forRequiredExtension('json', null),
],
];

Expand Down Expand Up @@ -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'),
],
];

Expand Down
2 changes: 1 addition & 1 deletion tests/RequirementChecker/DecodedComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function composerJsonProvider(): iterable
],
];

yield 'packages and extensions required' => [
yield 'nominal' => [
<<<'JSON'
{
"require": {
Expand Down
4 changes: 2 additions & 2 deletions tests/RequirementChecker/RequirementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit e76d896

Please sign in to comment.