Skip to content

Commit

Permalink
Fix platform version resolution, use require first (#5555)
Browse files Browse the repository at this point in the history
* add fix for platofmr version

* fix test
  • Loading branch information
TomasVotruba authored Feb 4, 2024
1 parent 037153b commit 625bc7e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php80\NodeFactory\AttrGroupsFactory;
Expand All @@ -33,7 +34,6 @@
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public static function resolve(string $composerJson): ?int
$composerJsonContents = FileSystem::read($composerJson);
$projectComposerJson = Json::decode($composerJsonContents, Json::FORCE_ARRAY);

// give this one a priority, as more generic one
$requirePhpVersion = $projectComposerJson['require']['php'] ?? null;
if ($requirePhpVersion !== null) {
self::$cachedPhpVersions[$composerJson] = self::createIntVersionFromComposerVersion($requirePhpVersion);
return self::$cachedPhpVersions[$composerJson];
}

// see https://getcomposer.org/doc/06-config.md#platform
$platformPhp = $projectComposerJson['config']['platform']['php'] ?? null;
if ($platformPhp !== null) {
self::$cachedPhpVersions[$composerJson] = PhpVersionFactory::createIntVersion($platformPhp);
return self::$cachedPhpVersions[$composerJson];
}

$requirePhpVersion = $projectComposerJson['require']['php'] ?? null;
if ($requirePhpVersion === null) {
return self::$cachedPhpVersions[$composerJson] = null;
}

self::$cachedPhpVersions[$composerJson] = self::createIntVersionFromComposerVersion($requirePhpVersion);

return self::$cachedPhpVersions[$composerJson];
return null;
}

private static function createIntVersionFromComposerVersion(string $projectPhpVersion): int
Expand Down
6 changes: 1 addition & 5 deletions src/Util/PhpVersionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ public static function createIntVersion(string $version): int
$explodeVersion = explode('.', $version);
$countExplodedVersion = count($explodeVersion);

if ($countExplodedVersion === 2) {
if ($countExplodedVersion >= 2) {
return (int) $explodeVersion[0] * 10000 + (int) $explodeVersion[1] * 100;
}

if ($countExplodedVersion >= 3) {
return (int) $explodeVersion[0] * 10000 + (int) $explodeVersion[1] * 100 + (int) $explodeVersion[2];
}

return (int) $version;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"require": {
"php": ">=7.3"
"php": ">=7.4"
},
"config": {
"platform": {
"php": "7.4"
"php": "7.3"
}
}
}

0 comments on commit 625bc7e

Please sign in to comment.