Skip to content

Commit

Permalink
[Performance] Cache PHP version features on reading composer.json on …
Browse files Browse the repository at this point in the history
…PhpVersionProvider (#5484)
  • Loading branch information
samsonasik committed Jan 21, 2024
1 parent 663f526 commit 3dac3a0
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Php/PhpVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
/**
* @see \Rector\Tests\Php\PhpVersionProviderTest
*/
final readonly class PhpVersionProvider
final class PhpVersionProvider
{
/**
* @var string
* @see https://regex101.com/r/qBMnbl/1
*/
private const VALID_PHP_VERSION_REGEX = '#^\d{5,6}$#';

private int|null $phpVersionFeatures = null;

public function __construct(
private ProjectComposerJsonPhpVersionResolver $projectComposerJsonPhpVersionResolver
private readonly ProjectComposerJsonPhpVersionResolver $projectComposerJsonPhpVersionResolver
) {
}

Expand All @@ -34,15 +36,13 @@ public function __construct(
*/
public function provide(): int
{
$phpVersionFeatures = null;

if (SimpleParameterProvider::hasParameter(Option::PHP_VERSION_FEATURES)) {
$phpVersionFeatures = SimpleParameterProvider::provideIntParameter(Option::PHP_VERSION_FEATURES);
$this->validatePhpVersionFeaturesParameter($phpVersionFeatures);
$this->phpVersionFeatures = SimpleParameterProvider::provideIntParameter(Option::PHP_VERSION_FEATURES);
$this->validatePhpVersionFeaturesParameter($this->phpVersionFeatures);
}

if ($phpVersionFeatures > 0) {
return $phpVersionFeatures;
if ($this->phpVersionFeatures > 0) {
return $this->phpVersionFeatures;
}

// for tests
Expand All @@ -55,12 +55,12 @@ public function provide(): int
if (file_exists($projectComposerJson)) {
$phpVersion = $this->projectComposerJsonPhpVersionResolver->resolve($projectComposerJson);
if ($phpVersion !== null) {
return $phpVersion;
return $this->phpVersionFeatures = $phpVersion;
}
}

// fallback to current PHP runtime version
return PHP_VERSION_ID;
return $this->phpVersionFeatures = PHP_VERSION_ID;
}

public function isAtLeastPhpVersion(int $phpVersion): bool
Expand Down

0 comments on commit 3dac3a0

Please sign in to comment.