From 934d528ddf7182a86dffaa7543d9199fb970a311 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 29 Apr 2022 15:03:28 +0000 Subject: [PATCH] Updated Rector to commit 6063b59cde02c08a540ba18ab90d69ab8ba8acc7 https://github.com/rectorphp/rector-src/commit/6063b59cde02c08a540ba18ab90d69ab8ba8acc7 [Php74] Refactor MakePropertyTypedGuard to check directly on ClassReflection instead of lookup parent to get class (#2197) --- .../MatchPropertyTypeExpectedNameResolver.php | 16 ++++++++++++-- rules/Php74/Guard/MakePropertyTypedGuard.php | 21 ++++++------------- src/Application/VersionResolver.php | 4 ++-- src/NodeManipulator/PropertyManipulator.php | 19 ++++++++--------- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 14 ++++++------- vendor/composer/autoload_static.php | 8 +++---- vendor/scoper-autoload.php | 10 ++++----- 8 files changed, 48 insertions(+), 46 deletions(-) diff --git a/rules/Naming/ExpectedNameResolver/MatchPropertyTypeExpectedNameResolver.php b/rules/Naming/ExpectedNameResolver/MatchPropertyTypeExpectedNameResolver.php index 4d12501a31a..66489ecd44b 100644 --- a/rules/Naming/ExpectedNameResolver/MatchPropertyTypeExpectedNameResolver.php +++ b/rules/Naming/ExpectedNameResolver/MatchPropertyTypeExpectedNameResolver.php @@ -5,9 +5,11 @@ namespace Rector\Naming\ExpectedNameResolver; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; +use PHPStan\Reflection\ClassReflection; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Core\NodeManipulator\PropertyManipulator; use Rector\Core\PhpParser\Node\BetterNodeFinder; +use Rector\Core\Reflection\ReflectionResolver; use Rector\Naming\Naming\PropertyNaming; use Rector\Naming\ValueObject\ExpectedName; use Rector\NodeNameResolver\NodeNameResolver; @@ -38,13 +40,19 @@ final class MatchPropertyTypeExpectedNameResolver * @var \Rector\Core\NodeManipulator\PropertyManipulator */ private $propertyManipulator; - public function __construct(\Rector\Naming\Naming\PropertyNaming $propertyNaming, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator) + /** + * @readonly + * @var \Rector\Core\Reflection\ReflectionResolver + */ + private $reflectionResolver; + public function __construct(\Rector\Naming\Naming\PropertyNaming $propertyNaming, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver) { $this->propertyNaming = $propertyNaming; $this->phpDocInfoFactory = $phpDocInfoFactory; $this->nodeNameResolver = $nodeNameResolver; $this->betterNodeFinder = $betterNodeFinder; $this->propertyManipulator = $propertyManipulator; + $this->reflectionResolver = $reflectionResolver; } public function resolve(\PhpParser\Node\Stmt\Property $property) : ?string { @@ -52,8 +60,12 @@ final class MatchPropertyTypeExpectedNameResolver if (!$class instanceof \PhpParser\Node\Stmt\Class_) { return null; } + $classReflection = $this->reflectionResolver->resolveClassReflection($property); + if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) { + return null; + } $propertyName = $this->nodeNameResolver->getName($property); - if ($this->propertyManipulator->isUsedByTrait($class, $propertyName)) { + if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) { return null; } $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); diff --git a/rules/Php74/Guard/MakePropertyTypedGuard.php b/rules/Php74/Guard/MakePropertyTypedGuard.php index b18ae8417ee..36f4dd4bff8 100644 --- a/rules/Php74/Guard/MakePropertyTypedGuard.php +++ b/rules/Php74/Guard/MakePropertyTypedGuard.php @@ -3,23 +3,16 @@ declare (strict_types=1); namespace Rector\Php74\Guard; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use Rector\Core\NodeAnalyzer\PropertyAnalyzer; use Rector\Core\NodeManipulator\PropertyManipulator; -use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Privatization\Guard\ParentPropertyLookupGuard; final class MakePropertyTypedGuard { - /** - * @readonly - * @var \Rector\Core\PhpParser\Node\BetterNodeFinder - */ - private $betterNodeFinder; /** * @readonly * @var \Rector\NodeNameResolver\NodeNameResolver @@ -40,9 +33,8 @@ final class MakePropertyTypedGuard * @var \Rector\Privatization\Guard\ParentPropertyLookupGuard */ private $parentPropertyLookupGuard; - public function __construct(\Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\NodeAnalyzer\PropertyAnalyzer $propertyAnalyzer, \Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator, \Rector\Privatization\Guard\ParentPropertyLookupGuard $parentPropertyLookupGuard) + public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\NodeAnalyzer\PropertyAnalyzer $propertyAnalyzer, \Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator, \Rector\Privatization\Guard\ParentPropertyLookupGuard $parentPropertyLookupGuard) { - $this->betterNodeFinder = $betterNodeFinder; $this->nodeNameResolver = $nodeNameResolver; $this->propertyAnalyzer = $propertyAnalyzer; $this->propertyManipulator = $propertyManipulator; @@ -68,12 +60,11 @@ final class MakePropertyTypedGuard * - trait properties are unpredictable based on class context they appear in * - on interface properties as well, as interface not allowed to have property */ - $class = $this->betterNodeFinder->findParentType($property, \PhpParser\Node\Stmt\Class_::class); - if (!$class instanceof \PhpParser\Node\Stmt\Class_) { + if (!$classReflection->isClass()) { return \false; } $propertyName = $this->nodeNameResolver->getName($property); - if ($this->propertyManipulator->isUsedByTrait($class, $propertyName)) { + if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) { return \false; } if ($inlinePublic) { @@ -82,14 +73,14 @@ final class MakePropertyTypedGuard if ($property->isPrivate()) { return !$this->propertyAnalyzer->hasForbiddenType($property); } - return $this->isSafeProtectedProperty($property, $class); + return $this->isSafeProtectedProperty($property, $classReflection); } - private function isSafeProtectedProperty(\PhpParser\Node\Stmt\Property $property, \PhpParser\Node\Stmt\Class_ $class) : bool + private function isSafeProtectedProperty(\PhpParser\Node\Stmt\Property $property, \PHPStan\Reflection\ClassReflection $classReflection) : bool { if (!$property->isProtected()) { return \false; } - if (!$class->isFinal()) { + if (!$classReflection->isFinal()) { return \false; } return $this->parentPropertyLookupGuard->isLegal($property); diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 30905543dfb..2e080d81bf5 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -16,11 +16,11 @@ final class VersionResolver /** * @var string */ - public const PACKAGE_VERSION = 'dbc2638e4267c557e55bfe2372a8c91c72cec736'; + public const PACKAGE_VERSION = '6063b59cde02c08a540ba18ab90d69ab8ba8acc7'; /** * @var string */ - public const RELEASE_DATE = '2022-04-29 16:44:44'; + public const RELEASE_DATE = '2022-04-29 16:45:18'; /** * @var string */ diff --git a/src/NodeManipulator/PropertyManipulator.php b/src/NodeManipulator/PropertyManipulator.php index f7a19291a19..cffe272a1b8 100644 --- a/src/NodeManipulator/PropertyManipulator.php +++ b/src/NodeManipulator/PropertyManipulator.php @@ -25,6 +25,7 @@ use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Trait_; use PhpParser\Node\Stmt\Unset_; +use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\Type; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; @@ -265,17 +266,15 @@ final class PropertyManipulator } return null; } - public function isUsedByTrait(\PhpParser\Node\Stmt\Class_ $class, string $propertyName) : bool + public function isUsedByTrait(\PHPStan\Reflection\ClassReflection $classReflection, string $propertyName) : bool { - foreach ($class->getTraitUses() as $traitUse) { - foreach ($traitUse->traits as $traitName) { - $trait = $this->astResolver->resolveClassFromName($traitName->toString()); - if (!$trait instanceof \PhpParser\Node\Stmt\Trait_) { - continue; - } - if ($this->propertyFetchAnalyzer->containsLocalPropertyFetchName($trait, $propertyName)) { - return \true; - } + foreach ($classReflection->getTraits() as $traitUse) { + $trait = $this->astResolver->resolveClassFromName($traitUse->getName()); + if (!$trait instanceof \PhpParser\Node\Stmt\Trait_) { + continue; + } + if ($this->propertyFetchAnalyzer->containsLocalPropertyFetchName($trait, $propertyName)) { + return \true; } } return \false; diff --git a/vendor/autoload.php b/vendor/autoload.php index 32d20c78f04..351577d68a3 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81::getLoader(); +return ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 32c9da48afe..ab8ad497642 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81 +class ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e { private static $loader; @@ -22,19 +22,19 @@ class ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $includeFiles = \Composer\Autoload\ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81::$files; + $includeFiles = \Composer\Autoload\ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e::$files; foreach ($includeFiles as $fileIdentifier => $file) { - composerRequire8b9b49c35bc5226227b3d3ebf0c5ac81($fileIdentifier, $file); + composerRequire60232de5c6523ab6cfec5f5cd3a1071e($fileIdentifier, $file); } return $loader; @@ -46,7 +46,7 @@ class ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81 * @param string $file * @return void */ -function composerRequire8b9b49c35bc5226227b3d3ebf0c5ac81($fileIdentifier, $file) +function composerRequire60232de5c6523ab6cfec5f5cd3a1071e($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 fcf00cb801a..5ca3e97d9d4 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81 +class ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e { public static $files = array ( '320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php', @@ -3881,9 +3881,9 @@ class ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit8b9b49c35bc5226227b3d3ebf0c5ac81::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit60232de5c6523ab6cfec5f5cd3a1071e::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/scoper-autoload.php b/vendor/scoper-autoload.php index 952558e37ba..5a43e58b9e3 100644 --- a/vendor/scoper-autoload.php +++ b/vendor/scoper-autoload.php @@ -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('RectorPrefix20220429\AutoloadIncluder'); } -if (!class_exists('ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81', false) && !interface_exists('ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81', false) && !trait_exists('ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81', false)) { - spl_autoload_call('RectorPrefix20220429\ComposerAutoloaderInit8b9b49c35bc5226227b3d3ebf0c5ac81'); +if (!class_exists('ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e', false) && !interface_exists('ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e', false) && !trait_exists('ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e', false)) { + spl_autoload_call('RectorPrefix20220429\ComposerAutoloaderInit60232de5c6523ab6cfec5f5cd3a1071e'); } 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('RectorPrefix20220429\Helmich\TypoScriptParser\Parser\AST\Statement'); @@ -59,9 +59,9 @@ if (!function_exists('print_node')) { return \RectorPrefix20220429\print_node(...func_get_args()); } } -if (!function_exists('composerRequire8b9b49c35bc5226227b3d3ebf0c5ac81')) { - function composerRequire8b9b49c35bc5226227b3d3ebf0c5ac81() { - return \RectorPrefix20220429\composerRequire8b9b49c35bc5226227b3d3ebf0c5ac81(...func_get_args()); +if (!function_exists('composerRequire60232de5c6523ab6cfec5f5cd3a1071e')) { + function composerRequire60232de5c6523ab6cfec5f5cd3a1071e() { + return \RectorPrefix20220429\composerRequire60232de5c6523ab6cfec5f5cd3a1071e(...func_get_args()); } } if (!function_exists('scanPath')) {