Skip to content

Commit

Permalink
fix: Use information from the composer.lock when possible for the req…
Browse files Browse the repository at this point in the history
…uirement checker (#868)

As explained in #867, the `composer.json` provides only partial and incomplete information which is why it is preferable to use the `composer.lock` file instead.
  • Loading branch information
theofidry committed Feb 11, 2023
1 parent 1ee7d39 commit 155237c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private static function retrievePhpVersionRequirements(
DecodedComposerJson $composerJson,
DecodedComposerLock $composerLock,
): array {
// If the application config is set, it is the authority.
return $composerLock->isEmpty() && $composerJson->hasRequiredPhpVersion() || $composerLock->hasRequiredPhpVersion()
// If the application has a constraint on the PHP version, it is the authority.
return $composerLock->hasRequiredPhpVersion() || $composerJson->hasRequiredPhpVersion()
? self::retrievePHPRequirementFromPlatform($composerJson, $composerLock)
: self::retrievePHPRequirementFromPackages($composerLock);
}
Expand All @@ -62,9 +62,7 @@ private static function retrievePHPRequirementFromPlatform(
DecodedComposerJson $composerJson,
DecodedComposerLock $composerLock,
): array {
$requiredPhpVersion = $composerLock->isEmpty()
? $composerJson->getRequiredPhpVersion()
: $composerLock->getRequiredPhpVersion();
$requiredPhpVersion = $composerLock->getRequiredPhpVersion() ?? $composerJson->getRequiredPhpVersion();

return null === $requiredPhpVersion ? [] : [Requirement::forPHP($requiredPhpVersion, null)];
}
Expand Down Expand Up @@ -148,7 +146,6 @@ private static function collectExtensionRequirements(
}

/**
* TODO: review: I am not sure this makes sense since there is no info about the packages used.
* @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>}
Expand Down
124 changes: 122 additions & 2 deletions tests/RequirementChecker/AppRequirementsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,127 @@ public static function lockContentsProvider(): iterable
],
];

yield 'The application defines a PHP requirement (takes it from the composer.lock)' => [
<<<'JSON'
{
"require": {
"php": ">=5.3"
},
"require-dev": []
}
JSON,
<<<'JSON'
{
"platform": {
"php": ">=5.4"
},
"packages": [
{
"name": "beberlei/assert",
"version": "v2.9.2",
"require": {
"php": ">=5.3"
},
"require-dev": []
},
{
"name": "composer/ca-bundle",
"version": "1.1.0",
"require": {},
"require-dev": {
"ext-pdo_sqlite3": "*"
}
}
]
}
JSON,
CompressionAlgorithm::NONE,
[
Requirement::forPHP('>=5.4', null),
],
];

yield 'The application defines a PHP requirement (takes it from the composer.json if the .lock does not have it)' => [
<<<'JSON'
{
"require": {
"php": ">=5.3"
},
"require-dev": []
}
JSON,
<<<'JSON'
{
"packages": [
{
"name": "beberlei/assert",
"version": "v2.9.2",
"require": {
"php": ">=5.3"
},
"require-dev": []
},
{
"name": "composer/ca-bundle",
"version": "1.1.0",
"require": {},
"require-dev": {
"ext-pdo_sqlite3": "*"
}
}
]
}
JSON,
CompressionAlgorithm::NONE,
[
Requirement::forPHP('>=5.3', null),
],
];

yield 'The application does not define a PHP requirement (takes it from packages)' => [
<<<'JSON'
{
"require": {},
"require-dev": []
}
JSON,
<<<'JSON'
{
"packages": [
{
"name": "beberlei/assert",
"version": "v2.9.2",
"require": {
"php": ">=5.3"
},
"require-dev": []
},
{
"name": "acme/foo",
"version": "2.0.0",
"require": {},
"require-dev": {}
},
{
"name": "composer/ca-bundle",
"version": "1.1.0",
"require": {
"php": "^7.1"
},
"require-dev": {
"ext-pdo_sqlite3": "*"
}
}
]
}
JSON,
CompressionAlgorithm::NONE,
[
Requirement::forPHP('>=5.3', 'beberlei/assert'),
Requirement::forPHP('^7.1', 'composer/ca-bundle'),
],
];

yield 'json & lock file packages requirements' => [
<<<'JSON'
{
Expand Down Expand Up @@ -486,8 +607,7 @@ public static function lockContentsProvider(): iterable
JSON,
CompressionAlgorithm::NONE,
[
Requirement::forPHP('>=5.3', 'beberlei/assert'),
Requirement::forPHP('^5.3.2 || ^7.0', 'acme/foo'),
Requirement::forPHP('>=5.3', null),
Requirement::forExtension('mbstring', 'beberlei/assert'),
Requirement::forExtension('openssl', 'composer/ca-bundle'),
Requirement::forExtension('openssl', 'acme/foo'),
Expand Down

0 comments on commit 155237c

Please sign in to comment.