Updated Rector to commit 384e84a982b8535dc06e49c0ed90b1979029e0e8

384e84a982 refactors Spatie enum method calls to native enums (#3226)
This commit is contained in:
Tomas Votruba 2022-12-28 12:01:35 +00:00
parent 8a89fc1eb2
commit b6297a1113
8 changed files with 158 additions and 13 deletions

View File

@ -13,6 +13,7 @@ use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector;
use Rector\Php81\Rector\FunctionLike\IntersectionTypesRector;
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
use Rector\Php81\Rector\MethodCall\SpatieEnumMethodCallToEnumConstRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
return static function (RectorConfig $rectorConfig) : void {
@ -22,6 +23,7 @@ return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(FinalizePublicClassConstantRector::class);
$rectorConfig->rule(ReadOnlyPropertyRector::class);
$rectorConfig->rule(SpatieEnumClassToEnumRector::class);
$rectorConfig->rule(SpatieEnumMethodCallToEnumConstRector::class);
$rectorConfig->rule(Php81ResourceReturnToObjectRector::class);
$rectorConfig->rule(NewInInitializerRector::class);
$rectorConfig->rule(IntersectionTypesRector::class);

View File

@ -46,7 +46,7 @@
- [Php80](#php80) (20)
- [Php81](#php81) (11)
- [Php81](#php81) (12)
- [Php82](#php82) (2)
@ -6549,6 +6549,25 @@ Refactor Spatie enum class to native Enum
<br>
### SpatieEnumMethodCallToEnumConstRector
Refactor Spatie enum method calls
- class: [`Rector\Php81\Rector\MethodCall\SpatieEnumMethodCallToEnumConstRector`](../rules/Php81/Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.php)
```diff
-$value1 = SomeEnum::SOME_CONSTANT()->getValue();
-$value2 = SomeEnum::SOME_CONSTANT()->value;
-$name1 = SomeEnum::SOME_CONSTANT()->getName();
-$name2 = SomeEnum::SOME_CONSTANT()->name;
+$value1 = SomeEnum::SOME_CONSTANT->value;
+$value2 = SomeEnum::SOME_CONSTANT->value;
+$name1 = SomeEnum::SOME_CONSTANT->name;
+$name2 = SomeEnum::SOME_CONSTANT->name;
```
<br>
## Php82
### ReadOnlyClassRector

View File

@ -0,0 +1,122 @@
<?php
declare (strict_types=1);
namespace Rector\Php81\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/enumerations
*
* @see \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\MyCLabsMethodCallToEnumConstRectorTest
*/
final class SpatieEnumMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface
{
private const SPATIE_FQN = 'Spatie\\Enum\\Enum';
/**
* @var string[]
*/
private const ENUM_METHODS = ['from', 'values', 'keys', 'isValid', 'search', 'toArray', 'assertValidValue'];
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Refactor Spatie enum method calls', [new CodeSample(<<<'CODE_SAMPLE'
$value1 = SomeEnum::SOME_CONSTANT()->getValue();
$value2 = SomeEnum::SOME_CONSTANT()->value;
$name1 = SomeEnum::SOME_CONSTANT()->getName();
$name2 = SomeEnum::SOME_CONSTANT()->name;
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$value1 = SomeEnum::SOME_CONSTANT->value;
$value2 = SomeEnum::SOME_CONSTANT->value;
$name1 = SomeEnum::SOME_CONSTANT->name;
$name2 = SomeEnum::SOME_CONSTANT->name;
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class];
}
/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->name instanceof Expr) {
return null;
}
$enumCaseName = $this->getName($node->name);
if ($enumCaseName === null) {
return null;
}
if ($this->shouldOmitEnumCase($enumCaseName)) {
return null;
}
if ($node instanceof MethodCall) {
return $this->refactorMethodCall($node, $enumCaseName);
}
if (!$this->isObjectType($node->class, new ObjectType(self::SPATIE_FQN))) {
return null;
}
$className = $this->getName($node->class);
if (!\is_string($className)) {
return null;
}
$constantName = \strtoupper($enumCaseName);
return $this->nodeFactory->createClassConstFetch($className, $constantName);
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::ENUM;
}
private function refactorGetterToMethodCall(MethodCall $methodCall, string $property) : ?PropertyFetch
{
if (!$methodCall->var instanceof StaticCall) {
return null;
}
$staticCall = $methodCall->var;
$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;
}
$upperCaseName = \strtoupper($enumCaseName);
$enumConstFetch = $this->nodeFactory->createClassConstFetch($className, $upperCaseName);
return new PropertyFetch($enumConstFetch, $property);
}
private function refactorMethodCall(MethodCall $methodCall, string $methodName) : ?\PhpParser\Node\Expr\PropertyFetch
{
if (!$this->isObjectType($methodCall->var, new ObjectType(self::SPATIE_FQN))) {
return null;
}
if ($methodName === 'getName' || $methodName === 'label') {
return $this->refactorGetterToMethodCall($methodCall, 'name');
}
if ($methodName === 'getValue' || $methodName === 'value') {
return $this->refactorGetterToMethodCall($methodCall, 'value');
}
return null;
}
private function shouldOmitEnumCase(string $enumCaseName) : bool
{
return \in_array($enumCaseName, self::ENUM_METHODS, \true);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'ac2f8537c1f5b9739baaeeb8228d514b29c6b0b6';
public const PACKAGE_VERSION = '384e84a982b8535dc06e49c0ed90b1979029e0e8';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-12-25 15:05:22';
public const RELEASE_DATE = '2022-12-28 12:57:18';
/**
* @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 ComposerAutoloaderInitfd66961213f532093eb0107995840d13::getLoader();
return ComposerAutoloaderInit87fe46d5f52e67c6c6f0a318acd9b01c::getLoader();

View File

@ -2271,6 +2271,7 @@ return array(
'Rector\\Php81\\Rector\\FuncCall\\Php81ResourceReturnToObjectRector' => $baseDir . '/rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php',
'Rector\\Php81\\Rector\\FunctionLike\\IntersectionTypesRector' => $baseDir . '/rules/Php81/Rector/FunctionLike/IntersectionTypesRector.php',
'Rector\\Php81\\Rector\\MethodCall\\MyCLabsMethodCallToEnumConstRector' => $baseDir . '/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php',
'Rector\\Php81\\Rector\\MethodCall\\SpatieEnumMethodCallToEnumConstRector' => $baseDir . '/rules/Php81/Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.php',
'Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector' => $baseDir . '/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php',
'Rector\\Php82\\Rector\\Class_\\ReadOnlyClassRector' => $baseDir . '/rules/Php82/Rector/Class_/ReadOnlyClassRector.php',
'Rector\\Php82\\Rector\\FuncCall\\Utf8DecodeEncodeToMbConvertEncodingRector' => $baseDir . '/rules/Php82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitfd66961213f532093eb0107995840d13
class ComposerAutoloaderInit87fe46d5f52e67c6c6f0a318acd9b01c
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitfd66961213f532093eb0107995840d13
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitfd66961213f532093eb0107995840d13', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit87fe46d5f52e67c6c6f0a318acd9b01c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitfd66961213f532093eb0107995840d13', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit87fe46d5f52e67c6c6f0a318acd9b01c', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitfd66961213f532093eb0107995840d13::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitfd66961213f532093eb0107995840d13::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c::$files;
$requireFile = 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 ComposerStaticInitfd66961213f532093eb0107995840d13
class ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2516,6 +2516,7 @@ class ComposerStaticInitfd66961213f532093eb0107995840d13
'Rector\\Php81\\Rector\\FuncCall\\Php81ResourceReturnToObjectRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php',
'Rector\\Php81\\Rector\\FunctionLike\\IntersectionTypesRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/FunctionLike/IntersectionTypesRector.php',
'Rector\\Php81\\Rector\\MethodCall\\MyCLabsMethodCallToEnumConstRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php',
'Rector\\Php81\\Rector\\MethodCall\\SpatieEnumMethodCallToEnumConstRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.php',
'Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Property/ReadOnlyPropertyRector.php',
'Rector\\Php82\\Rector\\Class_\\ReadOnlyClassRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/Class_/ReadOnlyClassRector.php',
'Rector\\Php82\\Rector\\FuncCall\\Utf8DecodeEncodeToMbConvertEncodingRector' => __DIR__ . '/../..' . '/rules/Php82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector.php',
@ -3062,9 +3063,9 @@ class ComposerStaticInitfd66961213f532093eb0107995840d13
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitfd66961213f532093eb0107995840d13::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfd66961213f532093eb0107995840d13::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfd66961213f532093eb0107995840d13::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit87fe46d5f52e67c6c6f0a318acd9b01c::$classMap;
}, null, ClassLoader::class);
}