From 3cfdad6ce695b2ea651c1d602e37f129f18e585e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 31 Jan 2024 23:19:02 +0100 Subject: [PATCH] [stabilize] Remove last children method checks (#5538) * remove last children method use * remove non-reliable fixtures --- .../skip_has_children_return_mixed.php.inc | 16 ----- .../skip_has_children_return_string.php.inc | 16 ----- .../parent_and_child_return_array.php.inc | 51 --------------- ...ip_child_class_has_typed_from_void.php.inc | 22 ------- ...nge_abstract_method_different_type.php.inc | 21 ------- .../skip_child_class_has_typed.php.inc | 24 ------- .../skip_child_class_has_typed.php.inc | 27 -------- .../skip_child_class_has_typed.php.inc | 22 ------- .../skip_child_class_has_typed.php.inc | 20 ------ .../skip_child_class_has_typed.php.inc | 24 ------- .../Fixture/final_method.php.inc | 49 --------------- .../Fixture/skip_if_child_has_typed.php.inc | 22 ------- .../FixtureHardCodedOnly/final_method.php.inc | 49 --------------- .../skip_child_class_has_typed.php.inc | 25 -------- .../skip_child_class_has_typed.php.inc | 22 ------- ...nullable_union_different_in_parent.php.inc | 63 ------------------- .../Reflection/FamilyRelationsAnalyzer.php | 28 --------- .../ClassMethodReturnTypeOverrideGuard.php | 50 +-------------- 18 files changed, 1 insertion(+), 550 deletions(-) delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_has_children_return_mixed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_has_children_return_string.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector/Fixture/parent_and_child_return_array.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector/Fixture/skip_child_class_has_typed_from_void.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_change_abstract_method_different_type.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeCallRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/final_method.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/skip_if_child_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/FixtureHardCodedOnly/final_method.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/skip_child_class_has_typed.php.inc delete mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/do_not_change_nullable_union_different_in_parent.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_has_children_return_mixed.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_has_children_return_mixed.php.inc deleted file mode 100644 index 5b35e30e041..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector/Fixture/skip_has_children_return_mixed.php.inc +++ /dev/null @@ -1,16 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector/Fixture/skip_child_class_has_typed_from_void.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector/Fixture/skip_child_class_has_typed_from_void.php.inc deleted file mode 100644 index a3a19cbb2bd..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector/Fixture/skip_child_class_has_typed_from_void.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -first() && true; - } - - public function first() - { - return true; - } -} - -class SomeChild extends SkipChildClassHasTyped -{ - public function run(): string - { - return 'test'; - } - -} \ No newline at end of file diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector/Fixture/skip_child_class_has_typed.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector/Fixture/skip_child_class_has_typed.php.inc deleted file mode 100644 index 6e982187c88..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictConstantReturnRector/Fixture/skip_child_class_has_typed.php.inc +++ /dev/null @@ -1,22 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/skip_if_child_has_typed.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/skip_if_child_has_typed.php.inc deleted file mode 100644 index 6fe1392c62a..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/skip_if_child_has_typed.php.inc +++ /dev/null @@ -1,22 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/skip_child_class_has_typed.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/skip_child_class_has_typed.php.inc deleted file mode 100644 index e4b902e862a..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/skip_child_class_has_typed.php.inc +++ /dev/null @@ -1,25 +0,0 @@ -getNumber(); - } - - private function getNumber(): int - { - return 1000; - } -} - -class SomeChild extends SkipChildClassHasTyped -{ - public function getData(): string - { - return 'test'; - } - -} \ No newline at end of file diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/skip_child_class_has_typed.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/skip_child_class_has_typed.php.inc deleted file mode 100644 index 8925e2a9293..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/skip_child_class_has_typed.php.inc +++ /dev/null @@ -1,22 +0,0 @@ -age; - } -} - -class SomeChild extends SkipChildClassHasTyped -{ - public function getAge(): string - { - return 'test'; - } - -} \ No newline at end of file diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/do_not_change_nullable_union_different_in_parent.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/do_not_change_nullable_union_different_in_parent.php.inc deleted file mode 100644 index c394484d045..00000000000 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/Fixture/do_not_change_nullable_union_different_in_parent.php.inc +++ /dev/null @@ -1,63 +0,0 @@ - ------ - \ No newline at end of file diff --git a/src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php b/src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php index 748d379ee8a..a2a5282a60b 100644 --- a/src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php +++ b/src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php @@ -10,43 +10,15 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use Rector\NodeNameResolver\NodeNameResolver; -use Rector\Util\Reflection\PrivatesAccessor; final readonly class FamilyRelationsAnalyzer { public function __construct( private ReflectionProvider $reflectionProvider, - private PrivatesAccessor $privatesAccessor, private NodeNameResolver $nodeNameResolver, ) { } - /** - * @return ClassReflection[] - * @deprecated as not reliable - */ - public function getChildrenOfClassReflection(ClassReflection $desiredClassReflection): array - { - if ($desiredClassReflection->isFinalByKeyword()) { - return []; - } - - /** @var ClassReflection[] $classReflections */ - $classReflections = $this->privatesAccessor->getPrivateProperty($this->reflectionProvider, 'classes'); - - $childrenClassReflections = []; - - foreach ($classReflections as $classReflection) { - if (! $classReflection->isSubclassOf($desiredClassReflection->getName())) { - continue; - } - - $childrenClassReflections[] = $classReflection; - } - - return $childrenClassReflections; - } - /** * @api * @return string[] diff --git a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php index 93307af0e99..dc9792c1983 100644 --- a/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php +++ b/src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php @@ -4,30 +4,22 @@ namespace Rector\VendorLocker\NodeVendorLocker; -use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionVariantWithPhpDocs; use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; -use PHPStan\Reflection\Php\PhpMethodReflection; use PHPStan\Type\MixedType; -use PHPStan\Type\Type; -use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; use Rector\FileSystem\FilePathHelper; use Rector\NodeAnalyzer\MagicClassMethodAnalyzer; use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; use Rector\Reflection\ReflectionResolver; -use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard; final readonly class ClassMethodReturnTypeOverrideGuard { public function __construct( - private FamilyRelationsAnalyzer $familyRelationsAnalyzer, private ReflectionResolver $reflectionResolver, - private ReturnTypeInferer $returnTypeInferer, private ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, private FilePathHelper $filePathHelper, private MagicClassMethodAnalyzer $magicClassMethodAnalyzer @@ -57,47 +49,7 @@ public function shouldSkipClassMethod(ClassMethod $classMethod, Scope $scope): b return true; } - if ($classMethod->isFinal()) { - return false; - } - - $childrenClassReflections = $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection); - if ($childrenClassReflections === []) { - return false; - } - - if ($classMethod->returnType instanceof Node) { - return true; - } - - $returnType = $this->returnTypeInferer->inferFunctionLike($classMethod); - return $this->hasChildrenDifferentTypeClassMethod($classMethod, $childrenClassReflections, $returnType); - } - - /** - * @param ClassReflection[] $childrenClassReflections - */ - private function hasChildrenDifferentTypeClassMethod( - ClassMethod $classMethod, - array $childrenClassReflections, - Type $returnType - ): bool { - $methodName = $classMethod->name->toString(); - foreach ($childrenClassReflections as $childClassReflection) { - $methodReflection = $childClassReflection->getNativeMethod($methodName); - if (! $methodReflection instanceof PhpMethodReflection) { - continue; - } - - $parametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants()); - $childReturnType = $parametersAcceptor->getNativeReturnType(); - - if (! $returnType->isSuperTypeOf($childReturnType)->yes()) { - return true; - } - } - - return false; + return $classMethod->isFinal(); } private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scope): bool