Updated Rector to commit 333933b2d3

333933b2d3 Change RemovePhpVersionIdCheckRector to work with direct If_ (#2259)
This commit is contained in:
Tomas Votruba 2022-05-07 18:21:39 +00:00
parent ae706b6205
commit 0f7f24ba96
7 changed files with 130 additions and 105 deletions

View File

@ -10,37 +10,31 @@ use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\PhpVersionFactory;
use Rector\Core\ValueObject\PhpVersion;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20220507\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\RemovePhpVersionIdCheckRectorTest
*/
final class RemovePhpVersionIdCheckRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string|int|null
* @var PhpVersion::*|null
*/
private $phpVersionConstraint = null;
/**
* @readonly
* @var \Rector\Core\Util\PhpVersionFactory
*/
private $phpVersionFactory;
private $phpVersion = null;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(\Rector\Core\Util\PhpVersionFactory $phpVersionFactory, \Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
public function __construct(\Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionFactory = $phpVersionFactory;
$this->phpVersionProvider = $phpVersionProvider;
}
/**
@ -48,11 +42,16 @@ final class RemovePhpVersionIdCheckRector extends \Rector\Core\Rector\AbstractRe
*/
public function configure(array $configuration) : void
{
$this->phpVersionConstraint = \array_pop($configuration);
$phpVersion = $configuration[0];
\RectorPrefix20220507\Webmozart\Assert\Assert::integer($phpVersion);
\Rector\Core\ValueObject\PhpVersion::assertValidValue($phpVersion);
// ensure cast to (string) first to allow string like "8.0" value to be converted to the int value
/** @var PhpVersion::* $phpVersion */
$this->phpVersion = $phpVersion;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unneeded PHP_VERSION_ID check', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unneeded PHP_VERSION_ID conditional checks', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
@ -60,6 +59,7 @@ class SomeClass
if (PHP_VERSION_ID < 80000) {
return;
}
echo 'do something';
}
}
@ -80,150 +80,172 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\ConstFetch::class];
return [\PhpParser\Node\Stmt\If_::class];
}
/**
* @param ConstFetch $node
* @param If_ $node
* @return Stmt[]|null
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
public function refactor(\PhpParser\Node $node) : ?array
{
if (!$this->isName($node, 'PHP_VERSION_ID')) {
return null;
}
/**
* $this->phpVersionProvider->provide() fallback is here as $currentFileProvider must be accessed after initialization
*/
$phpVersionConstraint = $this->phpVersionConstraint ?? $this->phpVersionProvider->provide();
// ensure cast to (string) first to allow string like "8.0" value to be converted to the int value
$this->phpVersionConstraint = $this->phpVersionFactory->createIntVersion((string) $phpVersionConstraint);
$if = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\If_::class);
if (!$if instanceof \PhpParser\Node\Stmt\If_) {
if ($this->phpVersion === null) {
$this->phpVersion = $this->phpVersionProvider->provide();
}
if (!$node->cond instanceof \PhpParser\Node\Expr\BinaryOp) {
return null;
}
$parent = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if ($this->shouldSkip($node, $if, $parent)) {
$binaryOp = $node->cond;
if ($binaryOp->left instanceof \PhpParser\Node\Expr\ConstFetch && $this->isName($binaryOp->left->name, 'PHP_VERSION_ID')) {
return $this->refactorConstFetch($binaryOp->left, $node, $binaryOp);
}
if (!$binaryOp->right instanceof \PhpParser\Node\Expr\ConstFetch) {
return null;
}
if ($parent instanceof \PhpParser\Node\Expr\BinaryOp\Smaller) {
return $this->processSmaller($node, $parent, $if);
if (!$this->isName($binaryOp->right->name, 'PHP_VERSION_ID')) {
return null;
}
if ($parent instanceof \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual) {
return $this->processGreaterOrEqual($node, $parent, $if);
}
if ($parent instanceof \PhpParser\Node\Expr\BinaryOp\Greater) {
return $this->processGreater($node, $parent, $if);
}
return null;
return $this->refactorConstFetch($binaryOp->right, $node, $binaryOp);
}
private function shouldSkip(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Stmt\If_ $if, ?\PhpParser\Node $node) : bool
{
$if = $this->betterNodeFinder->findParentType($constFetch, \PhpParser\Node\Stmt\If_::class);
if (!$if instanceof \PhpParser\Node\Stmt\If_) {
return \true;
}
$node = $constFetch->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if (!$node instanceof \PhpParser\Node\Expr\BinaryOp) {
return \true;
}
return $if->cond !== $node;
}
private function processSmaller(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return Stmt[]|null
*/
private function processSmaller(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?array
{
if ($smaller->left === $constFetch) {
return $this->processSmallerLeft($constFetch, $smaller, $if);
return $this->processSmallerLeft($smaller, $if);
}
if ($smaller->right === $constFetch) {
return $this->processSmallerRight($constFetch, $smaller, $if);
return $this->processSmallerRight($smaller, $if);
}
return null;
}
private function processGreaterOrEqual(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return Stmt[]|null
*/
private function processGreaterOrEqual(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?array
{
if ($greaterOrEqual->left === $constFetch) {
return $this->processGreaterOrEqualLeft($constFetch, $greaterOrEqual, $if);
return $this->processGreaterOrEqualLeft($greaterOrEqual, $if);
}
if ($greaterOrEqual->right === $constFetch) {
return $this->processGreaterOrEqualRight($constFetch, $greaterOrEqual, $if);
return $this->processGreaterOrEqualRight($greaterOrEqual, $if);
}
return null;
}
private function processSmallerLeft(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return null
*/
private function processSmallerLeft(\PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if)
{
$value = $smaller->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
}
return $constFetch;
return null;
}
private function processSmallerRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return Stmt[]|null
*/
private function processSmallerRight(\PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?array
{
$value = $smaller->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->nodesToAddCollector->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
if ($this->phpVersion >= $value->value) {
return $if->stmts;
}
return $constFetch;
return null;
}
private function processGreaterOrEqualLeft(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return mixed[]|null
*/
private function processGreaterOrEqualLeft(\PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if)
{
$value = $greaterOrEqual->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->nodesToAddCollector->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
if ($this->phpVersion >= $value->value) {
return $if->stmts;
}
return $constFetch;
return null;
}
private function processGreaterOrEqualRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return null
*/
private function processGreaterOrEqualRight(\PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if)
{
$value = $greaterOrEqual->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
}
return $constFetch;
}
private function processGreater(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
{
if ($greater->left === $constFetch) {
return $this->processGreaterLeft($constFetch, $greater, $if);
}
if ($greater->right === $constFetch) {
return $this->processGreaterRight($constFetch, $greater, $if);
}
return null;
}
private function processGreaterLeft(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return Stmt[]|null
*/
private function processGreater(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?array
{
if ($greater->left === $constFetch) {
return $this->processGreaterLeft($greater, $if);
}
if ($greater->right === $constFetch) {
return $this->processGreaterRight($greater, $if);
}
return null;
}
/**
* @return Stmt[]|null
*/
private function processGreaterLeft(\PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?array
{
$value = $greater->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->nodesToAddCollector->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
if ($this->phpVersion >= $value->value) {
return $if->stmts;
}
return $constFetch;
return null;
}
private function processGreaterRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
/**
* @return null
*/
private function processGreaterRight(\PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if)
{
$value = $greater->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
}
return $constFetch;
return null;
}
/**
* @return Stmt[]|null
*/
private function refactorConstFetch(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Stmt\If_ $if, \PhpParser\Node\Expr\BinaryOp $binaryOp) : ?array
{
if ($binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\Smaller) {
return $this->processSmaller($constFetch, $binaryOp, $if);
}
if ($binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual) {
return $this->processGreaterOrEqual($constFetch, $binaryOp, $if);
}
if ($binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\Greater) {
return $this->processGreater($constFetch, $binaryOp, $if);
}
return null;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'e97288c13e76eadb2265145d9d680d757924a194';
public const PACKAGE_VERSION = '333933b2d37035134f71f29af0f2e923caaef45a';
/**
* @var string
*/
public const RELEASE_DATE = '2022-05-07 13:38:43';
public const RELEASE_DATE = '2022-05-07 20:12:58';
/**
* @var string
*/

View File

@ -35,6 +35,9 @@ final class PhpVersionProvider
$this->parameterProvider = $parameterProvider;
$this->projectComposerJsonPhpVersionResolver = $projectComposerJsonPhpVersionResolver;
}
/**
* @return PhpVersion::*
*/
public function provide() : int
{
$phpVersionFeatures = $this->parameterProvider->provideParameter(\Rector\Core\Configuration\Option::PHP_VERSION_FEATURES);

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53::getLoader();
return ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53
class ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInite705914248a5c1c227c811f5fdf95f71::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInite705914248a5c1c227c811f5fdf95f71::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire78c9480d50b8ff4e437cc8803eb66c53($fileIdentifier, $file);
composerRequiree705914248a5c1c227c811f5fdf95f71($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53
* @param string $file
* @return void
*/
function composerRequire78c9480d50b8ff4e437cc8803eb66c53($fileIdentifier, $file)
function composerRequiree705914248a5c1c227c811f5fdf95f71($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53
class ComposerStaticInite705914248a5c1c227c811f5fdf95f71
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -3882,9 +3882,9 @@ class ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit78c9480d50b8ff4e437cc8803eb66c53::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInite705914248a5c1c227c811f5fdf95f71::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite705914248a5c1c227c811f5fdf95f71::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite705914248a5c1c227c811f5fdf95f71::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20220507\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53', false) && !interface_exists('ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53', false) && !trait_exists('ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53', false)) {
spl_autoload_call('RectorPrefix20220507\ComposerAutoloaderInit78c9480d50b8ff4e437cc8803eb66c53');
if (!class_exists('ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71', false) && !interface_exists('ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71', false) && !trait_exists('ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71', false)) {
spl_autoload_call('RectorPrefix20220507\ComposerAutoloaderInite705914248a5c1c227c811f5fdf95f71');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20220507\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220507\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire78c9480d50b8ff4e437cc8803eb66c53')) {
function composerRequire78c9480d50b8ff4e437cc8803eb66c53() {
return \RectorPrefix20220507\composerRequire78c9480d50b8ff4e437cc8803eb66c53(...func_get_args());
if (!function_exists('composerRequiree705914248a5c1c227c811f5fdf95f71')) {
function composerRequiree705914248a5c1c227c811f5fdf95f71() {
return \RectorPrefix20220507\composerRequiree705914248a5c1c227c811f5fdf95f71(...func_get_args());
}
}
if (!function_exists('scanPath')) {