Skip to content

Commit 14022fb

Browse files
authored
add dynamic instanceof support (#578)
1 parent ce0d4e5 commit 14022fb

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ArrowWithClassVariable extends TestCase
8+
{
9+
public function test($type)
10+
{
11+
$this->createMock('SomeType')
12+
->method('someMethod')
13+
->with($this->callback(
14+
fn ($item): bool => $item instanceof $type &&
15+
$item->getName() === 'name'
16+
));
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class ArrowWithClassVariable extends TestCase
29+
{
30+
public function test($type)
31+
{
32+
$this->createMock('SomeType')
33+
->method('someMethod')
34+
->with($this->callback(
35+
function ($item) use ($type): bool {
36+
$this->assertInstanceOf($type, $item);
37+
$this->assertSame('name', $item->getName());
38+
return true;
39+
}
40+
));
41+
}
42+
}
43+
44+
?>

rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ public function create(array $exprs): ?array
6969
}
7070

7171
if ($expr instanceof Instanceof_) {
72-
if (! $expr->class instanceof Name) {
73-
return null;
72+
if ($expr->class instanceof Name) {
73+
$classNameExpr = new ClassConstFetch(new FullyQualified($expr->class->name), 'class');
74+
} else {
75+
$classNameExpr = $expr->class;
7476
}
7577

76-
$className = $expr->class->name;
7778
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
7879
'this',
7980
'assertInstanceOf',
80-
[new ClassConstFetch(new FullyQualified($className), 'class'), $expr->expr]
81+
[$classNameExpr, $expr->expr]
8182
);
8283

8384
continue;

0 commit comments

Comments
 (0)