Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Fixture;

class Fixture
use Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Source\SomeParentClass;

class Fixture extends SomeParentClass
{
public function test()
{
Expand All @@ -16,7 +18,9 @@ class Fixture

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Fixture;

class Fixture
use Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Source\SomeParentClass;

class Fixture extends SomeParentClass
{
public function test()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Fixture;

class SkipGetParentClassWithoutParent
{
public function test()
{
echo get_parent_class();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Fixture;

function skipOutsideClass()
{
echo get_class();
echo get_parent_class();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector\Source;

class SomeParentClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\VarLikeIdentifier;
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\PhpVersion;
Expand All @@ -20,6 +22,11 @@
*/
final class RemoveGetClassGetParentClassNoArgsRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -76,11 +83,21 @@ public function refactor(Node $node): ?Node
$target = 'parent';
}

if ($target !== null) {
return new ClassConstFetch(new Name([$target]), new VarLikeIdentifier('class'));
if ($target === null) {
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

// parent::class is a fatal error in a class without parent
if ($target === 'parent' && ! $classReflection->getParentClass() instanceof ClassReflection) {
return null;
}

return null;
return new ClassConstFetch(new Name([$target]), new VarLikeIdentifier('class'));
}

public function provideMinPhpVersion(): int
Expand Down
Loading