Skip to content

Commit 7643289

Browse files
committed
[CodeQuality] Use static Assert:: call for assert() inside anonymous class in AssertFuncCallToPHPUnitAssertRector
1 parent ee4dc09 commit 7643289

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AssertInsideAnonymousClass extends TestCase
8+
{
9+
public function some($response)
10+
{
11+
return new class($response) {
12+
public function __construct($response)
13+
{
14+
assert((bool) $response);
15+
}
16+
};
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class AssertInsideAnonymousClass extends TestCase
29+
{
30+
public function some($response)
31+
{
32+
return new class($response) {
33+
public function __construct($response)
34+
{
35+
\PHPUnit\Framework\Assert::assertTrue((bool) $response);
36+
}
37+
};
38+
}
39+
}
40+
41+
?>

‎rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ public function refactor(Node $node): ?Class_
110110
return null;
111111
}
112112

113+
// assert() inside an anonymous class has no $this of the test case
114+
if ($node instanceof Class_ && $node->isAnonymous()) {
115+
$useStaticAssert = true;
116+
return null;
117+
}
118+
113119
if (! $node instanceof FuncCall) {
114120
return null;
115121
}

0 commit comments

Comments
 (0)