Skip to content

Commit f0d267e

Browse files
Dukeczsamsonasik
andauthored
ReplaceTestAnnotationWithPrefixedFunctionRector does not match @test annotation if it is being part of string (#671)
* ReplaceTestAnnotationWithPrefixedFunctionRector does not match @test annotation if being part of string * NewLineSplitter used instead of PHP_EOL explode * Added break to avoid unnecessary foreach iteration Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
1 parent 501420d commit f0d267e

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\Fixture;
4+
5+
class SkipTestAnnotationAsPartOfText extends \PHPUnit\Framework\TestCase
6+
{
7+
/**
8+
* there is some comment with something@test
9+
* @test-different-annotation
10+
*/
11+
public function onePlusOneShouldBeTwo()
12+
{
13+
$this->assertSame(2, 1+1);
14+
}
15+
}
16+
17+
?>

rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1313
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1414
use Rector\Rector\AbstractRector;
15+
use Rector\Util\NewLineSplitter;
1516
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1617
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
use function in_array;
1719

1820
/**
1921
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest
@@ -84,7 +86,15 @@ public function refactor(Node $node): ?Node
8486
return null;
8587
}
8688

87-
if (! str_contains($docComment->getText(), '@test')) {
89+
$hasAnnotation = false;
90+
foreach(NewLineSplitter::split($docComment->getText()) as $row) {
91+
if (in_array(trim($row), ['*@test', '* @test'])) {
92+
$hasAnnotation = true;
93+
break;
94+
}
95+
}
96+
97+
if (! $hasAnnotation) {
8898
return null;
8999
}
90100

0 commit comments

Comments
 (0)