Skip to content

Commit df327c2

Browse files
authored
[PHPUnit90] Handle named and unpacked preg_match() args in AssertRegExpRector (#820)
1 parent 22c6dae commit df327c2

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public function refactor(Node $node): ?Node
128128
continue;
129129
}
130130

131+
if (! $this->hasMovablePregMatchArgs($secondArgumentValue)) {
132+
continue;
133+
}
134+
131135
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
132136
$stmt->expr,
133137
[self::ASSERT_SAME, self::ASSERT_EQUALS, self::ASSERT_NOT_SAME, self::ASSERT_NOT_EQUALS]
@@ -194,6 +198,16 @@ private function renameMethod(MethodCall|StaticCall $node, string $oldMethodName
194198
}
195199
}
196200

201+
private function hasMovablePregMatchArgs(FuncCall $funcCall): bool
202+
{
203+
$args = $funcCall->getArgs();
204+
if (! isset($args[0], $args[1])) {
205+
return false;
206+
}
207+
208+
return array_all($args, fn ($arg): bool => ! $arg->unpack);
209+
}
210+
197211
private function moveFunctionArgumentsUp(MethodCall|StaticCall $node): void
198212
{
199213
$oldArguments = $node->getArgs();
@@ -204,6 +218,13 @@ private function moveFunctionArgumentsUp(MethodCall|StaticCall $node): void
204218
$regex = $pregMatchFunction->getArgs()[0];
205219
$variable = $pregMatchFunction->getArgs()[1];
206220

221+
// preg_match() names it $subject, PHPUnit names it $string
222+
foreach ([$regex, $variable] as $arg) {
223+
if ($arg->name instanceof Identifier && $this->isName($arg->name, 'subject')) {
224+
$arg->name = new Identifier('string');
225+
}
226+
}
227+
207228
unset($oldArguments[0], $oldArguments[1]);
208229

209230
$node->args = [$regex, $variable, ...$oldArguments];

0 commit comments

Comments
 (0)