Skip to content

Commit

Permalink
DowngradeTo74: Fix rector rule if ReflectionClass is nullable (#201)
Browse files Browse the repository at this point in the history
* Run rector

* DowngradeTo74: Fix rector rule if ReflectionClass is nullable
  • Loading branch information
malteschlueter authored Nov 3, 2023
1 parent 2d20783 commit da60f65
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture;

class SomeClass
{
public function run(?\ReflectionClass $reflectionClass)
{
if ($reflectionClass->getAttributes('SomeAttribute')[0] ?? null) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector\Fixture;

class SomeClass
{
public function run(?\ReflectionClass $reflectionClass)
{
if ((method_exists($reflectionClass, 'getAttributes') ? $reflectionClass->getAttributes('SomeAttribute') : [])[0] ?? null) {
return true;
}

return false;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit da60f65

Please sign in to comment.