From da60f65f51c0d90b0ea7b54f62303a2336150489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Schl=C3=BCter?= Date: Fri, 3 Nov 2023 17:29:50 +0100 Subject: [PATCH] DowngradeTo74: Fix rector rule if ReflectionClass is nullable (#201) * Run rector * DowngradeTo74: Fix rector rule if ReflectionClass is nullable --- .../Fixture/class_is_nullable.php.inc | 35 +++++++++++++++++++ ...DowngradeListReferenceAssignmentRector.php | 15 ++++---- ...wngradeContravariantArgumentTypeRector.php | 2 +- .../DowngradeCovariantReturnTypeRector.php | 2 +- ...dePreviouslyImplementedInterfaceRector.php | 8 ++--- .../DowngradePropertyPromotionRector.php | 2 +- ...DowngradeReflectionGetAttributesRector.php | 5 ++- 7 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/class_is_nullable.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/class_is_nullable.php.inc b/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/class_is_nullable.php.inc new file mode 100644 index 00000000..41d45084 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector/Fixture/class_is_nullable.php.inc @@ -0,0 +1,35 @@ +getAttributes('SomeAttribute')[0] ?? null) { + return true; + } + + return false; + } +} + +?> +----- +getAttributes('SomeAttribute') : [])[0] ?? null) { + return true; + } + + return false; + } +} + +?> diff --git a/rules/DowngradePhp73/Rector/List_/DowngradeListReferenceAssignmentRector.php b/rules/DowngradePhp73/Rector/List_/DowngradeListReferenceAssignmentRector.php index 2082f264..e10b4561 100644 --- a/rules/DowngradePhp73/Rector/List_/DowngradeListReferenceAssignmentRector.php +++ b/rules/DowngradePhp73/Rector/List_/DowngradeListReferenceAssignmentRector.php @@ -234,15 +234,12 @@ private function createAssignRefArrayFromListReferences( // Nested list. Combine with the nodes from the recursive call /** @var List_ $nestedList */ $nestedList = $listItem->value; - $listNestedArrayIndexes = array_merge($nestedArrayIndexes, [$key]); - $newNodes = array_merge( - $newNodes, - $this->createAssignRefArrayFromListReferences( - $nestedList->items, - $exprVariable, - $listNestedArrayIndexes - ) - ); + $listNestedArrayIndexes = [...$nestedArrayIndexes, $key]; + $newNodes = [...$newNodes, ...$this->createAssignRefArrayFromListReferences( + $nestedList->items, + $exprVariable, + $listNestedArrayIndexes + )]; } return $newNodes; diff --git a/rules/DowngradePhp74/Rector/ClassMethod/DowngradeContravariantArgumentTypeRector.php b/rules/DowngradePhp74/Rector/ClassMethod/DowngradeContravariantArgumentTypeRector.php index 1e5ff413..30b6a9f0 100644 --- a/rules/DowngradePhp74/Rector/ClassMethod/DowngradeContravariantArgumentTypeRector.php +++ b/rules/DowngradePhp74/Rector/ClassMethod/DowngradeContravariantArgumentTypeRector.php @@ -173,7 +173,7 @@ private function getDifferentParamTypeFromAncestorClass(Param $param, ClassMetho // parent classes or implemented interfaces /** @var ClassReflection[] $parentClassReflections */ - $parentClassReflections = array_merge($classReflection->getParents(), $classReflection->getInterfaces()); + $parentClassReflections = [...$classReflection->getParents(), ...$classReflection->getInterfaces()]; foreach ($parentClassReflections as $parentClassReflection) { $parentReflectionMethod = $this->resolveParentReflectionMethod($parentClassReflection, $methodName); diff --git a/rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php b/rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php index 25065ee1..676d92d0 100644 --- a/rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php +++ b/rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php @@ -173,7 +173,7 @@ private function resolveDifferentAncestorReturnType( $methodName = $this->getName($classMethod); /** @var ClassReflection[] $parentClassesAndInterfaces */ - $parentClassesAndInterfaces = array_merge($classReflection->getParents(), $classReflection->getInterfaces()); + $parentClassesAndInterfaces = [...$classReflection->getParents(), ...$classReflection->getInterfaces()]; return $this->resolveMatchingReturnType($parentClassesAndInterfaces, $methodName, $classMethod, $returnType); } diff --git a/rules/DowngradePhp74/Rector/Interface_/DowngradePreviouslyImplementedInterfaceRector.php b/rules/DowngradePhp74/Rector/Interface_/DowngradePreviouslyImplementedInterfaceRector.php index 41683ca0..92082f2e 100644 --- a/rules/DowngradePhp74/Rector/Interface_/DowngradePreviouslyImplementedInterfaceRector.php +++ b/rules/DowngradePhp74/Rector/Interface_/DowngradePreviouslyImplementedInterfaceRector.php @@ -89,10 +89,10 @@ public function refactor(Node $node): ?Node continue; } - $collectInterfaces = array_merge( - $collectInterfaces, - $this->familyRelationsAnalyzer->getClassLikeAncestorNames($extend) - ); + $collectInterfaces = [ + ...$collectInterfaces, + ...$this->familyRelationsAnalyzer->getClassLikeAncestorNames($extend), + ]; } if (! $isCleaned) { diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php index 8ae6c568..b0b530dd 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php @@ -193,7 +193,7 @@ private function addPropertyAssignsToConstructorClassMethod( /** @var ClassMethod $constructorClassMethod */ $constructorClassMethod = $class->getMethod(MethodName::CONSTRUCT); - $constructorClassMethod->stmts = array_merge($assigns, (array) $constructorClassMethod->stmts); + $constructorClassMethod->stmts = [...$assigns, ...(array) $constructorClassMethod->stmts]; } /** diff --git a/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php b/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php index 4cc0c46a..ee0c8b4b 100644 --- a/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php +++ b/rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php @@ -80,7 +80,10 @@ public function refactor(Node $node): Ternary|null|int return null; } - if (! $this->isObjectType($node->var, new ObjectType('Reflector'))) { + if ( + ! $this->isObjectType($node->var, new ObjectType('Reflector')) + && ! $this->isObjectType($node->var, new ObjectType('ReflectionClass')) + ) { return null; }