Skip to content

Commit 339b179

Browse files
committed
[CodeQuality] Use static Assert:: call for assert() inside anonymous class in AssertFuncCallToPHPUnitAssertRector
1 parent 3ee1681 commit 339b179

11 files changed

Lines changed: 75 additions & 10 deletions
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/NodeAnalyser/DoctrineEntityDocumentAnalyser.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public function isEntityClass(string $className): bool
3232
return false;
3333
}
3434

35-
return array_any(self::ENTITY_DOCBLOCK_MARKERS, fn(string $entityDocBlockMarkers): bool => str_contains($resolvedPhpDocBlock->getPhpDocString(), $entityDocBlockMarkers));
35+
return array_any(
36+
self::ENTITY_DOCBLOCK_MARKERS,
37+
fn (string $entityDocBlockMarkers): bool => str_contains(
38+
$resolvedPhpDocBlock->getPhpDocString(),
39+
$entityDocBlockMarkers
40+
)
41+
);
3642
}
3743
}

‎rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
66

7-
use function in_array;
87
use PhpParser\Comment\Doc;
98
use PhpParser\Node;
109
use PhpParser\Node\Stmt\ClassMethod;
@@ -16,6 +15,7 @@
1615
use Rector\Util\NewLineSplitter;
1716
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1817
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
use function in_array;
1919

2020
/**
2121
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest
@@ -86,7 +86,10 @@ public function refactor(Node $node): ?Node
8686
return null;
8787
}
8888

89-
$hasAnnotation = array_any(NewLineSplitter::split($docComment->getText()), fn(string $row): bool => in_array(trim($row), ['*@test', '* @test'], true));
89+
$hasAnnotation = array_any(
90+
NewLineSplitter::split($docComment->getText()),
91+
fn (string $row): bool => in_array(trim($row), ['*@test', '* @test'], true)
92+
);
9093

9194
if (! $hasAnnotation) {
9295
return null;

‎rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

7-
use PhpParser\Node\Identifier;
87
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Stmt\Class_;
1010
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1111
use Rector\Rector\AbstractRector;

‎rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

7-
use PhpParser\Node\Identifier;
87
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Stmt\Class_;
1010
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1111
use Rector\Rector\AbstractRector;

‎rules/CodeQuality/Rector/Expression/AssertArrayCastedObjectToAssertSameRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,6 @@ public function refactor(Node $node): ?array
161161
*/
162162
private function hasAllKeysString(array $assertedArrayValues): bool
163163
{
164-
return array_all(array_keys($assertedArrayValues), fn(int|string $key): bool => is_string($key));
164+
return array_all(array_keys($assertedArrayValues), fn (int|string $key): bool => is_string($key));
165165
}
166166
}

‎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
}

‎rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@ private function hasRelateParentClass(Class_ $class): bool
119119
return false;
120120
}
121121

122-
return array_any(self::PARENT_CLASSES, fn(string $parentClass): bool => $classReflection->is($parentClass));
122+
return array_any(self::PARENT_CLASSES, fn (string $parentClass): bool => $classReflection->is($parentClass));
123123
}
124124
}

‎rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ private function shouldAddAttribute(array $missedTestMethodsByMockPropertyName):
270270
*/
271271
private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMockPropertyName): bool
272272
{
273-
return array_any($usingTestMethodsByMockPropertyName, fn(array $usingTestMethods): bool => $usingTestMethods !== []);
273+
return array_any(
274+
$usingTestMethodsByMockPropertyName,
275+
fn (array $usingTestMethods): bool => $usingTestMethods !== []
276+
);
274277
}
275278

276279
private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool

‎src/NodeAnalyzer/AssertCallAnalyzer.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public function isAssertMethodCall(MethodCall|StaticCall $call): bool
9797
return false;
9898
}
9999

100-
return array_any(self::ASSERT_METHOD_NAME_PREFIXES, fn(string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix));
100+
return array_any(
101+
self::ASSERT_METHOD_NAME_PREFIXES,
102+
fn (string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix)
103+
);
101104
}
102105

103106
private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool

0 commit comments

Comments
 (0)