Skip to content

Commit d32a17a

Browse files
authored
[PHPUnit90] Skip non-literal expected value in AssertRegExpRector (#819)
* [PHPUnit90] Skip non-literal expected value in AssertRegExpRector * Add fixture mixing converted and skipped calls
1 parent df327c2 commit d32a17a

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\AssertRegExpRector\Fixture;
4+
5+
final class MixedConvertedAndSkipped extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testFoo(bool $expectMatch): void
8+
{
9+
$string = 'keys=1000';
10+
11+
$this->assertSame(1, preg_match('/^keys=\d+/', $string));
12+
$this->assertSame($expectMatch ? 1 : 0, preg_match('/^keys=\d+/', $string));
13+
$this->assertEquals(0, preg_match('/^values=\d+/', $string));
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\AssertRegExpRector\Fixture;
22+
23+
final class MixedConvertedAndSkipped extends \PHPUnit\Framework\TestCase
24+
{
25+
public function testFoo(bool $expectMatch): void
26+
{
27+
$string = 'keys=1000';
28+
29+
$this->assertMatchesRegularExpression('/^keys=\d+/', $string);
30+
$this->assertSame($expectMatch ? 1 : 0, preg_match('/^keys=\d+/', $string));
31+
$this->assertDoesNotMatchRegularExpression('/^values=\d+/', $string);
32+
}
33+
}
34+
35+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\AssertRegExpRector\Fixture;
4+
5+
final class SkipNonLiteralExpected extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testFoo(bool $expectMatch, int $expected): void
8+
{
9+
$string = 'keys=1000';
10+
11+
$this->assertSame($expectMatch ? 1 : 0, preg_match('/^keys=\d+/', $string));
12+
$this->assertSame($expected, preg_match('/^keys=\d+/', $string));
13+
$this->assertSame(2, preg_match('/^keys=\d+/', $string));
14+
}
15+
}

‎rules/PHPUnit90/Rector/MethodCall/AssertRegExpRector.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PhpParser\Node\Identifier;
1515
use PhpParser\Node\Scalar\Int_;
1616
use PhpParser\Node\Stmt\Expression;
17-
use Rector\Exception\ShouldNotHappenException;
1817
use Rector\NodeManipulator\StmtsManipulator;
1918
use Rector\PhpParser\Enum\NodeGroup;
2019
use Rector\PhpParser\Node\Value\ValueResolver;
@@ -153,9 +152,10 @@ public function refactor(Node $node): ?Node
153152
continue;
154153
}
155154

156-
$oldFirstArgument = $stmt->expr->getArgs()[0]
157-
->value;
158-
$oldCondition = $this->resolveOldCondition($oldFirstArgument);
155+
$oldCondition = $this->resolveOldCondition($callArgs[0]->value);
156+
if ($oldCondition === null) {
157+
continue;
158+
}
159159

160160
$this->renameMethod($stmt->expr, $oldMethodName, $oldCondition);
161161
$this->moveFunctionArgumentsUp($stmt->expr);
@@ -170,17 +170,17 @@ public function refactor(Node $node): ?Node
170170
return null;
171171
}
172172

173-
private function resolveOldCondition(Expr $expr): int
173+
private function resolveOldCondition(Expr $expr): ?int
174174
{
175175
if ($expr instanceof Int_) {
176-
return $expr->value;
176+
return in_array($expr->value, [0, 1], true) ? $expr->value : null;
177177
}
178178

179179
if ($expr instanceof ConstFetch) {
180180
return $this->valueResolver->isTrue($expr) ? 1 : 0;
181181
}
182182

183-
throw new ShouldNotHappenException();
183+
return null;
184184
}
185185

186186
private function renameMethod(MethodCall|StaticCall $node, string $oldMethodName, int $oldCondition): void

0 commit comments

Comments
 (0)