From 45e2f2209863a58ce35456a9ac0af87539a8c7d7 Mon Sep 17 00:00:00 2001 From: Michal Kruzik Date: Mon, 11 May 2026 14:51:36 +0200 Subject: [PATCH 1/3] ReplaceTestAnnotationWithPrefixedFunctionRector does not match @test annotation if being part of string --- ...skip_test_annotation_as_part_of_text.php.inc | 17 +++++++++++++++++ ...TestAnnotationWithPrefixedFunctionRector.php | 11 ++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector/Fixture/skip_test_annotation_as_part_of_text.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector/Fixture/skip_test_annotation_as_part_of_text.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector/Fixture/skip_test_annotation_as_part_of_text.php.inc new file mode 100644 index 00000000..40109988 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector/Fixture/skip_test_annotation_as_part_of_text.php.inc @@ -0,0 +1,17 @@ +assertSame(2, 1+1); + } +} + +?> diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index b01682ac..b2493bad 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -14,6 +14,8 @@ use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; +use function explode; +use function in_array; /** * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest @@ -84,7 +86,14 @@ public function refactor(Node $node): ?Node return null; } - if (! str_contains($docComment->getText(), '@test')) { + $hasAnnotation = false; + foreach(explode(PHP_EOL, $docComment->getText()) as $row) { + if (in_array(trim($row), ['*@test', '* @test'])) { + $hasAnnotation = true; + } + } + + if (! $hasAnnotation) { return null; } From c0ef6a53320f90706ae68bab19793fcebeb7de22 Mon Sep 17 00:00:00 2001 From: Michal Kruzik Date: Mon, 11 May 2026 15:40:46 +0200 Subject: [PATCH 2/3] NewLineSplitter used instead of PHP_EOL explode --- .../ReplaceTestAnnotationWithPrefixedFunctionRector.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index b2493bad..2bc66e8a 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -12,9 +12,9 @@ use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; +use Rector\Util\NewLineSplitter; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use function explode; use function in_array; /** @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node } $hasAnnotation = false; - foreach(explode(PHP_EOL, $docComment->getText()) as $row) { + foreach(NewLineSplitter::split($docComment->getText()) as $row) { if (in_array(trim($row), ['*@test', '* @test'])) { $hasAnnotation = true; } From f839abc98e16f1e6feff07f957ff618cafc1cd3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kru=C5=BE=C3=ADk?= Date: Mon, 11 May 2026 15:51:07 +0200 Subject: [PATCH 3/3] Added break to avoid unnecessary foreach iteration Co-authored-by: Abdul Malik Ikhsan --- .../ReplaceTestAnnotationWithPrefixedFunctionRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index 2bc66e8a..02b5d843 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -90,6 +90,7 @@ public function refactor(Node $node): ?Node foreach(NewLineSplitter::split($docComment->getText()) as $row) { if (in_array(trim($row), ['*@test', '* @test'])) { $hasAnnotation = true; + break; } }