Skip to content

Commit

Permalink
DowngradeTo74: Fix rector rule if ReflectionClass is nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
malteschlueter committed Nov 2, 2023
1 parent dac3d75 commit a82dccc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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 @@ -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 a82dccc

Please sign in to comment.