mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
Updated Rector to commit e6ebae365bf66fee2d321531c6f4e59e33efce58
e6ebae365b
[PHP 8.0] Add ConstantListClassToEnumRector (#2404)
This commit is contained in:
parent
9116f9a534
commit
4d8282a2ea
62
rules/Php80/NodeAnalyzer/EnumConstListClassDetector.php
Normal file
62
rules/Php80/NodeAnalyzer/EnumConstListClassDetector.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
final class EnumConstListClassDetector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
||||
*/
|
||||
private $nodeTypeResolver;
|
||||
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver)
|
||||
{
|
||||
$this->nodeTypeResolver = $nodeTypeResolver;
|
||||
}
|
||||
public function detect(\PhpParser\Node\Stmt\Class_ $class) : bool
|
||||
{
|
||||
$classConstants = $class->getConstants();
|
||||
// must have at least 2 constants, otherwise probably not enum
|
||||
if (\count($classConstants) < 2) {
|
||||
return \false;
|
||||
}
|
||||
// only constants are allowed, nothing else
|
||||
if (\count($class->stmts) !== \count($classConstants)) {
|
||||
return \false;
|
||||
}
|
||||
// all constant must be public
|
||||
foreach ($classConstants as $classConstant) {
|
||||
if (!$classConstant->isPublic()) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
// all constants must have exactly 1 value
|
||||
foreach ($classConstants as $classConstant) {
|
||||
if (\count($classConstant->consts) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
$constantUniqueTypeCount = $this->resolveConstantUniqueTypeCount($classConstants);
|
||||
// must be exactly 1 type
|
||||
return $constantUniqueTypeCount === 1;
|
||||
}
|
||||
/**
|
||||
* @param ClassConst[] $classConsts
|
||||
*/
|
||||
private function resolveConstantUniqueTypeCount(array $classConsts) : int
|
||||
{
|
||||
$typeClasses = [];
|
||||
// all constants must have same type
|
||||
foreach ($classConsts as $classConst) {
|
||||
$const = $classConst->consts[0];
|
||||
$constantType = $this->nodeTypeResolver->getType($const->value);
|
||||
$typeClasses[] = \get_class($constantType);
|
||||
}
|
||||
$uniqueTypeClasses = \array_unique($typeClasses);
|
||||
return \count($uniqueTypeClasses);
|
||||
}
|
||||
}
|
70
rules/Php80/Rector/Class_/ConstantListClassToEnumRector.php
Normal file
70
rules/Php80/Rector/Class_/ConstantListClassToEnumRector.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Php80\NodeAnalyzer\EnumConstListClassDetector;
|
||||
use Rector\Php81\NodeFactory\EnumFactory;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\ConstantListClassToEnumRectorTest
|
||||
*/
|
||||
final class ConstantListClassToEnumRector extends \Rector\Core\Rector\AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php80\NodeAnalyzer\EnumConstListClassDetector
|
||||
*/
|
||||
private $enumConstListClassDetector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php81\NodeFactory\EnumFactory
|
||||
*/
|
||||
private $enumFactory;
|
||||
public function __construct(\Rector\Php80\NodeAnalyzer\EnumConstListClassDetector $enumConstListClassDetector, \Rector\Php81\NodeFactory\EnumFactory $enumFactory)
|
||||
{
|
||||
$this->enumConstListClassDetector = $enumConstListClassDetector;
|
||||
$this->enumFactory = $enumFactory;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Upgrade constant list classes to full blown enum', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
||||
class Direction
|
||||
{
|
||||
public const LEFT = 'left';
|
||||
|
||||
public const RIGHT = 'right';
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
enum Direction
|
||||
{
|
||||
case LEFT;
|
||||
|
||||
case RIGHT;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [\PhpParser\Node\Stmt\Class_::class];
|
||||
}
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
||||
{
|
||||
if (!$this->enumConstListClassDetector->detect($node)) {
|
||||
return null;
|
||||
}
|
||||
return $this->enumFactory->createFromClass($node);
|
||||
}
|
||||
}
|
@ -16,11 +16,11 @@ final class VersionResolver
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '8eac546e111b9d2f4053c722a7552a2eba9e5836';
|
||||
public const PACKAGE_VERSION = 'e6ebae365bf66fee2d321531c6f4e59e33efce58';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-06-01 15:12:57';
|
||||
public const RELEASE_DATE = '2022-06-01 15:16:02';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958::getLoader();
|
||||
return ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff::getLoader();
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -2577,6 +2577,7 @@ return array(
|
||||
'Rector\\Php80\\MatchAndRefactor\\StrStartsWithMatchAndRefactor\\StrposMatchAndRefactor' => $baseDir . '/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php',
|
||||
'Rector\\Php80\\MatchAndRefactor\\StrStartsWithMatchAndRefactor\\SubstrMatchAndRefactor' => $baseDir . '/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\AnnotationTargetResolver' => $baseDir . '/rules/Php80/NodeAnalyzer/AnnotationTargetResolver.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\EnumConstListClassDetector' => $baseDir . '/rules/Php80/NodeAnalyzer/EnumConstListClassDetector.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\MatchSwitchAnalyzer' => $baseDir . '/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\PhpAttributeAnalyzer' => $baseDir . '/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\PromotedPropertyCandidateResolver' => $baseDir . '/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php',
|
||||
@ -2600,6 +2601,7 @@ return array(
|
||||
'Rector\\Php80\\Rector\\ClassMethod\\SetStateToStaticRector' => $baseDir . '/rules/Php80/Rector/ClassMethod/SetStateToStaticRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\AnnotationToAttributeRector' => $baseDir . '/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\ClassPropertyAssignToConstructorPromotionRector' => $baseDir . '/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\ConstantListClassToEnumRector' => $baseDir . '/rules/Php80/Rector/Class_/ConstantListClassToEnumRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\DoctrineAnnotationClassToAttributeRector' => $baseDir . '/rules/Php80/Rector/Class_/DoctrineAnnotationClassToAttributeRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\StringableForToStringRector' => $baseDir . '/rules/Php80/Rector/Class_/StringableForToStringRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\ClassOnObjectRector' => $baseDir . '/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php',
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958
|
||||
class ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,19 +22,19 @@ class ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit12b79b18dd89cdd5167f5528c1474958::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit556465aba0dafd245547eb9bc41abeff::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit12b79b18dd89cdd5167f5528c1474958::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit556465aba0dafd245547eb9bc41abeff::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire12b79b18dd89cdd5167f5528c1474958($fileIdentifier, $file);
|
||||
composerRequire556465aba0dafd245547eb9bc41abeff($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
@ -46,7 +46,7 @@ class ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire12b79b18dd89cdd5167f5528c1474958($fileIdentifier, $file)
|
||||
function composerRequire556465aba0dafd245547eb9bc41abeff($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
10
vendor/composer/autoload_static.php
vendored
10
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit12b79b18dd89cdd5167f5528c1474958
|
||||
class ComposerStaticInit556465aba0dafd245547eb9bc41abeff
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
@ -2904,6 +2904,7 @@ class ComposerStaticInit12b79b18dd89cdd5167f5528c1474958
|
||||
'Rector\\Php80\\MatchAndRefactor\\StrStartsWithMatchAndRefactor\\StrposMatchAndRefactor' => __DIR__ . '/../..' . '/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php',
|
||||
'Rector\\Php80\\MatchAndRefactor\\StrStartsWithMatchAndRefactor\\SubstrMatchAndRefactor' => __DIR__ . '/../..' . '/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\AnnotationTargetResolver' => __DIR__ . '/../..' . '/rules/Php80/NodeAnalyzer/AnnotationTargetResolver.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\EnumConstListClassDetector' => __DIR__ . '/../..' . '/rules/Php80/NodeAnalyzer/EnumConstListClassDetector.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\MatchSwitchAnalyzer' => __DIR__ . '/../..' . '/rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\PhpAttributeAnalyzer' => __DIR__ . '/../..' . '/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php',
|
||||
'Rector\\Php80\\NodeAnalyzer\\PromotedPropertyCandidateResolver' => __DIR__ . '/../..' . '/rules/Php80/NodeAnalyzer/PromotedPropertyCandidateResolver.php',
|
||||
@ -2927,6 +2928,7 @@ class ComposerStaticInit12b79b18dd89cdd5167f5528c1474958
|
||||
'Rector\\Php80\\Rector\\ClassMethod\\SetStateToStaticRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/ClassMethod/SetStateToStaticRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\AnnotationToAttributeRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\ClassPropertyAssignToConstructorPromotionRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\ConstantListClassToEnumRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/ConstantListClassToEnumRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\DoctrineAnnotationClassToAttributeRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/DoctrineAnnotationClassToAttributeRector.php',
|
||||
'Rector\\Php80\\Rector\\Class_\\StringableForToStringRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/StringableForToStringRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\ClassOnObjectRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php',
|
||||
@ -3782,9 +3784,9 @@ class ComposerStaticInit12b79b18dd89cdd5167f5528c1474958
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit12b79b18dd89cdd5167f5528c1474958::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit12b79b18dd89cdd5167f5528c1474958::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit12b79b18dd89cdd5167f5528c1474958::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit556465aba0dafd245547eb9bc41abeff::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit556465aba0dafd245547eb9bc41abeff::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit556465aba0dafd245547eb9bc41abeff::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
@ -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('RectorPrefix20220601\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958', false) && !interface_exists('ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958', false) && !trait_exists('ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958', false)) {
|
||||
spl_autoload_call('RectorPrefix20220601\ComposerAutoloaderInit12b79b18dd89cdd5167f5528c1474958');
|
||||
if (!class_exists('ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff', false) && !interface_exists('ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff', false) && !trait_exists('ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff', false)) {
|
||||
spl_autoload_call('RectorPrefix20220601\ComposerAutoloaderInit556465aba0dafd245547eb9bc41abeff');
|
||||
}
|
||||
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('RectorPrefix20220601\Helmich\TypoScriptParser\Parser\AST\Statement');
|
||||
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
|
||||
return \RectorPrefix20220601\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequire12b79b18dd89cdd5167f5528c1474958')) {
|
||||
function composerRequire12b79b18dd89cdd5167f5528c1474958() {
|
||||
return \RectorPrefix20220601\composerRequire12b79b18dd89cdd5167f5528c1474958(...func_get_args());
|
||||
if (!function_exists('composerRequire556465aba0dafd245547eb9bc41abeff')) {
|
||||
function composerRequire556465aba0dafd245547eb9bc41abeff() {
|
||||
return \RectorPrefix20220601\composerRequire556465aba0dafd245547eb9bc41abeff(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('scanPath')) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user