diff --git a/packages/NodeTypeResolver/TypeComparator/TypeComparator.php b/packages/NodeTypeResolver/TypeComparator/TypeComparator.php index 60f6d8e2150..4c0d00040a1 100644 --- a/packages/NodeTypeResolver/TypeComparator/TypeComparator.php +++ b/packages/NodeTypeResolver/TypeComparator/TypeComparator.php @@ -5,10 +5,8 @@ namespace Rector\NodeTypeResolver\TypeComparator; use PhpParser\Node; -use PHPStan\Analyser\Scope; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; -use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ArrayType; use PHPStan\Type\BooleanType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -23,7 +21,6 @@ use PHPStan\Type\TypeTraverser; use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\NodeTypeResolver\PHPStan\TypeHasher; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -272,17 +269,13 @@ private function areTypesSameWithLiteralTypeInPhpDoc( private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType, Node $node): bool { - // special case for non-final $this/self compare; in case of interface/abstract class, it can be another $this - if ($phpStanDocType instanceof ThisType && $phpParserNodeType instanceof ThisType) { - $scope = $node->getAttribute(AttributeKey::SCOPE); - if ($scope instanceof Scope) { - $classReflection = $scope->getClassReflection(); - if ($classReflection instanceof ClassReflection) { - return $classReflection->isFinal(); - } - } - } - return true; + /** + * Special case for $this/(self|static) compare + * + * $this refers to the exact object identity, not just the same type. Therefore, it's valid and should not be removed + * @see https://wiki.php.net/rfc/this_return_type for more context + */ + return ! ($phpStanDocType instanceof ThisType && $phpParserNodeType instanceof StaticType); } } diff --git a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/union_self_this_final_class.php.inc b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_self_this_final_class.php.inc similarity index 52% rename from rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/union_self_this_final_class.php.inc rename to rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_self_this_final_class.php.inc index bfcbae5aa56..0e6c915ece2 100644 --- a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/union_self_this_final_class.php.inc +++ b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_self_this_final_class.php.inc @@ -12,19 +12,3 @@ final class SkipUnionSelfThisFinalClass return $this; } } - -?> ------ - diff --git a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_static_this_class.php.inc b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_static_this_class.php.inc new file mode 100644 index 00000000000..afd86497184 --- /dev/null +++ b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_union_static_this_class.php.inc @@ -0,0 +1,14 @@ +