Skip to content

Commit 48b1b2a

Browse files
authored
Merge branch 'main' into __wakeup
2 parents c57a516 + 4099408 commit 48b1b2a

9 files changed

Lines changed: 105 additions & 74 deletions

File tree

config/set/php85.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use PhpParser\Node\Expr\Cast\String_;
99
use Rector\Config\RectorConfig;
1010
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
11-
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
1211
use Rector\Php85\Rector\Class_\SleepToSerializeRector;
1312
use Rector\Php85\Rector\Class_\WakeupToUnserializeRector;
13+
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
1414
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
1515
use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector;
1616
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
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+
?>
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\NullToStrictStringFuncCallArgRector\Fixture;
4+
5+
final class DifferentNamed
6+
{
7+
public function run()
8+
{
9+
preg_split('#a#', null, flags: 0);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;
18+
19+
final class DifferentNamed
20+
{
21+
public function run()
22+
{
23+
preg_split('#a#', '', flags: 0);
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/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPStan\Reflection\ClassReflection;
1313
use PHPStan\Reflection\FunctionReflection;
1414
use PHPStan\Reflection\Native\NativeFunctionReflection;
15-
use Rector\NodeAnalyzer\ArgsAnalyzer;
1615
use Rector\NodeTypeResolver\Node\AttributeKey;
1716
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
1817
use Rector\Php81\Enum\NameNullToStrictNullFunctionMap;
@@ -31,7 +30,6 @@ final class NullToStrictStringFuncCallArgRector extends AbstractRector implement
3130
{
3231
public function __construct(
3332
private readonly ReflectionResolver $reflectionResolver,
34-
private readonly ArgsAnalyzer $argsAnalyzer,
3533
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter
3634
) {
3735
}
@@ -89,9 +87,7 @@ public function refactor(Node $node): ?Node
8987
}
9088

9189
$args = $node->getArgs();
92-
$positions = $this->argsAnalyzer->hasNamedArg($args)
93-
? $this->resolveNamedPositions($node, $args)
94-
: $this->resolveOriginalPositions($node, $scope);
90+
$positions = $this->resolvePositions($node, $args, $scope);
9591

9692
if ($positions === []) {
9793
return null;
@@ -139,11 +135,13 @@ public function provideMinPhpVersion(): int
139135
* @param Arg[] $args
140136
* @return int[]|string[]
141137
*/
142-
private function resolveNamedPositions(FuncCall $funcCall, array $args): array
138+
private function resolvePositions(FuncCall $funcCall, array $args, Scope $scope): array
143139
{
140+
$positions = [];
141+
144142
$functionName = $this->getName($funcCall);
145143
$argNames = NameNullToStrictNullFunctionMap::FUNCTION_TO_PARAM_NAMES[$functionName] ?? [];
146-
$positions = [];
144+
$excludedArgNames = [];
147145

148146
foreach ($args as $position => $arg) {
149147
if (! $arg->name instanceof Identifier) {
@@ -154,20 +152,13 @@ private function resolveNamedPositions(FuncCall $funcCall, array $args): array
154152
continue;
155153
}
156154

155+
$excludedArgNames[] = $arg->name->toString();
157156
$positions[] = $position;
158157
}
159158

160-
return $positions;
161-
}
162-
163-
/**
164-
* @return int[]|string[]
165-
*/
166-
private function resolveOriginalPositions(FuncCall $funcCall, Scope $scope): array
167-
{
168159
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($funcCall);
169160
if (! $functionReflection instanceof NativeFunctionReflection) {
170-
return [];
161+
return $positions;
171162
}
172163

173164
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
@@ -177,10 +168,10 @@ private function resolveOriginalPositions(FuncCall $funcCall, Scope $scope): arr
177168
);
178169
$functionName = $functionReflection->getName();
179170
$argNames = NameNullToStrictNullFunctionMap::FUNCTION_TO_PARAM_NAMES[$functionName];
180-
$positions = [];
181171

182172
foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
183-
if (in_array($parameterReflection->getName(), $argNames, true)) {
173+
if (in_array($parameterReflection->getName(), $argNames, true)
174+
&& ! in_array($parameterReflection->getName(), $excludedArgNames, true)) {
184175
$positions[] = $position;
185176
}
186177
}

rules/Php85/Rector/Class_/SleepToSerializeRector.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Expr\ArrayItem;
1010
use PhpParser\Node\Expr\PropertyFetch;
11+
use PhpParser\Node\Expr\Variable;
1112
use PhpParser\Node\Identifier;
13+
use PhpParser\Node\Scalar\String_;
1214
use PhpParser\Node\Stmt\Class_;
1315
use PhpParser\Node\Stmt\ClassMethod;
1416
use Rector\PhpParser\Node\BetterNodeFinder;
@@ -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)