Skip to content

Commit 28c8eda

Browse files
Fix RemoveNamedArgsInDataProviderRector so that it is not applied if no change is made
1 parent bdc4ec8 commit 28c8eda

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

rules-tests/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector/Fixture/provide_data_with_no_named_args.php.inc

Lines changed: 0 additions & 45 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\RemoveNamedArgsInDataProviderRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ProvideDataWithNoNamedArgs extends TestCase
8+
{
9+
/**
10+
* @dataProvider provideData()
11+
*/
12+
public function test()
13+
{
14+
}
15+
16+
public static function provideData()
17+
{
18+
yield [100];
19+
}
20+
}
21+
22+
?>

rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ public function refactor(Node $node): ?Node
9999
/** @var Expression $stmt */
100100
foreach ($dataProvider->getStmts() ?? [] as $stmt) {
101101
$expr = $stmt->expr;
102+
$arrayChanged = false;
102103
if ($expr instanceof Yield_) {
103-
$this->handleStmt($expr->value);
104-
$hasChanged = true;
104+
$arrayChanged = $this->handleArray($expr->value);
105105
} elseif ($expr instanceof Array_) {
106-
$this->handleStmt($expr);
106+
$arrayChanged = $this->handleArray($expr);
107+
}
108+
if ($arrayChanged) {
107109
$hasChanged = true;
108110
}
109111
}
@@ -116,16 +118,19 @@ public function refactor(Node $node): ?Node
116118
return null;
117119
}
118120

119-
private function handleStmt(Array_ $array): void
121+
private function handleArray(Array_ $array): bool
120122
{
123+
$hasChanged = false;
121124
foreach ($array->items as $item) {
122125
if (! $item instanceof ArrayItem) {
123126
continue;
124127
}
125128

126-
if (! $item->key instanceof Int_) {
129+
if (! $item->key instanceof Int_ && $item->key !== null) {
127130
$item->key = null;
131+
$hasChanged = true;
128132
}
129133
}
134+
return $hasChanged;
130135
}
131136
}

0 commit comments

Comments
 (0)