diff --git a/rules/Php74/Guard/MakePropertyTypedGuard.php b/rules/Php74/Guard/MakePropertyTypedGuard.php index d7ed77864b5..79c44b942a2 100644 --- a/rules/Php74/Guard/MakePropertyTypedGuard.php +++ b/rules/Php74/Guard/MakePropertyTypedGuard.php @@ -4,6 +4,7 @@ declare (strict_types=1); namespace Rector\Php74\Guard; use PhpParser\Node\Stmt\Property; +use PHPStan\Reflection\ClassReflection; final class MakePropertyTypedGuard { /** @@ -15,11 +16,11 @@ final class MakePropertyTypedGuard { $this->propertyTypeChangeGuard = $propertyTypeChangeGuard; } - public function isLegal(Property $property, bool $inlinePublic = \true) : bool + public function isLegal(Property $property, ClassReflection $classReflection, bool $inlinePublic = \true) : bool { if ($property->type !== null) { return \false; } - return $this->propertyTypeChangeGuard->isLegal($property, $inlinePublic); + return $this->propertyTypeChangeGuard->isLegal($property, $classReflection, $inlinePublic); } } diff --git a/rules/Php74/Guard/PropertyTypeChangeGuard.php b/rules/Php74/Guard/PropertyTypeChangeGuard.php index 409e21e4f86..d07a5efca87 100644 --- a/rules/Php74/Guard/PropertyTypeChangeGuard.php +++ b/rules/Php74/Guard/PropertyTypeChangeGuard.php @@ -7,7 +7,6 @@ use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\ClassReflection; use Rector\Core\NodeAnalyzer\PropertyAnalyzer; use Rector\Core\NodeManipulator\PropertyManipulator; -use Rector\Core\Reflection\ReflectionResolver; use Rector\NodeNameResolver\NodeNameResolver; use Rector\Privatization\Guard\ParentPropertyLookupGuard; final class PropertyTypeChangeGuard @@ -32,28 +31,18 @@ final class PropertyTypeChangeGuard * @var \Rector\Privatization\Guard\ParentPropertyLookupGuard */ private $parentPropertyLookupGuard; - /** - * @readonly - * @var \Rector\Core\Reflection\ReflectionResolver - */ - private $reflectionResolver; - public function __construct(NodeNameResolver $nodeNameResolver, PropertyAnalyzer $propertyAnalyzer, PropertyManipulator $propertyManipulator, ParentPropertyLookupGuard $parentPropertyLookupGuard, ReflectionResolver $reflectionResolver) + public function __construct(NodeNameResolver $nodeNameResolver, PropertyAnalyzer $propertyAnalyzer, PropertyManipulator $propertyManipulator, ParentPropertyLookupGuard $parentPropertyLookupGuard) { $this->nodeNameResolver = $nodeNameResolver; $this->propertyAnalyzer = $propertyAnalyzer; $this->propertyManipulator = $propertyManipulator; $this->parentPropertyLookupGuard = $parentPropertyLookupGuard; - $this->reflectionResolver = $reflectionResolver; } - public function isLegal(Property $property, bool $inlinePublic = \true, bool $isConstructorPromotion = \false) : bool + public function isLegal(Property $property, ClassReflection $classReflection, bool $inlinePublic = \true, bool $isConstructorPromotion = \false) : bool { if (\count($property->props) > 1) { return \false; } - $classReflection = $this->reflectionResolver->resolveClassReflection($property); - if (!$classReflection instanceof ClassReflection) { - return \false; - } /** * - trait properties are unpredictable based on class context they appear in * - on interface properties as well, as interface not allowed to have property diff --git a/rules/Php80/Guard/MakePropertyPromotionGuard.php b/rules/Php80/Guard/MakePropertyPromotionGuard.php index a3e3c1bb540..d4624ad4adf 100644 --- a/rules/Php80/Guard/MakePropertyPromotionGuard.php +++ b/rules/Php80/Guard/MakePropertyPromotionGuard.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; +use PHPStan\Reflection\ClassReflection; use Rector\Php74\Guard\PropertyTypeChangeGuard; final class MakePropertyPromotionGuard { @@ -19,9 +20,9 @@ final class MakePropertyPromotionGuard { $this->propertyTypeChangeGuard = $propertyTypeChangeGuard; } - public function isLegal(Class_ $class, Property $property, Param $param, bool $inlinePublic = \true) : bool + public function isLegal(Class_ $class, ClassReflection $classReflection, Property $property, Param $param, bool $inlinePublic = \true) : bool { - if (!$this->propertyTypeChangeGuard->isLegal($property, $inlinePublic, \true)) { + if (!$this->propertyTypeChangeGuard->isLegal($property, $classReflection, $inlinePublic, \true)) { return \false; } if ($class->isFinal()) { diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index 4786c623390..3e138b9653f 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -22,6 +22,7 @@ use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\NodeAnalyzer\ParamAnalyzer; use Rector\Core\Rector\AbstractRector; +use Rector\Core\Reflection\ReflectionResolver; use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; @@ -76,6 +77,11 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect * @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator */ private $typeComparator; + /** + * @readonly + * @var \Rector\Core\Reflection\ReflectionResolver + */ + private $reflectionResolver; /** * @api * @var string @@ -91,7 +97,7 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect * @var bool */ private $inlinePublic = \false; - public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, VarTagRemover $varTagRemover, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, MakePropertyPromotionGuard $makePropertyPromotionGuard, TypeComparator $typeComparator) + public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, VarTagRemover $varTagRemover, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, MakePropertyPromotionGuard $makePropertyPromotionGuard, TypeComparator $typeComparator, ReflectionResolver $reflectionResolver) { $this->promotedPropertyCandidateResolver = $promotedPropertyCandidateResolver; $this->variableRenamer = $variableRenamer; @@ -100,6 +106,7 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect $this->phpDocTypeChanger = $phpDocTypeChanger; $this->makePropertyPromotionGuard = $makePropertyPromotionGuard; $this->typeComparator = $typeComparator; + $this->reflectionResolver = $reflectionResolver; } public function getRuleDefinition() : RuleDefinition { @@ -151,6 +158,7 @@ CODE_SAMPLE return null; } $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($constructClassMethod); + $classReflection = null; foreach ($promotionCandidates as $promotionCandidate) { // does property have some useful annotations? $property = $promotionCandidate->getProperty(); @@ -158,7 +166,13 @@ CODE_SAMPLE if ($this->shouldSkipParam($param)) { continue; } - if (!$this->makePropertyPromotionGuard->isLegal($node, $property, $param, $this->inlinePublic)) { + if ($classReflection === null) { + $classReflection = $this->reflectionResolver->resolveClassReflection($node); + } + if ($classReflection === null) { + return null; + } + if (!$this->makePropertyPromotionGuard->isLegal($node, $classReflection, $property, $param, $this->inlinePublic)) { continue; } $propertyStmtKey = $property->getAttribute(AttributeKey::STMT_KEY); diff --git a/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php b/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php index 01f5acaec4f..af9b5489484 100644 --- a/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php +++ b/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php @@ -26,10 +26,10 @@ final class PropertyTypeOverrideGuard } public function isLegal(Property $property, ClassReflection $classReflection) : bool { - $propertyName = $this->nodeNameResolver->getName($property); - if (!$this->makePropertyTypedGuard->isLegal($property)) { + if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection)) { return \false; } + $propertyName = $this->nodeNameResolver->getName($property); foreach ($classReflection->getParents() as $parentClassReflection) { $nativeReflectionClass = $parentClassReflection->getNativeReflection(); if (!$nativeReflectionClass->hasProperty($propertyName)) { diff --git a/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php b/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php index 50edacb5cc8..a9ba18e353f 100644 --- a/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php +++ b/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php @@ -13,6 +13,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use Rector\Core\Rector\AbstractRector; +use Rector\Core\Reflection\ReflectionResolver; use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Php74\Guard\MakePropertyTypedGuard; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; @@ -41,11 +42,17 @@ final class PropertyTypeFromStrictSetterGetterRector extends AbstractRector impl * @var \Rector\Php74\Guard\MakePropertyTypedGuard */ private $makePropertyTypedGuard; - public function __construct(GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer, SetterTypeDeclarationPropertyTypeInferer $setterTypeDeclarationPropertyTypeInferer, MakePropertyTypedGuard $makePropertyTypedGuard) + /** + * @readonly + * @var \Rector\Core\Reflection\ReflectionResolver + */ + private $reflectionResolver; + public function __construct(GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer, SetterTypeDeclarationPropertyTypeInferer $setterTypeDeclarationPropertyTypeInferer, MakePropertyTypedGuard $makePropertyTypedGuard, ReflectionResolver $reflectionResolver) { $this->getterTypeDeclarationPropertyTypeInferer = $getterTypeDeclarationPropertyTypeInferer; $this->setterTypeDeclarationPropertyTypeInferer = $setterTypeDeclarationPropertyTypeInferer; $this->makePropertyTypedGuard = $makePropertyTypedGuard; + $this->reflectionResolver = $reflectionResolver; } public function getRuleDefinition() : RuleDefinition { @@ -96,6 +103,7 @@ CODE_SAMPLE public function refactor(Node $node) : ?Node { $hasChanged = \false; + $classReflection = null; foreach ($node->getProperties() as $property) { if ($property->type instanceof Node) { continue; @@ -110,7 +118,13 @@ CODE_SAMPLE if (!$this->isDefaultExprTypeCompatible($property, $getterSetterPropertyType)) { continue; } - if (!$this->makePropertyTypedGuard->isLegal($property, \false)) { + if ($classReflection === null) { + $classReflection = $this->reflectionResolver->resolveClassReflection($node); + } + if ($classReflection === null) { + return null; + } + if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection, \false)) { continue; } $propertyTypeDeclaration = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($getterSetterPropertyType, TypeKind::PROPERTY); diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php index 6fa2d807d47..5d234625c6e 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php @@ -126,9 +126,6 @@ CODE_SAMPLE $hasChanged = \false; $classReflection = null; foreach ($node->getProperties() as $property) { - if (!$this->makePropertyTypedGuard->isLegal($property, $this->inlinePublic)) { - continue; - } // non-private property can be anything with not inline public configured if (!$property->isPrivate() && !$this->inlinePublic) { continue; @@ -139,6 +136,9 @@ CODE_SAMPLE if (!$classReflection instanceof ClassReflection) { return null; } + if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection, $this->inlinePublic)) { + continue; + } $inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection); if (!$inferredType instanceof Type) { continue; diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 4612c2f4821..4e29bfe31d4 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 = 'a2c12ab0db6bb7d6207ea5a5c63ef5e6426cf0f0'; + public const PACKAGE_VERSION = '141a94aa49a3f64e78e3dd2f2a4b1d91de425b8b'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-07-20 07:54:14'; + public const RELEASE_DATE = '2023-07-20 07:20:17'; /** * @var int */ diff --git a/vendor/autoload.php b/vendor/autoload.php index 35f1762dd3f..2c9ff1ca185 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 ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e::getLoader(); +return ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 29993c9d2e7..a91eadcb4db 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e +class ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$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 98d71db5bf0..930a309569f 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit21c4afe8327ea31dba51e0a196af480e +class ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3023,9 +3023,9 @@ class ComposerStaticInit21c4afe8327ea31dba51e0a196af480e public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$classMap; }, null, ClassLoader::class); }