Skip to content
Merged
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
@@ -0,0 +1,17 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipAnonymousClass extends TestCase
{
public function test()
{
$someObject = mt_rand(0, 1) ? new class {
public array $items = [];
} : null;

$this->assertNotNull($someObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -107,6 +108,11 @@ public function refactor(Node $node): ?Node
return null;
}

// anonymous class has no writable class name, PHPStan uses an internal AnonymousClass<hash> name
if ($pureType->getClassReflection() instanceof ClassReflection && $pureType->getClassReflection()->isAnonymous()) {
return null;
}

$node->name = new Identifier('assertInstanceOf');

$fullyQualified = new FullyQualified($pureType->getClassName());
Expand Down
Loading