Skip to content

Commit 5a66eeb

Browse files
[Php81] Handle different named argument on NullToStrictIntPregSlitFuncCallLimitArgRector (#7241)
* [Php81] Handle different named argument on NullToStrictIntPregSlitFuncCallLimitArgRector * use * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 667de49 commit 5a66eeb

6 files changed

Lines changed: 68 additions & 55 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/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Rector\DeadCode\Rector\ClassMethod;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Name\FullyQualified;
98
use PhpParser\Node\Stmt\Class_;
109
use PhpParser\Node\Stmt\ClassMethod;
1110
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;

rules/Php81/Rector/FuncCall/NullToStrictIntPregSlitFuncCallLimitArgRector.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace Rector\Php81\Rector\FuncCall;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Arg;
98
use PhpParser\Node\Expr\FuncCall;
10-
use PhpParser\Node\Identifier;
119
use PHPStan\Analyser\Scope;
1210
use PHPStan\Reflection\ClassReflection;
1311
use PHPStan\Reflection\FunctionReflection;
@@ -88,13 +86,7 @@ public function refactor(Node $node): ?Node
8886
}
8987

9088
$args = $node->getArgs();
91-
$position = $this->argsAnalyzer->hasNamedArg($args)
92-
? $this->resolveNamedPosition($args)
93-
: 2;
94-
95-
if ($position === null) {
96-
return null;
97-
}
89+
$position = $this->argsAnalyzer->resolveArgPosition($args, 'limit', 2);
9890

9991
if (! isset($args[$position])) {
10092
return null;
@@ -131,26 +123,6 @@ public function provideMinPhpVersion(): int
131123
return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_STRING_FUNCTION;
132124
}
133125

134-
/**
135-
* @param Arg[] $args
136-
*/
137-
private function resolveNamedPosition(array $args): ?int
138-
{
139-
foreach ($args as $position => $arg) {
140-
if (! $arg->name instanceof Identifier) {
141-
continue;
142-
}
143-
144-
if (! $this->isName($arg->name, 'limit')) {
145-
continue;
146-
}
147-
148-
return $position;
149-
}
150-
151-
return null;
152-
}
153-
154126
private function shouldSkip(FuncCall $funcCall): bool
155127
{
156128
if (! $this->isName($funcCall, 'preg_split')) {

rules/Php85/Rector/Class_/SleepToSerializeRector.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Rector\Php85\Rector\Class_;
66

7+
use PhpParser\Node\Scalar\String_;
8+
use PhpParser\Node\Expr\Variable;
79
use PhpParser\Node;
810
use PhpParser\Node\Expr\Array_;
911
use PhpParser\Node\Expr\ArrayItem;
@@ -28,8 +30,8 @@
2830
final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface
2931
{
3032
public function __construct(
31-
private BetterNodeFinder $betterNodeFinder,
32-
private ReturnAnalyzer $returnAnalyzer
33+
private readonly BetterNodeFinder $betterNodeFinder,
34+
private readonly ReturnAnalyzer $returnAnalyzer
3335
) {
3436
}
3537

@@ -110,18 +112,19 @@ public function refactor(Node $node): ?Node
110112
return null;
111113
}
112114

113-
if (count($return->expr->items) > 0) {
115+
if ($return->expr->items !== []) {
114116
$newItems = [];
115117
foreach ($return->expr->items as $item) {
116-
if ($item !== null && $item->value instanceof Node\Scalar\String_) {
118+
if ($item !== null && $item->value instanceof String_) {
117119
$propName = $item->value->value;
118120
$newItems[] = new ArrayItem(
119-
new PropertyFetch(new Node\Expr\Variable('this'), $propName),
121+
new PropertyFetch(new Variable('this'), $propName),
120122
$item->value
121123
);
122124
}
123125
}
124-
if (count($newItems) > 0) {
126+
127+
if ($newItems !== []) {
125128
$hasChanged = true;
126129
$return->expr->items = $newItems;
127130
}

rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
namespace Rector\Php85\Rector\FuncCall;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Arg;
98
use PhpParser\Node\Expr\FuncCall;
10-
use PhpParser\Node\Identifier;
119
use PHPStan\Analyser\Scope;
1210
use PHPStan\Reflection\ClassReflection;
1311
use PHPStan\Reflection\FunctionReflection;
12+
use Rector\NodeAnalyzer\ArgsAnalyzer;
1413
use Rector\NodeTypeResolver\Node\AttributeKey;
1514
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
1615
use Rector\Php81\NodeManipulator\NullToStrictStringIntConverter;
@@ -29,7 +28,8 @@ final class ArrayKeyExistsNullToEmptyStringRector extends AbstractRector impleme
2928
{
3029
public function __construct(
3130
private readonly ReflectionResolver $reflectionResolver,
32-
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter
31+
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter,
32+
private readonly ArgsAnalyzer $argsAnalyzer
3333
) {
3434
}
3535

@@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node
9393
$result = $this->nullToStrictStringIntConverter->convertIfNull(
9494
$node,
9595
$args,
96-
$this->resolvePosition($args),
96+
$this->argsAnalyzer->resolveArgPosition($args, 'key', 0),
9797
$isTrait,
9898
$scope,
9999
$parametersAcceptor
@@ -109,18 +109,4 @@ public function provideMinPhpVersion(): int
109109
{
110110
return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION;
111111
}
112-
113-
/**
114-
* @param Arg[] $args
115-
*/
116-
private function resolvePosition(array $args): int
117-
{
118-
foreach ($args as $position => $arg) {
119-
if ($arg->name instanceof Identifier && $arg->name->toString() === 'key') {
120-
return $position;
121-
}
122-
}
123-
124-
return 0;
125-
}
126112
}

src/NodeAnalyzer/ArgsAnalyzer.php

Lines changed: 27 additions & 1 deletion
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

10-
final class ArgsAnalyzer
11+
final readonly class ArgsAnalyzer
1112
{
13+
public function __construct(
14+
private 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)