Skip to content

Commit

Permalink
Updated Rector to commit 171fddb1a1e9c54e194130ffe039809721d1f465
Browse files Browse the repository at this point in the history
rectorphp/rector-src@171fddb add withPhpSets() methods (#5519)
  • Loading branch information
TomasVotruba committed Jan 29, 2024
1 parent bc9920f commit 3c3170a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,10 @@
*/
final class VersionCompareFuncCallToConstantRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Util\PhpVersionFactory
*/
private $phpVersionFactory;
/**
* @var array<string, class-string<BinaryOp>>
*/
private const OPERATOR_TO_COMPARISON = ['=' => Identical::class, '==' => Identical::class, 'eq' => Identical::class, '!=' => NotIdentical::class, '<>' => NotIdentical::class, 'ne' => NotIdentical::class, '>' => Greater::class, 'gt' => Greater::class, '<' => Smaller::class, 'lt' => Smaller::class, '>=' => GreaterOrEqual::class, 'ge' => GreaterOrEqual::class, '<=' => SmallerOrEqual::class, 'le' => SmallerOrEqual::class];
public function __construct(PhpVersionFactory $phpVersionFactory)
{
$this->phpVersionFactory = $phpVersionFactory;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes use of call to version compare function to use of PHP version constant', [new CodeSample(<<<'CODE_SAMPLE'
Expand Down Expand Up @@ -121,7 +112,7 @@ private function getVersionNumberFormVersionString(Expr $expr) : ?LNumber
if (!$expr instanceof String_) {
return null;
}
$value = $this->phpVersionFactory->createIntVersion($expr->value);
$value = PhpVersionFactory::createIntVersion($expr->value);
return new LNumber($value);
}
}
10 changes: 2 additions & 8 deletions rules/DeadCode/ConditionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ final class ConditionResolver
* @var \Rector\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
/**
* @readonly
* @var \Rector\Util\PhpVersionFactory
*/
private $phpVersionFactory;
public function __construct(NodeNameResolver $nodeNameResolver, PhpVersionProvider $phpVersionProvider, ValueResolver $valueResolver, PhpVersionFactory $phpVersionFactory)
public function __construct(NodeNameResolver $nodeNameResolver, PhpVersionProvider $phpVersionProvider, ValueResolver $valueResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->phpVersionProvider = $phpVersionProvider;
$this->valueResolver = $valueResolver;
$this->phpVersionFactory = $phpVersionFactory;
}
public function resolveFromExpr(Expr $expr) : ?ConditionInterface
{
Expand Down Expand Up @@ -121,7 +115,7 @@ private function resolveArgumentValue(FuncCall $funcCall, int $argumentPosition)
return $this->phpVersionProvider->provide();
}
if (\is_string($version)) {
return $this->phpVersionFactory->createIntVersion($version);
return PhpVersionFactory::createIntVersion($version);
}
return $version;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'ca23ae894e833861e83533ed9f89fc7b3bf37fee';
public const PACKAGE_VERSION = '171fddb1a1e9c54e194130ffe039809721d1f465';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-01-30 00:31:13';
public const RELEASE_DATE = '2024-01-29 23:50:30';
/**
* @var int
*/
Expand Down
44 changes: 44 additions & 0 deletions src/Configuration/PhpLevelSetResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare (strict_types=1);
namespace Rector\Configuration;

