Updated Rector to commit f8c6c5cb6a23ce48ddd576dffceff52d440c4a07

f8c6c5cb6a Remove parent node from RenameMethodRector (#4154)
This commit is contained in:
Tomas Votruba 2023-06-10 09:11:14 +00:00
parent 484965351d
commit f67dcbaeac
7 changed files with 100 additions and 72 deletions

View File

@ -3,8 +3,6 @@
declare (strict_types=1);
namespace Rector\Skipper\FileSystem;
use RectorPrefix202306\Nette\Utils\Strings;
use Rector\Skipper\Enum\AsteriskMatch;
/**
* @see \Rector\Tests\Skipper\FileSystem\FnMatchPathNormalizerTest
*/

View File

@ -9,8 +9,8 @@ use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
@ -71,34 +71,17 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
return [MethodCall::class, StaticCall::class, Class_::class, Interface_::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
* @param MethodCall|StaticCall|Class_|Interface_ $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
$classReflection = $scope->getClassReflection();
foreach ($this->methodCallRenames as $methodCallRename) {
if (!$this->isName($node->name, $methodCallRename->getOldMethod())) {
continue;
}
if ($this->shouldKeepForParentInterface($methodCallRename, $node, $classReflection)) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $methodCallRename->getObjectType())) {
continue;
}
if ($this->shouldSkipClassMethod($node, $methodCallRename)) {
continue;
}
$node->name = new Identifier($methodCallRename->getNewMethod());
if ($methodCallRename instanceof MethodCallRenameWithArrayKey && !$node instanceof ClassMethod) {
return new ArrayDimFetch($node, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
}
return $node;
if ($node instanceof Class_ || $node instanceof Interface_) {
return $this->refactorClass($node, $scope);
}
return null;
return $this->refactorMethodCallAndStaticCall($node);
}
/**
* @param mixed[] $configuration
@ -109,47 +92,37 @@ CODE_SAMPLE
$this->methodCallRenames = $configuration;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod $node
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $call
*/
private function shouldSkipClassMethod($node, MethodCallRenameInterface $methodCallRename) : bool
private function shouldSkipClassMethod($call, MethodCallRenameInterface $methodCallRename) : bool
{
if (!$node instanceof ClassMethod) {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($node);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
$targetClass = $methodCallRename->getClass();
if (!$this->reflectionProvider->hasClass($targetClass)) {
return \false;
}
$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return \false;
}
// different with configured ClassLike source? it is a child, which may has old and new exists
if (!$classReflection->hasMethod($methodCallRename->getOldMethod())) {
return \false;
}
return $classReflection->hasMethod($methodCallRename->getNewMethod());
}
return $this->shouldSkipForAlreadyExistingClassMethod($node, $methodCallRename);
}
private function shouldSkipForAlreadyExistingClassMethod(ClassMethod $classMethod, MethodCallRenameInterface $methodCallRename) : bool
{
$classLike = $this->betterNodeFinder->findParentType($classMethod, ClassLike::class);
if (!$classLike instanceof ClassLike) {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
return (bool) $classLike->getMethod($methodCallRename->getNewMethod());
$targetClass = $methodCallRename->getClass();
if (!$this->reflectionProvider->hasClass($targetClass)) {
return \false;
}
$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return \false;
}
// different with configured ClassLike source? it is a child, which may has old and new exists
if (!$classReflection->hasMethod($methodCallRename->getOldMethod())) {
return \false;
}
return $classReflection->hasMethod($methodCallRename->getNewMethod());
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInternace
*/
private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, $node, ?ClassReflection $classReflection) : bool
private function hasClassNewClassMethod($classOrInternace, MethodCallRenameInterface $methodCallRename) : bool
{
return (bool) $classOrInternace->getMethod($methodCallRename->getNewMethod());
}
private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, ?ClassReflection $classReflection) : bool
{
if (!$node instanceof ClassMethod) {
return \false;
}
if (!$classReflection instanceof ClassReflection) {
return \false;
}
@ -159,4 +132,62 @@ CODE_SAMPLE
}
return $this->classManipulator->hasParentMethodOrInterface($methodCallRename->getObjectType(), $methodCallRename->getOldMethod(), $methodCallRename->getNewMethod());
}
/**
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
* @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null
*/
private function refactorClass($classOrInterface, Scope $scope)
{
if (!$scope->isInClass()) {
return null;
}
$classReflection = $scope->getClassReflection();
$hasChanged = \false;
foreach ($classOrInterface->getMethods() as $classMethod) {
foreach ($this->methodCallRenames as $methodCallRename) {
if (!$this->isName($classMethod->name, $methodCallRename->getOldMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($classMethod, $methodCallRename->getObjectType())) {
continue;
}
if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) {
continue;
}
if ($this->hasClassNewClassMethod($classOrInterface, $methodCallRename)) {
continue;
}
$classMethod->name = new Identifier($methodCallRename->getNewMethod());
$hasChanged = \true;
}
}
if ($hasChanged) {
return $classOrInterface;
}
return null;
}
/**
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $call
* @return \PhpParser\Node\Expr\ArrayDimFetch|null|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
*/
private function refactorMethodCallAndStaticCall($call)
{
foreach ($this->methodCallRenames as $methodCallRename) {
if (!$this->isName($call->name, $methodCallRename->getOldMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($call, $methodCallRename->getObjectType())) {
continue;
}
if ($this->shouldSkipClassMethod($call, $methodCallRename)) {
continue;
}
$call->name = new Identifier($methodCallRename->getNewMethod());
if ($methodCallRename instanceof MethodCallRenameWithArrayKey) {
return new ArrayDimFetch($call, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
}
return $call;
}
return null;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '21a233dac32e82d493a60fad86d2eb72cd568838';
public const PACKAGE_VERSION = 'f8c6c5cb6a23ce48ddd576dffceff52d440c4a07';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-10 09:37:04';
public const RELEASE_DATE = '2023-06-10 09:07:22';
/**
* @var int
*/

View File

@ -5,7 +5,6 @@ namespace Rector\Core\FileSystem;
use Rector\Caching\UnchangedFilesFilter;
use Rector\Core\Util\StringUtils;
use Rector\Skipper\Enum\AsteriskMatch;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use RectorPrefix202306\Symfony\Component\Finder\Finder;
use RectorPrefix202306\Symfony\Component\Finder\SplFileInfo;

2
vendor/autoload.php vendored
View File

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

View File

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