From 8135dd31be5fa83e2a7a8a33247efd14369c2ab1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 9 Jun 2023 16:23:18 +0000 Subject: [PATCH] Updated Rector to commit 27ac0efdca63485c6e0ee27086cda9bc7b2e6c17 https://github.com/rectorphp/rector-src/commit/27ac0efdca63485c6e0ee27086cda9bc7b2e6c17 Remove parent node lookup (#4138) --- .../GetCalledClassToSelfClassRector.php | 2 +- ...isCallOnStaticMethodToStaticCallRector.php | 96 +++++++++---------- .../FinalizePublicClassConstantRector.php | 56 +++++------ .../ParamTypeByMethodCallTypeRector.php | 55 +++++------ src/Application/VersionResolver.php | 4 +- src/Kernel/RectorKernel.php | 2 +- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 10 +- vendor/composer/autoload_static.php | 8 +- 9 files changed, 110 insertions(+), 125 deletions(-) diff --git a/rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php b/rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php index b150aa3bd2f..6d901c62171 100644 --- a/rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php +++ b/rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php @@ -3,9 +3,9 @@ declare (strict_types=1); namespace Rector\Php55\Rector\FuncCall; -use PHPStan\Analyser\Scope; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; +use PHPStan\Analyser\Scope; use Rector\Core\Enum\ObjectReference; use Rector\Core\Rector\AbstractScopeAwareRector; use Rector\Core\ValueObject\PhpVersionFeature; diff --git a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php index 21ec0e1df29..719d0885ac0 100644 --- a/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php +++ b/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php @@ -5,15 +5,15 @@ namespace Rector\Php70\Rector\MethodCall; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassLike; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\Php\PhpMethodReflection; -use PHPStan\Reflection\ReflectionProvider; -use PHPStan\Type\ObjectType; use Rector\Core\Enum\ObjectReference; -use Rector\Core\Rector\AbstractRector; +use Rector\Core\Rector\AbstractScopeAwareRector; use Rector\Core\Reflection\ReflectionResolver; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\NodeCollector\StaticAnalyzer; @@ -24,7 +24,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; * @changelog https://3v4l.org/rkiSC * @see \Rector\Tests\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest */ -final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector implements MinPhpVersionInterface +final class ThisCallOnStaticMethodToStaticCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface { /** * @readonly @@ -36,16 +36,10 @@ final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector impl * @var \Rector\Core\Reflection\ReflectionResolver */ private $reflectionResolver; - /** - * @readonly - * @var \PHPStan\Reflection\ReflectionProvider - */ - private $reflectionProvider; - public function __construct(StaticAnalyzer $staticAnalyzer, ReflectionResolver $reflectionResolver, ReflectionProvider $reflectionProvider) + public function __construct(StaticAnalyzer $staticAnalyzer, ReflectionResolver $reflectionResolver) { $this->staticAnalyzer = $staticAnalyzer; $this->reflectionResolver = $reflectionResolver; - $this->reflectionProvider = $reflectionProvider; } public function provideMinPhpVersion() : int { @@ -86,59 +80,61 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [MethodCall::class]; + return [Class_::class]; } /** - * @param MethodCall $node + * @param Class_ $node */ - public function refactor(Node $node) : ?Node + public function refactorWithScope(Node $node, Scope $scope) : ?Node { - if (!$node->var instanceof Variable) { - return null; - } - if (!$this->nodeNameResolver->isName($node->var, 'this')) { - return null; - } - if (!$node->name instanceof Identifier) { - return null; - } - $methodName = $this->getName($node->name); - if ($methodName === null) { + if (!$scope->isInClass()) { return null; } + $classReflection = $scope->getClassReflection(); // skip PHPUnit calls, as they accept both self:: and $this-> formats - if ($this->isObjectType($node->var, new ObjectType('PHPUnit\\Framework\\TestCase'))) { + if ($classReflection->isSubclassOf('PHPUnit\\Framework\\TestCase')) { return null; } - $classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class); - if (!$classLike instanceof ClassLike) { - return null; + $hasChanged = \false; + $this->traverseNodesWithCallable($node, function (Node $node) use($classReflection, &$hasChanged) : ?StaticCall { + if (!$node instanceof MethodCall) { + return null; + } + if (!$node->var instanceof Variable) { + return null; + } + if (!$this->nodeNameResolver->isName($node->var, 'this')) { + return null; + } + if (!$node->name instanceof Identifier) { + return null; + } + $methodName = $this->getName($node->name); + if ($methodName === null) { + return null; + } + $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName); + if (!$isStaticMethod) { + return null; + } + if ($node->isFirstClassCallable()) { + return null; + } + $hasChanged = \true; + $objectReference = $this->resolveClassSelf($classReflection, $node); + return $this->nodeFactory->createStaticCall($objectReference, $methodName, $node->args); + }); + if ($hasChanged) { + return $node; } - $className = (string) $this->nodeNameResolver->getName($classLike); - if (!$this->reflectionProvider->hasClass($className)) { - return null; - } - $classReflection = $this->reflectionProvider->getClass($className); - $isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName); - if (!$isStaticMethod) { - return null; - } - if ($node->isFirstClassCallable()) { - return null; - } - $objectReference = $this->resolveClassSelf($node); - return $this->nodeFactory->createStaticCall($objectReference, $methodName, $node->args); + return null; } /** * @return ObjectReference::STATIC|ObjectReference::SELF */ - private function resolveClassSelf(MethodCall $methodCall) : string + private function resolveClassSelf(ClassReflection $classReflection, MethodCall $methodCall) : string { - $classLike = $this->betterNodeFinder->findParentType($methodCall, Class_::class); - if (!$classLike instanceof Class_) { - return ObjectReference::STATIC; - } - if ($classLike->isFinal()) { + if ($classReflection->isFinalByKeyword()) { return ObjectReference::SELF; } $methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall); diff --git a/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php b/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php index 114357b3323..fc7a5ce2b16 100644 --- a/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php +++ b/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php @@ -3,12 +3,11 @@ declare (strict_types=1); namespace Rector\Php81\Rector\ClassConst; +use PHPStan\Analyser\Scope; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassConst; use PHPStan\Reflection\ReflectionProvider; -use Rector\Core\NodeAnalyzer\ClassAnalyzer; -use Rector\Core\Rector\AbstractRector; +use Rector\Core\Rector\AbstractScopeAwareRector; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; use Rector\Privatization\NodeManipulator\VisibilityManipulator; @@ -20,7 +19,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; * * @see \Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\FinalizePublicClassConstantRectorTest */ -final class FinalizePublicClassConstantRector extends AbstractRector implements MinPhpVersionInterface +final class FinalizePublicClassConstantRector extends AbstractScopeAwareRector implements MinPhpVersionInterface { /** * @readonly @@ -32,21 +31,15 @@ final class FinalizePublicClassConstantRector extends AbstractRector implements * @var \PHPStan\Reflection\ReflectionProvider */ private $reflectionProvider; - /** - * @readonly - * @var \Rector\Core\NodeAnalyzer\ClassAnalyzer - */ - private $classAnalyzer; /** * @readonly * @var \Rector\Privatization\NodeManipulator\VisibilityManipulator */ private $visibilityManipulator; - public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReflectionProvider $reflectionProvider, ClassAnalyzer $classAnalyzer, VisibilityManipulator $visibilityManipulator) + public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReflectionProvider $reflectionProvider, VisibilityManipulator $visibilityManipulator) { $this->familyRelationsAnalyzer = $familyRelationsAnalyzer; $this->reflectionProvider = $reflectionProvider; - $this->classAnalyzer = $classAnalyzer; $this->visibilityManipulator = $visibilityManipulator; } public function getRuleDefinition() : RuleDefinition @@ -70,34 +63,41 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [ClassConst::class]; + return [Class_::class]; } /** - * @param ClassConst $node + * @param Class_ $node */ - public function refactor(Node $node) : ?Node + public function refactorWithScope(Node $node, Scope $scope) : ?Node { - $class = $this->betterNodeFinder->findParentType($node, Class_::class); - if (!$class instanceof Class_) { - return null; - } - if ($class->isFinal()) { - return null; - } - if (!$node->isPublic()) { - return null; - } if ($node->isFinal()) { return null; } - if ($this->classAnalyzer->isAnonymousClass($class)) { + if (!$scope->isInClass()) { return null; } - if ($this->isClassHasChildren($class)) { + $classReflection = $scope->getClassReflection(); + if ($classReflection->isAnonymous()) { return null; } - $this->visibilityManipulator->makeFinal($node); - return $node; + $hasChanged = \false; + foreach ($node->getConstants() as $classConst) { + if (!$classConst->isPublic()) { + continue; + } + if ($classConst->isFinal()) { + continue; + } + if ($this->isClassHasChildren($node)) { + continue; + } + $hasChanged = \true; + $this->visibilityManipulator->makeFinal($classConst); + } + if ($hasChanged) { + return $node; + } + return null; } public function provideMinPhpVersion() : int { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index 210dddffcae..c02cd9b7559 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -14,13 +14,13 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\NullableType; use PhpParser\Node\Param; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\If_; use PhpParser\Node\UnionType; use PHPStan\Analyser\Scope; -use PHPStan\Reflection\ClassReflection; use Rector\Core\Rector\AbstractScopeAwareRector; -use Rector\Core\Reflection\ReflectionResolver; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher; use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard; @@ -46,17 +46,11 @@ final class ParamTypeByMethodCallTypeRector extends AbstractScopeAwareRector * @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard */ private $parentClassMethodTypeOverrideGuard; - /** - * @readonly - * @var \Rector\Core\Reflection\ReflectionResolver - */ - private $reflectionResolver; - public function __construct(CallerParamMatcher $callerParamMatcher, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, ReflectionResolver $reflectionResolver) + public function __construct(CallerParamMatcher $callerParamMatcher, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard) { $this->callerParamMatcher = $callerParamMatcher; $this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser; $this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard; - $this->reflectionResolver = $reflectionResolver; } public function getRuleDefinition() : RuleDefinition { @@ -109,30 +103,32 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [ClassMethod::class]; + return [Class_::class]; } /** - * @param ClassMethod $node + * @param Class_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { - if ($this->shouldSkipClassMethod($node)) { - return null; - } - /** @var array $callers */ - $callers = $this->betterNodeFinder->findInstancesOf((array) $node->stmts, [StaticCall::class, MethodCall::class, FuncCall::class]); $hasChanged = \false; - foreach ($node->params as $param) { - if ($this->shouldSkipParam($param, $node)) { + foreach ($node->getMethods() as $classMethod) { + if ($this->shouldSkipClassMethod($classMethod)) { continue; } - foreach ($callers as $caller) { - $paramType = $this->callerParamMatcher->matchCallParamType($caller, $param, $scope); - if ($paramType === null) { + /** @var array $callers */ + $callers = $this->betterNodeFinder->findInstancesOf($classMethod, [StaticCall::class, MethodCall::class, FuncCall::class]); + foreach ($classMethod->params as $param) { + if ($this->shouldSkipParam($param, $classMethod)) { continue; } - $this->mirrorParamType($param, $paramType); - $hasChanged = \true; + foreach ($callers as $caller) { + $paramType = $this->callerParamMatcher->matchCallParamType($caller, $param, $scope); + if ($paramType === null) { + continue; + } + $this->mirrorParamType($param, $paramType); + $hasChanged = \true; + } } } if ($hasChanged) { @@ -145,14 +141,7 @@ CODE_SAMPLE if ($classMethod->params === []) { return \true; } - if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) { - return \true; - } - $classReflection = $this->reflectionResolver->resolveClassReflection($classMethod); - if (!$classReflection instanceof ClassReflection) { - return \true; - } - return !$classReflection->isClass(); + return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod); } /** * @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType|\PhpParser\Node\ComplexType $paramType @@ -162,8 +151,8 @@ CODE_SAMPLE // mimic type $newParamType = $paramType; $this->simpleCallableNodeTraverser->traverseNodesWithCallable($newParamType, static function (Node $node) { - // original attributes have to removed to avoid tokens crashing from origin positions - $node->setAttributes([]); + // original node has to removed to avoid tokens crashing from origin positions + $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); return null; }); $decoratedParam->type = $newParamType; diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 6404cad90f7..d3f646d8ae3 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'af780c8f07bfe6fcf011789979369f03e279db44'; + public const PACKAGE_VERSION = '27ac0efdca63485c6e0ee27086cda9bc7b2e6c17'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-06-09 16:54:47'; + public const RELEASE_DATE = '2023-06-09 16:18:11'; /** * @var int */ diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index 6df4eed7e0b..30bb6c71265 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -15,7 +15,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v83'; + private const CACHE_KEY = 'v84'; /** * @var \Symfony\Component\DependencyInjection\ContainerInterface|null */ diff --git a/vendor/autoload.php b/vendor/autoload.php index d8f9f79d6eb..158c30ccf45 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit45c6c7287d901d58b750167655728442::getLoader(); +return ComposerAutoloaderInitdbb9ba42e4c01bc09b28ae467199df0b::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 6c1f35b19df..1a3925c493d 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit45c6c7287d901d58b750167655728442 +class ComposerAutoloaderInitdbb9ba42e4c01bc09b28ae467199df0b { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit45c6c7287d901d58b750167655728442 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit45c6c7287d901d58b750167655728442', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInitdbb9ba42e4c01bc09b28ae467199df0b', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit45c6c7287d901d58b750167655728442', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInitdbb9ba42e4c01bc09b28ae467199df0b', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit45c6c7287d901d58b750167655728442::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit45c6c7287d901d58b750167655728442::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 93333ca9e97..7bb8ee2b574 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit45c6c7287d901d58b750167655728442 +class ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3128,9 +3128,9 @@ class ComposerStaticInit45c6c7287d901d58b750167655728442 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit45c6c7287d901d58b750167655728442::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit45c6c7287d901d58b750167655728442::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit45c6c7287d901d58b750167655728442::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitdbb9ba42e4c01bc09b28ae467199df0b::$classMap; }, null, ClassLoader::class); }