Updated Rector to commit c0758c29a9

c0758c29a9 [DeadCode] Add RemovePhpVersionIdCheckRector (#222)
This commit is contained in:
Tomas Votruba 2021-06-25 15:32:54 +00:00
parent 16b47c2532
commit 348ed1d016
7 changed files with 245 additions and 19 deletions

View File

@ -0,0 +1,224 @@
<?php
declare (strict_types=1);
namespace Rector\DeadCode\Rector\ConstFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Greater;
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\If_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
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;
/**
* @see \Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\RemovePhpVersionIdCheckRectorTest
*/
final class RemovePhpVersionIdCheckRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string
*/
public const PHP_VERSION_CONSTRAINT = 'phpVersionConstraint';
/**
* @var string|int|null
*/
private $phpVersionConstraint;
/**
* @var \Rector\Core\Util\PhpVersionFactory
*/
private $phpVersionFactory;
public function __construct(\Rector\Core\Util\PhpVersionFactory $phpVersionFactory)
{
$this->phpVersionFactory = $phpVersionFactory;
}
/**
* @param array<string, int|string> $configuration
*/
public function configure(array $configuration) : void
{
$this->phpVersionConstraint = $configuration[self::PHP_VERSION_CONSTRAINT] ?? null;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
$exampleConfiguration = [self::PHP_VERSION_CONSTRAINT => \Rector\Core\ValueObject\PhpVersion::PHP_80];
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unneded PHP_VERSION_ID check', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
if (PHP_VERSION_ID < 80000) {
return;
}
echo 'do something';
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
echo 'do something';
}
}
CODE_SAMPLE
, $exampleConfiguration)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\ConstFetch::class];
}
/**
* @param ConstFetch $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
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);
$parent = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if ($this->shouldSkip($node, $if, $parent)) {
return null;
}
/** @var If_ $if */
if ($parent instanceof \PhpParser\Node\Expr\BinaryOp\Smaller) {
return $this->processSmaller($node, $parent, $if);
}
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;
}
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
{
if ($smaller->left === $constFetch) {
return $this->processSmallerLeft($constFetch, $smaller, $if);
}
if ($smaller->right === $constFetch) {
return $this->processSmallerRight($constFetch, $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
{
if ($greaterOrEqual->left === $constFetch) {
return $this->processGreaterOrEqualLeft($constFetch, $greaterOrEqual, $if);
}
if ($greaterOrEqual->right === $constFetch) {
return $this->processGreaterOrEqualRight($constFetch, $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
{
$value = $smaller->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->removeNode($if);
}
return $constFetch;
}
private function processSmallerRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
{
$value = $smaller->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
}
return $constFetch;
}
private function processGreaterOrEqualLeft(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
{
$value = $greaterOrEqual->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
}
return $constFetch;
}
private function processGreaterOrEqualRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
{
$value = $greaterOrEqual->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $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
{
$value = $greater->right;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->addNodesBeforeNode($if->stmts, $if);
$this->removeNode($if);
}
return $constFetch;
}
private function processGreaterRight(\PhpParser\Node\Expr\ConstFetch $constFetch, \PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Stmt\If_ $if) : ?\PhpParser\Node\Expr\ConstFetch
{
$value = $greater->left;
if (!$value instanceof \PhpParser\Node\Scalar\LNumber) {
return null;
}
if ($this->phpVersionConstraint >= $value->value) {
$this->removeNode($if);
}
return $constFetch;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '46e563eb57a58d2891bfdf817571cb211df46681';
public const PACKAGE_VERSION = 'c0758c29a9250cc16686e4d738a0b80381290b1b';
/**
* @var string
*/
public const RELEASE_DATE = '2021-06-25 17:18:12';
public const RELEASE_DATE = '2021-06-25 17:20:11';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210625\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff::getLoader();
return ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c::getLoader();

View File

@ -1968,6 +1968,7 @@ return array(
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessParamTagRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnTagRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php',
'Rector\\DeadCode\\Rector\\Concat\\RemoveConcatAutocastRector' => $baseDir . '/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php',
'Rector\\DeadCode\\Rector\\ConstFetch\\RemovePhpVersionIdCheckRector' => $baseDir . '/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php',
'Rector\\DeadCode\\Rector\\Expression\\RemoveDeadStmtRector' => $baseDir . '/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php',
'Rector\\DeadCode\\Rector\\Expression\\SimplifyMirrorAssignRector' => $baseDir . '/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php',
'Rector\\DeadCode\\Rector\\For_\\RemoveDeadIfForeachForRector' => $baseDir . '/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff
class ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit9a11df39cf795748223f61406285b5ff::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitb34a3d95b98b707041b08962e8820f1c::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit9a11df39cf795748223f61406285b5ff::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitb34a3d95b98b707041b08962e8820f1c::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire9a11df39cf795748223f61406285b5ff($fileIdentifier, $file);
composerRequireb34a3d95b98b707041b08962e8820f1c($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire9a11df39cf795748223f61406285b5ff($fileIdentifier, $file)
function composerRequireb34a3d95b98b707041b08962e8820f1c($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit9a11df39cf795748223f61406285b5ff
class ComposerStaticInitb34a3d95b98b707041b08962e8820f1c
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -2323,6 +2323,7 @@ class ComposerStaticInit9a11df39cf795748223f61406285b5ff
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessParamTagRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnTagRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php',
'Rector\\DeadCode\\Rector\\Concat\\RemoveConcatAutocastRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php',
'Rector\\DeadCode\\Rector\\ConstFetch\\RemovePhpVersionIdCheckRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php',
'Rector\\DeadCode\\Rector\\Expression\\RemoveDeadStmtRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php',
'Rector\\DeadCode\\Rector\\Expression\\SimplifyMirrorAssignRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php',
'Rector\\DeadCode\\Rector\\For_\\RemoveDeadIfForeachForRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php',
@ -3868,9 +3869,9 @@ class ComposerStaticInit9a11df39cf795748223f61406285b5ff
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit9a11df39cf795748223f61406285b5ff::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit9a11df39cf795748223f61406285b5ff::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit9a11df39cf795748223f61406285b5ff::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitb34a3d95b98b707041b08962e8820f1c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb34a3d95b98b707041b08962e8820f1c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb34a3d95b98b707041b08962e8820f1c::$classMap;
}, null, ClassLoader::class);
}

View File

@ -21,8 +21,8 @@ if (!class_exists('SomeTestCase', false) && !interface_exists('SomeTestCase', fa
if (!class_exists('CheckoutEntityFactory', false) && !interface_exists('CheckoutEntityFactory', false) && !trait_exists('CheckoutEntityFactory', false)) {
spl_autoload_call('RectorPrefix20210625\CheckoutEntityFactory');
}
if (!class_exists('ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff', false) && !interface_exists('ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff', false) && !trait_exists('ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff', false)) {
spl_autoload_call('RectorPrefix20210625\ComposerAutoloaderInit9a11df39cf795748223f61406285b5ff');
if (!class_exists('ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c', false) && !interface_exists('ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c', false) && !trait_exists('ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c', false)) {
spl_autoload_call('RectorPrefix20210625\ComposerAutoloaderInitb34a3d95b98b707041b08962e8820f1c');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210625\Doctrine\Inflector\Inflector');
@ -3323,9 +3323,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210625\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire9a11df39cf795748223f61406285b5ff')) {
function composerRequire9a11df39cf795748223f61406285b5ff() {
return \RectorPrefix20210625\composerRequire9a11df39cf795748223f61406285b5ff(...func_get_args());
if (!function_exists('composerRequireb34a3d95b98b707041b08962e8820f1c')) {
function composerRequireb34a3d95b98b707041b08962e8820f1c() {
return \RectorPrefix20210625\composerRequireb34a3d95b98b707041b08962e8820f1c(...func_get_args());
}
}
if (!function_exists('parseArgs')) {