mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 11:33:33 +01:00
Updated Rector to commit e5e33ebc3be86879d802690ee851bcc923a5e5f6
e5e33ebc3b
Use class reflection do work with enum suffix (#4134)
This commit is contained in:
parent
5fa193cb7d
commit
fc7326e2ba
@ -6,37 +6,31 @@ namespace Rector\DeadCode\Rector\ClassConst;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\NodeAnalyzer\EnumAnalyzer;
|
||||
use Rector\Core\NodeManipulator\ClassConstManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\RemoveUnusedPrivateClassConstantRectorTest
|
||||
*/
|
||||
final class RemoveUnusedPrivateClassConstantRector extends AbstractRector
|
||||
final class RemoveUnusedPrivateClassConstantRector extends AbstractScopeAwareRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\ClassConstManipulator
|
||||
*/
|
||||
private $classConstManipulator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\EnumAnalyzer
|
||||
*/
|
||||
private $enumAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(ClassConstManipulator $classConstManipulator, EnumAnalyzer $enumAnalyzer, ReflectionResolver $reflectionResolver)
|
||||
public function __construct(ClassConstManipulator $classConstManipulator, ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->classConstManipulator = $classConstManipulator;
|
||||
$this->enumAnalyzer = $enumAnalyzer;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
@ -71,9 +65,9 @@ CODE_SAMPLE
|
||||
/**
|
||||
* @param ClassConst $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?int
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?int
|
||||
{
|
||||
if ($this->shouldSkipClassConst($node)) {
|
||||
if ($this->shouldSkipClassConst($node, $scope)) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
||||
@ -85,7 +79,7 @@ CODE_SAMPLE
|
||||
}
|
||||
return NodeTraverser::REMOVE_NODE;
|
||||
}
|
||||
private function shouldSkipClassConst(ClassConst $classConst) : bool
|
||||
private function shouldSkipClassConst(ClassConst $classConst, Scope $scope) : bool
|
||||
{
|
||||
if (!$classConst->isPrivate()) {
|
||||
return \true;
|
||||
@ -93,6 +87,19 @@ CODE_SAMPLE
|
||||
if (\count($classConst->consts) !== 1) {
|
||||
return \true;
|
||||
}
|
||||
return $this->enumAnalyzer->isEnumClassConst($classConst);
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return \false;
|
||||
}
|
||||
return $this->hasParentClassOfEnumSuffix($classReflection);
|
||||
}
|
||||
private function hasParentClassOfEnumSuffix(ClassReflection $classReflection) : bool
|
||||
{
|
||||
foreach ($classReflection->getParentClassesNames() as $parentClassesName) {
|
||||
if (\substr_compare($parentClassesName, 'Enum', -\strlen('Enum')) === 0) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\NodeTraverser;
|
||||
@ -82,22 +81,21 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope)
|
||||
{
|
||||
if (!$scope->isInClass()) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->php4ConstructorClassMethodAnalyzer->detect($node, $scope)) {
|
||||
return null;
|
||||
}
|
||||
$classLike = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
if (!$classLike instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $scope->getClassReflection();
|
||||
// process parent call references first
|
||||
$this->processClassMethodStatementsForParentConstructorCalls($node, $scope);
|
||||
// not PSR-4 constructor
|
||||
if (!$this->nodeNameResolver->areNamesEqual($classLike, $node)) {
|
||||
if (!$this->nodeNameResolver->isName($node, $classReflection->getName())) {
|
||||
return null;
|
||||
}
|
||||
$classMethod = $classLike->getMethod(MethodName::CONSTRUCT);
|
||||
// does it already have a __construct method?
|
||||
if (!$classMethod instanceof ClassMethod) {
|
||||
if (!$classReflection->hasConstructor()) {
|
||||
$node->name = new Identifier(MethodName::CONSTRUCT);
|
||||
}
|
||||
$classMethodStmts = $node->stmts;
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'a8db1e55f49cfbae43bdb89808498d7de04f365f';
|
||||
public const PACKAGE_VERSION = 'e5e33ebc3be86879d802690ee851bcc923a5e5f6';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-06-09 15:35:37';
|
||||
public const RELEASE_DATE = '2023-06-09 16:32:53';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@ final class RectorKernel
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const CACHE_KEY = 'v81';
|
||||
private const CACHE_KEY = 'v82';
|
||||
/**
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
|
||||
*/
|
||||
|
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
final class EnumAnalyzer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
/**
|
||||
* @see https://github.com/myclabs/php-enum#declaration
|
||||
*/
|
||||
public function isEnumClassConst(ClassConst $classConst) : bool
|
||||
{
|
||||
$class = $this->betterNodeFinder->findParentType($classConst, Class_::class);
|
||||
if (!$class instanceof Class_) {
|
||||
return \false;
|
||||
}
|
||||
if (!$class->extends instanceof Name) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeNameResolver->isName($class->extends, '*Enum');
|
||||
}
|
||||
}
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitb6518fd6a6d165496f24416ab9e93525::getLoader();
|
||||
return ComposerAutoloaderInitac7d8fb677a63fa82d05400cd485ba11::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1587,7 +1587,6 @@ return array(
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => $baseDir . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => $baseDir . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\DoctrineEntityAnalyzer' => $baseDir . '/src/NodeAnalyzer/DoctrineEntityAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => $baseDir . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ExprAnalyzer' => $baseDir . '/src/NodeAnalyzer/ExprAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ParamAnalyzer' => $baseDir . '/src/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PromotedPropertyParamCleaner' => $baseDir . '/src/NodeAnalyzer/PromotedPropertyParamCleaner.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitb6518fd6a6d165496f24416ab9e93525
|
||||
class ComposerAutoloaderInitac7d8fb677a63fa82d05400cd485ba11
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitb6518fd6a6d165496f24416ab9e93525
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb6518fd6a6d165496f24416ab9e93525', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitac7d8fb677a63fa82d05400cd485ba11', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb6518fd6a6d165496f24416ab9e93525', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitac7d8fb677a63fa82d05400cd485ba11', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb6518fd6a6d165496f24416ab9e93525::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitb6518fd6a6d165496f24416ab9e93525::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitb6518fd6a6d165496f24416ab9e93525
|
||||
class ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -1838,7 +1838,6 @@ class ComposerStaticInitb6518fd6a6d165496f24416ab9e93525
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\DoctrineEntityAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/DoctrineEntityAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ExprAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ExprAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ParamAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PromotedPropertyParamCleaner' => __DIR__ . '/../..' . '/src/NodeAnalyzer/PromotedPropertyParamCleaner.php',
|
||||
@ -3134,9 +3133,9 @@ class ComposerStaticInitb6518fd6a6d165496f24416ab9e93525
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb6518fd6a6d165496f24416ab9e93525::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb6518fd6a6d165496f24416ab9e93525::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb6518fd6a6d165496f24416ab9e93525::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitac7d8fb677a63fa82d05400cd485ba11::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user