use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Set\ValueObject\LevelSetList;
use Rector\ValueObject\PhpVersion;
final class PhpLevelSetResolver
{
public static function resolveFromPhpVersion(int $phpVersion) : string
{
switch ($phpVersion) {
case PhpVersion::PHP_53:
return LevelSetList::UP_TO_PHP_53;
case PhpVersion::PHP_54:
return LevelSetList::UP_TO_PHP_54;
case PhpVersion::PHP_55:
return LevelSetList::UP_TO_PHP_55;
case PhpVersion::PHP_56:
return LevelSetList::UP_TO_PHP_56;
case PhpVersion::PHP_70:
return LevelSetList::UP_TO_PHP_70;
case PhpVersion::PHP_71:
return LevelSetList::UP_TO_PHP_71;
case PhpVersion::PHP_72:
return LevelSetList::UP_TO_PHP_72;
case PhpVersion::PHP_73:
return LevelSetList::UP_TO_PHP_73;
case PhpVersion::PHP_74:
return LevelSetList::UP_TO_PHP_74;
case PhpVersion::PHP_80:
return LevelSetList::UP_TO_PHP_80;
case PhpVersion::PHP_81:
return LevelSetList::UP_TO_PHP_81;
case PhpVersion::PHP_82:
return LevelSetList::UP_TO_PHP_82;
case PhpVersion::PHP_83:
return LevelSetList::UP_TO_PHP_83;
default:
throw new InvalidConfigurationException(\sprintf('Could not resolve PHP level set list for "%s"', $phpVersion));
}
}
}
53 changes: 53 additions & 0 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Php\PhpVersionResolver\ProjectComposerJsonPhpVersionResolver;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\FOSRestSetList;
use Rector\Symfony\Set\JMSSetList;
Expand Down Expand Up @@ -246,6 +249,56 @@ public function withAttributesSets(bool $symfony = \false, bool $doctrine = \fal
}
return $this;
}
/**
* What PHP sets should be applied? By default the same version
* as composer.json has is used
*/
public function withPhpSets(bool $php83 = \false, bool $php82 = \false, bool $php81 = \false, bool $php80 = \false, bool $php74 = \false, bool $php73 = \false, bool $php72 = \false, bool $php71 = \false, bool $php70 = \false, bool $php56 = \false, bool $php55 = \false, bool $php54 = \false, bool $php53 = \false) : self
{
$pickedArguments = \array_filter(\func_get_args());
if (\count($pickedArguments) > 1) {
throw new InvalidConfigurationException(\sprintf('Pick only one version target in "withPhpSets()". All rules up to this version will be used.%sTo use your composer.json PHP version, keep arguments empty.', \PHP_EOL));
}
if ($pickedArguments === []) {
// use composer.json PHP version
$projectComposerJsonFilePath = \getcwd() . '/composer.json';
if (\file_exists($projectComposerJsonFilePath)) {
$projectPhpVersion = ProjectComposerJsonPhpVersionResolver::resolve($projectComposerJsonFilePath);
if (\is_int($projectPhpVersion)) {
$this->sets[] = \Rector\Configuration\PhpLevelSetResolver::resolveFromPhpVersion($projectPhpVersion);
return $this;
}
}
throw new InvalidConfigurationException(\sprintf('We could not find local "composer.json" to determine your PHP version.%sPlease, fill the PHP version set in withPhpSets() manually.', \PHP_EOL));
} elseif ($php53) {
$this->sets[] = LevelSetList::UP_TO_PHP_53;
} elseif ($php54) {
$this->sets[] = LevelSetList::UP_TO_PHP_54;
} elseif ($php55) {
$this->sets[] = LevelSetList::UP_TO_PHP_55;
} elseif ($php56) {
$this->sets[] = LevelSetList::UP_TO_PHP_56;
} elseif ($php70) {
$this->sets[] = LevelSetList::UP_TO_PHP_70;
} elseif ($php71) {
$this->sets[] = LevelSetList::UP_TO_PHP_71;
} elseif ($php72) {
$this->sets[] = LevelSetList::UP_TO_PHP_72;
} elseif ($php73) {
$this->sets[] = LevelSetList::UP_TO_PHP_73;
} elseif ($php74) {
$this->sets[] = LevelSetList::UP_TO_PHP_74;
} elseif ($php80) {
$this->sets[] = LevelSetList::UP_TO_PHP_80;
} elseif ($php81) {
$this->sets[] = LevelSetList::UP_TO_PHP_81;
} elseif ($php82) {
$this->sets[] = LevelSetList::UP_TO_PHP_82;
} elseif ($php83) {
$this->sets[] = LevelSetList::UP_TO_PHP_83;
}
return $this;
}
public function withPreparedSets(bool $deadCode = \false, bool $codeQuality = \false, bool $codingStyle = \false, bool $typeDeclarations = \false, bool $privatization = \false, bool $naming = \false, bool $instanceOf = \false, bool $earlyReturn = \false, bool $strictBooleans = \false) : self
{
if ($deadCode) {
Expand Down
11 changes: 1 addition & 10 deletions src/Php/PhpVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
final class PhpVersionProvider
{
/**
* @readonly
* @var \Rector\Php\PhpVersionResolver\ProjectComposerJsonPhpVersionResolver
*/
private $projectComposerJsonPhpVersionResolver;
/**
* @var string
* @see https://regex101.com/r/qBMnbl/1
Expand All @@ -30,10 +25,6 @@ final class PhpVersionProvider
* @var int|null
*/
private $phpVersionFeatures = null;
public function __construct(ProjectComposerJsonPhpVersionResolver $projectComposerJsonPhpVersionResolver)
{
$this->projectComposerJsonPhpVersionResolver = $projectComposerJsonPhpVersionResolver;
}
/**
* @return PhpVersion::*
*/
Expand All @@ -53,7 +44,7 @@ public function provide() : int
}
$projectComposerJson = \getcwd() . '/composer.json';
if (\file_exists($projectComposerJson)) {
$phpVersion = $this->projectComposerJsonPhpVersionResolver->resolve($projectComposerJson);
$phpVersion = ProjectComposerJsonPhpVersionResolver::resolve($projectComposerJson);
if ($phpVersion !== null) {
return $this->phpVersionFeatures = $phpVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,36 @@
*/
final class ProjectComposerJsonPhpVersionResolver
{
/**
* @readonly
* @var \Composer\Semver\VersionParser
*/
private $versionParser;
/**
* @readonly
* @var \Rector\Util\PhpVersionFactory
*/
private $phpVersionFactory;
/**
* @var array<string, int>
*/
private $cachedPhpVersions = [];
public function __construct(VersionParser $versionParser, PhpVersionFactory $phpVersionFactory)
{
$this->versionParser = $versionParser;
$this->phpVersionFactory = $phpVersionFactory;
}
public function resolve(string $composerJson) : ?int
private static $cachedPhpVersions = [];
public static function resolve(string $composerJson) : ?int
{
if (isset($this->cachedPhpVersions[$composerJson])) {
return $this->cachedPhpVersions[$composerJson];
if (isset(self::$cachedPhpVersions[$composerJson])) {
return self::$cachedPhpVersions[$composerJson];
}
$composerJsonContents = FileSystem::read($composerJson);
$projectComposerJson = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
// see https://getcomposer.org/doc/06-config.md#platform
$platformPhp = $projectComposerJson['config']['platform']['php'] ?? null;
if ($platformPhp !== null) {
$this->cachedPhpVersions[$composerJson] = $this->phpVersionFactory->createIntVersion($platformPhp);
return $this->cachedPhpVersions[$composerJson];
self::$cachedPhpVersions[$composerJson] = PhpVersionFactory::createIntVersion($platformPhp);
return self::$cachedPhpVersions[$composerJson];
}
$requirePhpVersion = $projectComposerJson['require']['php'] ?? null;
if ($requirePhpVersion === null) {
return null;
}
$this->cachedPhpVersions[$composerJson] = $this->createIntVersionFromComposerVersion($requirePhpVersion);
return $this->cachedPhpVersions[$composerJson];
self::$cachedPhpVersions[$composerJson] = self::createIntVersionFromComposerVersion($requirePhpVersion);
return self::$cachedPhpVersions[$composerJson];
}
private function createIntVersionFromComposerVersion(string $projectPhpVersion) : int
private static function createIntVersionFromComposerVersion(string $projectPhpVersion) : int
{
$constraint = $this->versionParser->parseConstraints($projectPhpVersion);
$versionParser = new VersionParser();
$constraint = $versionParser->parseConstraints($projectPhpVersion);
$lowerBound = $constraint->getLowerBound();
$lowerBoundVersion = $lowerBound->getVersion();
return $this->phpVersionFactory->createIntVersion($lowerBoundVersion);
return PhpVersionFactory::createIntVersion($lowerBoundVersion);
}
}
2 changes: 1 addition & 1 deletion src/Util/PhpVersionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

final class PhpVersionFactory
{
public function createIntVersion(string $version) : int
public static function createIntVersion(string $version) : int
{
$explodeDash = \explode('-', $version);
if (\count($explodeDash) > 1) {
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@
'Rector\\Configuration\\ConfigurationFactory' => $baseDir . '/src/Configuration/ConfigurationFactory.php',
'Rector\\Configuration\\Option' => $baseDir . '/src/Configuration/Option.php',
'Rector\\Configuration\\Parameter\\SimpleParameterProvider' => $baseDir . '/src/Configuration/Parameter/SimpleParameterProvider.php',
'Rector\\Configuration\\PhpLevelSetResolver' => $baseDir . '/src/Configuration/PhpLevelSetResolver.php',
'Rector\\Configuration\\RectorConfigBuilder' => $baseDir . '/src/Configuration/RectorConfigBuilder.php',
'Rector\\Configuration\\RenamedClassesDataCollector' => $baseDir . '/src/Configuration/RenamedClassesDataCollector.php',
'Rector\\Console\\Command\\CustomRuleCommand' => $baseDir . '/src/Console/Command/CustomRuleCommand.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3
'Rector\\Configuration\\ConfigurationFactory' => __DIR__ . '/../..' . '/src/Configuration/ConfigurationFactory.php',
'Rector\\Configuration\\Option' => __DIR__ . '/../..' . '/src/Configuration/Option.php',
'Rector\\Configuration\\Parameter\\SimpleParameterProvider' => __DIR__ . '/../..' . '/src/Configuration/Parameter/SimpleParameterProvider.php',
'Rector\\Configuration\\PhpLevelSetResolver' => __DIR__ . '/../..' . '/src/Configuration/PhpLevelSetResolver.php',
'Rector\\Configuration\\RectorConfigBuilder' => __DIR__ . '/../..' . '/src/Configuration/RectorConfigBuilder.php',
'Rector\\Configuration\\RenamedClassesDataCollector' => __DIR__ . '/../..' . '/src/Configuration/RenamedClassesDataCollector.php',
'Rector\\Console\\Command\\CustomRuleCommand' => __DIR__ . '/../..' . '/src/Console/Command/CustomRuleCommand.php',
Expand Down

0 comments on commit 3c3170a

Please sign in to comment.