diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_inside_anonymous_class.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_inside_anonymous_class.php.inc new file mode 100644 index 00000000..de0a0193 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_inside_anonymous_class.php.inc @@ -0,0 +1,41 @@ + +----- + diff --git a/rules/CodeQuality/NodeAnalyser/DoctrineEntityDocumentAnalyser.php b/rules/CodeQuality/NodeAnalyser/DoctrineEntityDocumentAnalyser.php index e73200ea..95549dee 100644 --- a/rules/CodeQuality/NodeAnalyser/DoctrineEntityDocumentAnalyser.php +++ b/rules/CodeQuality/NodeAnalyser/DoctrineEntityDocumentAnalyser.php @@ -32,6 +32,12 @@ public function isEntityClass(string $className): bool return false; } - return array_any(self::ENTITY_DOCBLOCK_MARKERS, fn(string $entityDocBlockMarkers): bool => str_contains($resolvedPhpDocBlock->getPhpDocString(), $entityDocBlockMarkers)); + return array_any( + self::ENTITY_DOCBLOCK_MARKERS, + fn (string $entityDocBlockMarkers): bool => str_contains( + $resolvedPhpDocBlock->getPhpDocString(), + $entityDocBlockMarkers + ) + ); } } diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index bbf6ea55..de000683 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -4,7 +4,6 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; -use function in_array; use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; @@ -16,6 +15,7 @@ use Rector\Util\NewLineSplitter; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; +use function in_array; /** * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest @@ -86,7 +86,10 @@ public function refactor(Node $node): ?Node return null; } - $hasAnnotation = array_any(NewLineSplitter::split($docComment->getText()), fn(string $row): bool => in_array(trim($row), ['*@test', '* @test'], true)); + $hasAnnotation = array_any( + NewLineSplitter::split($docComment->getText()), + fn (string $row): bool => in_array(trim($row), ['*@test', '* @test'], true) + ); if (! $hasAnnotation) { return null; diff --git a/rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php b/rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php index d97a0c49..e1739a85 100644 --- a/rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php +++ b/rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php @@ -4,8 +4,8 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; -use PhpParser\Node\Identifier; use PhpParser\Node; +use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; diff --git a/rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php b/rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php index 497861a3..281b2d52 100644 --- a/rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php +++ b/rules/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector.php @@ -4,8 +4,8 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; -use PhpParser\Node\Identifier; use PhpParser\Node; +use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; diff --git a/rules/CodeQuality/Rector/Expression/AssertArrayCastedObjectToAssertSameRector.php b/rules/CodeQuality/Rector/Expression/AssertArrayCastedObjectToAssertSameRector.php index b4c729ec..7da53825 100644 --- a/rules/CodeQuality/Rector/Expression/AssertArrayCastedObjectToAssertSameRector.php +++ b/rules/CodeQuality/Rector/Expression/AssertArrayCastedObjectToAssertSameRector.php @@ -161,6 +161,6 @@ public function refactor(Node $node): ?array */ private function hasAllKeysString(array $assertedArrayValues): bool { - return array_all(array_keys($assertedArrayValues), fn(int|string $key): bool => is_string($key)); + return array_all(array_keys($assertedArrayValues), fn (int|string $key): bool => is_string($key)); } } diff --git a/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php b/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php index ae3ce0b7..0d44e1db 100644 --- a/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php +++ b/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php @@ -110,6 +110,12 @@ public function refactor(Node $node): ?Class_ return null; } + // assert() inside an anonymous class has no $this of the test case + if ($node instanceof Class_ && $node->isAnonymous()) { + $useStaticAssert = true; + return null; + } + if (! $node instanceof FuncCall) { return null; } diff --git a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php index ba35cfa7..10260a32 100644 --- a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php +++ b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php @@ -119,6 +119,6 @@ private function hasRelateParentClass(Class_ $class): bool return false; } - return array_any(self::PARENT_CLASSES, fn(string $parentClass): bool => $classReflection->is($parentClass)); + return array_any(self::PARENT_CLASSES, fn (string $parentClass): bool => $classReflection->is($parentClass)); } } diff --git a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php index a6be0425..87cf2f6c 100644 --- a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php +++ b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php @@ -270,7 +270,10 @@ private function shouldAddAttribute(array $missedTestMethodsByMockPropertyName): */ private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMockPropertyName): bool { - return array_any($usingTestMethodsByMockPropertyName, fn(array $usingTestMethods): bool => $usingTestMethods !== []); + return array_any( + $usingTestMethodsByMockPropertyName, + fn (array $usingTestMethods): bool => $usingTestMethods !== [] + ); } private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool diff --git a/src/NodeAnalyzer/AssertCallAnalyzer.php b/src/NodeAnalyzer/AssertCallAnalyzer.php index 7908aa42..7cd318d6 100644 --- a/src/NodeAnalyzer/AssertCallAnalyzer.php +++ b/src/NodeAnalyzer/AssertCallAnalyzer.php @@ -97,7 +97,10 @@ public function isAssertMethodCall(MethodCall|StaticCall $call): bool return false; } - return array_any(self::ASSERT_METHOD_NAME_PREFIXES, fn(string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix)); + return array_any( + self::ASSERT_METHOD_NAME_PREFIXES, + fn (string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix) + ); } private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index 6139ba5e..b6cc8d8f 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -35,7 +35,10 @@ public function isInTestClass(Node $node): bool return false; } - return array_any(PHPUnitClassName::TEST_CLASSES, fn(string $testCaseObjectClass): bool => $classReflection->is($testCaseObjectClass)); + return array_any( + PHPUnitClassName::TEST_CLASSES, + fn (string $testCaseObjectClass): bool => $classReflection->is($testCaseObjectClass) + ); } public function isTestClassMethod(ClassMethod $classMethod): bool