Skip to content

Commit 1909e73

Browse files
committed
[Php81] Handle different named argument on NullToStrictIntPregSlitFuncCallLimitArgRector
1 parent 667de49 commit 1909e73

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector\Fixture;
4+
5+
class DifferentNamedArg
6+
{
7+
public function run($output)
8+
{
9+
preg_split('/\s/', $output, NULL, flags: PREG_SPLIT_NO_EMPTY);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector\Fixture;
18+
19+
class DifferentNamedArg
20+
{
21+
public function run($output)
22+
{
23+
preg_split('/\s/', $output, 0, flags: PREG_SPLIT_NO_EMPTY);
24+
}
25+
}
26+
27+
?>

rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ public function refactor(Node $node): ?Node
8888
}
8989

9090
$args = $node->getArgs();
91-
$position = $this->argsAnalyzer->hasNamedArg($args)
92-
? $this->resolveNamedPosition($args)
93-
: 2;
91+
$position = $this->argsAnalyzer->resolveArgPosition($args, 'limit', 2);
9492

9593
if ($position === null) {
9694
return null;

src/NodeAnalyzer/ArgsAnalyzer.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66

77
use PhpParser\Node\Arg;
88
use PhpParser\Node\Identifier;
9+
use Rector\NodeNameResolver\NodeNameResolver;
910

1011
final class ArgsAnalyzer
1112
{
13+
public function __construct(
14+
private readonly NodeNameResolver $nodeNameResolver
15+
) {
16+
}
17+
1218
/**
1319
* @param Arg[] $args
1420
*/
@@ -22,4 +28,24 @@ public function hasNamedArg(array $args): bool
2228

2329
return false;
2430
}
31+
32+
/**
33+
* @param Arg[] $args
34+
*/
35+
public function resolveArgPosition(array $args, string $name, int $defaultPosition): int
36+
{
37+
foreach ($args as $position => $arg) {
38+
if (! $arg->name instanceof Identifier) {
39+
continue;
40+
}
41+
42+
if (! $this->nodeNameResolver->isName($arg->name, $name)) {
43+
continue;
44+
}
45+
46+
return $position;
47+
}
48+
49+
return $defaultPosition;
50+
}
2551
}

0 commit comments

Comments
 (0)