Updated Rector to commit 5a9315aab0e60e7a8086beb1c6891de678b2f003

5a9315aab0 Refactor MyCLabs\Enum\Enum::equals calls to comparisons (#4532)
This commit is contained in:
Tomas Votruba 2023-07-18 07:48:48 +00:00
parent 5aeca28e79
commit 8701fc25cc
5 changed files with 75 additions and 13 deletions

View File

@ -5,10 +5,12 @@ namespace Rector\Php81\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
@ -114,8 +116,65 @@ CODE_SAMPLE
$enumConstFetch = $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
return new PropertyFetch($enumConstFetch, 'value');
}
private function refactorEqualsMethodCall(MethodCall $methodCall) : ?Identical
{
$left = $this->getValidEnumExpr($methodCall->var);
if ($left === null) {
return null;
}
$arg = $methodCall->getArgs()[0] ?? null;
if ($arg === null) {
return null;
}
$right = $this->getValidEnumExpr($arg->value);
if ($right === null) {
return null;
}
return new Identical($left, $right);
}
/**
* @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr\PropertyFetch
* @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr
*/
private function getValidEnumExpr(Node $node)
{
switch (\get_class($node)) {
case Variable::class:
case PropertyFetch::class:
return $this->getPropertyFetchOrVariable($node);
case StaticCall::class:
return $this->getEnumConstFetch($node);
default:
return null;
}
}
/**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\Variable $expr
* @return null|\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\Variable
*/
private function getPropertyFetchOrVariable($expr)
{
if (!$this->isObjectType($expr, new ObjectType('MyCLabs\\Enum\\Enum'))) {
return null;
}
return $expr;
}
private function getEnumConstFetch(StaticCall $staticCall) : ?\PhpParser\Node\Expr\ClassConstFetch
{
$className = $this->getName($staticCall->class);
if ($className === null) {
return null;
}
$enumCaseName = $this->getName($staticCall->name);
if ($enumCaseName === null) {
return null;
}
if ($this->shouldOmitEnumCase($enumCaseName)) {
return null;
}
return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
}
/**
* @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\BinaryOp\Identical
*/
private function refactorMethodCall(MethodCall $methodCall, string $methodName)
{
@ -128,6 +187,9 @@ CODE_SAMPLE
if ($methodName === 'getValue') {
return $this->refactorGetValueMethodCall($methodCall);
}
if ($methodName === 'equals') {
return $this->refactorEqualsMethodCall($methodCall);
}
return null;
}
private function shouldOmitEnumCase(string $enumCaseName) : bool

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '56068f8571e4b8325617fa72012e58783bd03d46';
public const PACKAGE_VERSION = '5a9315aab0e60e7a8086beb1c6891de678b2f003';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-17 11:35:18';
public const RELEASE_DATE = '2023-07-18 08:44:36';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit6fc7ebfdba45eebf190fa147a01af196
class ComposerAutoloaderInit7c2d1664db3c0cb93d0f0538a851f407
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit6fc7ebfdba45eebf190fa147a01af196
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit6fc7ebfdba45eebf190fa147a01af196', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit7c2d1664db3c0cb93d0f0538a851f407', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit6fc7ebfdba45eebf190fa147a01af196', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit7c2d1664db3c0cb93d0f0538a851f407', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407::$files;
$requireFile = \Closure::bind(static function ($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 ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196
class ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3028,9 +3028,9 @@ class ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit6fc7ebfdba45eebf190fa147a01af196::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7c2d1664db3c0cb93d0f0538a851f407::$classMap;
}, null, ClassLoader::class);
}