mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit dfad0c3a53d7ee62aa0ea6e6feede26ada741c58
dfad0c3a53
RenameClassRector with callback support (#3023)
This commit is contained in:
parent
1767bf4e17
commit
a53b9ef444
81
rules/Renaming/Helper/RenameClassCallbackHandler.php
Normal file
81
rules/Renaming/Helper/RenameClassCallbackHandler.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Renaming\Helper;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\Configuration\RenamedClassesDataCollector;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
final class RenameClassCallbackHandler extends NodeVisitorAbstract
|
||||
{
|
||||
/**
|
||||
* @var array<callable(ClassLike, NodeNameResolver, ReflectionProvider): ?string>
|
||||
*/
|
||||
private $oldToNewClassCallbacks = [];
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Configuration\RenamedClassesDataCollector
|
||||
*/
|
||||
private $renamedClassesDataCollector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider)
|
||||
{
|
||||
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
}
|
||||
public function hasOldToNewClassCallbacks() : bool
|
||||
{
|
||||
return $this->oldToNewClassCallbacks !== [];
|
||||
}
|
||||
/**
|
||||
* @param array<callable(ClassLike, NodeNameResolver, ReflectionProvider): ?string> $oldToNewClassCallbacks
|
||||
*/
|
||||
public function addOldToNewClassCallbacks(array $oldToNewClassCallbacks) : void
|
||||
{
|
||||
$item0Unpacked = $this->oldToNewClassCallbacks;
|
||||
$this->oldToNewClassCallbacks = \array_merge($item0Unpacked, $oldToNewClassCallbacks);
|
||||
}
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getOldToNewClassesFromNode(Node $node) : array
|
||||
{
|
||||
if ($node instanceof ClassLike) {
|
||||
return $this->handleClassLike($node);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function handleClassLike(ClassLike $node) : array
|
||||
{
|
||||
$oldToNewClasses = [];
|
||||
$className = $node->name;
|
||||
if ($className === null) {
|
||||
return [];
|
||||
}
|
||||
foreach ($this->oldToNewClassCallbacks as $oldToNewClassCallback) {
|
||||
$newClassName = $oldToNewClassCallback($node, $this->nodeNameResolver, $this->reflectionProvider);
|
||||
if ($newClassName !== null) {
|
||||
$fullyQualifiedClassName = (string) $this->nodeNameResolver->getName($node);
|
||||
$this->renamedClassesDataCollector->addOldToNewClass($fullyQualifiedClassName, $newClassName);
|
||||
$oldToNewClasses[$fullyQualifiedClassName] = $newClassName;
|
||||
}
|
||||
}
|
||||
return $oldToNewClasses;
|
||||
}
|
||||
}
|
@ -34,6 +34,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockClassRenamer;
|
||||
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
|
||||
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
|
||||
use Rector\Renaming\Helper\RenameClassCallbackHandler;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
||||
final class ClassRenamer
|
||||
{
|
||||
@ -100,7 +101,12 @@ final class ClassRenamer
|
||||
* @var \Rector\Naming\Naming\UseImportsResolver
|
||||
*/
|
||||
private $useImportsResolver;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, ClassNaming $classNaming, NodeNameResolver $nodeNameResolver, PhpDocClassRenamer $phpDocClassRenamer, PhpDocInfoFactory $phpDocInfoFactory, DocBlockClassRenamer $docBlockClassRenamer, ReflectionProvider $reflectionProvider, NodeRemover $nodeRemover, ParameterProvider $parameterProvider, UseImportsResolver $useImportsResolver)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Renaming\Helper\RenameClassCallbackHandler
|
||||
*/
|
||||
private $renameClassCallbackHandler;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, ClassNaming $classNaming, NodeNameResolver $nodeNameResolver, PhpDocClassRenamer $phpDocClassRenamer, PhpDocInfoFactory $phpDocInfoFactory, DocBlockClassRenamer $docBlockClassRenamer, ReflectionProvider $reflectionProvider, NodeRemover $nodeRemover, ParameterProvider $parameterProvider, UseImportsResolver $useImportsResolver, RenameClassCallbackHandler $renameClassCallbackHandler)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
@ -113,13 +119,14 @@ final class ClassRenamer
|
||||
$this->nodeRemover = $nodeRemover;
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
$this->useImportsResolver = $useImportsResolver;
|
||||
$this->renameClassCallbackHandler = $renameClassCallbackHandler;
|
||||
}
|
||||
/**
|
||||
* @param array<string, string> $oldToNewClasses
|
||||
*/
|
||||
public function renameNode(Node $node, array $oldToNewClasses) : ?Node
|
||||
{
|
||||
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
|
||||
$oldToNewTypes = $this->createOldToNewTypes($node, $oldToNewClasses);
|
||||
$this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses);
|
||||
if ($node instanceof Name) {
|
||||
return $this->refactorName($node, $oldToNewClasses);
|
||||
@ -410,8 +417,9 @@ final class ClassRenamer
|
||||
* @param array<string, string> $oldToNewClasses
|
||||
* @return OldToNewType[]
|
||||
*/
|
||||
private function createOldToNewTypes(array $oldToNewClasses) : array
|
||||
private function createOldToNewTypes(Node $node, array $oldToNewClasses) : array
|
||||
{
|
||||
$oldToNewClasses = $this->resolveOldToNewClassCallbacks($node, $oldToNewClasses);
|
||||
$cacheKey = \md5(\serialize($oldToNewClasses));
|
||||
if (isset($this->oldToNewTypesByCacheKey[$cacheKey])) {
|
||||
return $this->oldToNewTypesByCacheKey[$cacheKey];
|
||||
@ -425,4 +433,13 @@ final class ClassRenamer
|
||||
$this->oldToNewTypesByCacheKey[$cacheKey] = $oldToNewTypes;
|
||||
return $oldToNewTypes;
|
||||
}
|
||||
/**
|
||||
* @param array<string, string> $oldToNewClasses
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function resolveOldToNewClassCallbacks(Node $node, array $oldToNewClasses) : array
|
||||
{
|
||||
$item1Unpacked = $this->renameClassCallbackHandler->getOldToNewClassesFromNode($node);
|
||||
return \array_merge($oldToNewClasses, $item1Unpacked);
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ use Rector\Core\Configuration\RenamedClassesDataCollector;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\Renaming\Helper\RenameClassCallbackHandler;
|
||||
use Rector\Renaming\NodeManipulator\ClassRenamer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -26,6 +28,10 @@ use RectorPrefix202212\Webmozart\Assert\Assert;
|
||||
*/
|
||||
final class RenameClassRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CALLBACKS = '#callbacks#';
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Configuration\RenamedClassesDataCollector
|
||||
@ -41,11 +47,17 @@ final class RenameClassRector extends AbstractRector implements ConfigurableRect
|
||||
* @var \Rector\Core\Configuration\RectorConfigProvider
|
||||
*/
|
||||
private $rectorConfigProvider;
|
||||
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer, RectorConfigProvider $rectorConfigProvider)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Renaming\Helper\RenameClassCallbackHandler
|
||||
*/
|
||||
private $renameClassCallbackHandler;
|
||||
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer, RectorConfigProvider $rectorConfigProvider, RenameClassCallbackHandler $renameClassCallbackHandler)
|
||||
{
|
||||
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
|
||||
$this->classRenamer = $classRenamer;
|
||||
$this->rectorConfigProvider = $rectorConfigProvider;
|
||||
$this->renameClassCallbackHandler = $renameClassCallbackHandler;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -88,7 +100,7 @@ CODE_SAMPLE
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
|
||||
if ($oldToNewClasses === []) {
|
||||
if ($oldToNewClasses === [] && !$this->renameClassCallbackHandler->hasOldToNewClassCallbacks()) {
|
||||
return null;
|
||||
}
|
||||
if (!$node instanceof Use_) {
|
||||
@ -104,6 +116,14 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
$oldToNewClassCallbacks = $configuration[self::CALLBACKS] ?? [];
|
||||
Assert::isArray($oldToNewClassCallbacks);
|
||||
if ($oldToNewClassCallbacks !== []) {
|
||||
Assert::allIsCallable($oldToNewClassCallbacks);
|
||||
/** @var array<callable(ClassLike, NodeNameResolver): ?string> $oldToNewClassCallbacks */
|
||||
$this->renameClassCallbackHandler->addOldToNewClassCallbacks($oldToNewClassCallbacks);
|
||||
unset($configuration[self::CALLBACKS]);
|
||||
}
|
||||
Assert::allString($configuration);
|
||||
Assert::allString(\array_keys($configuration));
|
||||
$this->addOldToNewClasses($configuration);
|
||||
|
@ -17,12 +17,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'b090cedeb5249554f17d811abc61234741d7c207';
|
||||
public const PACKAGE_VERSION = 'dfad0c3a53d7ee62aa0ea6e6feede26ada741c58';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-12-17 15:35:25';
|
||||
public const RELEASE_DATE = '2022-12-17 15:37:03';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
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 ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4::getLoader();
|
||||
return ComposerAutoloaderInite0f7a49cde01988aabc4ed685fc054f4::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -2381,6 +2381,7 @@ return array(
|
||||
'Rector\\Renaming\\Contract\\MethodCallRenameInterface' => $baseDir . '/rules/Renaming/Contract/MethodCallRenameInterface.php',
|
||||
'Rector\\Renaming\\Contract\\RenameAnnotationInterface' => $baseDir . '/rules/Renaming/Contract/RenameAnnotationInterface.php',
|
||||
'Rector\\Renaming\\Contract\\RenameClassConstFetchInterface' => $baseDir . '/rules/Renaming/Contract/RenameClassConstFetchInterface.php',
|
||||
'Rector\\Renaming\\Helper\\RenameClassCallbackHandler' => $baseDir . '/rules/Renaming/Helper/RenameClassCallbackHandler.php',
|
||||
'Rector\\Renaming\\NodeManipulator\\ClassRenamer' => $baseDir . '/rules/Renaming/NodeManipulator/ClassRenamer.php',
|
||||
'Rector\\Renaming\\NodeManipulator\\SwitchManipulator' => $baseDir . '/rules/Renaming/NodeManipulator/SwitchManipulator.php',
|
||||
'Rector\\Renaming\\Rector\\ClassConstFetch\\RenameClassConstFetchRector' => $baseDir . '/rules/Renaming/Rector/ClassConstFetch/RenameClassConstFetchRector.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 ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
class ComposerAutoloaderInite0f7a49cde01988aabc4ed685fc054f4
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,19 +22,19 @@ class ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInite0f7a49cde01988aabc4ed685fc054f4', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInite0f7a49cde01988aabc4ed685fc054f4', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire0d6fc333f230d1e8c971a3f46575eec4($fileIdentifier, $file);
|
||||
composerRequiree0f7a49cde01988aabc4ed685fc054f4($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
@ -46,7 +46,7 @@ class ComposerAutoloaderInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire0d6fc333f230d1e8c971a3f46575eec4($fileIdentifier, $file)
|
||||
function composerRequiree0f7a49cde01988aabc4ed685fc054f4($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 ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
class ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -2626,6 +2626,7 @@ class ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
'Rector\\Renaming\\Contract\\MethodCallRenameInterface' => __DIR__ . '/../..' . '/rules/Renaming/Contract/MethodCallRenameInterface.php',
|
||||
'Rector\\Renaming\\Contract\\RenameAnnotationInterface' => __DIR__ . '/../..' . '/rules/Renaming/Contract/RenameAnnotationInterface.php',
|
||||
'Rector\\Renaming\\Contract\\RenameClassConstFetchInterface' => __DIR__ . '/../..' . '/rules/Renaming/Contract/RenameClassConstFetchInterface.php',
|
||||
'Rector\\Renaming\\Helper\\RenameClassCallbackHandler' => __DIR__ . '/../..' . '/rules/Renaming/Helper/RenameClassCallbackHandler.php',
|
||||
'Rector\\Renaming\\NodeManipulator\\ClassRenamer' => __DIR__ . '/../..' . '/rules/Renaming/NodeManipulator/ClassRenamer.php',
|
||||
'Rector\\Renaming\\NodeManipulator\\SwitchManipulator' => __DIR__ . '/../..' . '/rules/Renaming/NodeManipulator/SwitchManipulator.php',
|
||||
'Rector\\Renaming\\Rector\\ClassConstFetch\\RenameClassConstFetchRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/ClassConstFetch/RenameClassConstFetchRector.php',
|
||||
@ -3054,9 +3055,9 @@ class ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit0d6fc333f230d1e8c971a3f46575eec4::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInite0f7a49cde01988aabc4ed685fc054f4::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user