Skip to content

Commit 3b17178

Browse files
authored
allow empty in StrictArrayParamDimFetchRector to make rule more practical (#7429)
1 parent 0279c00 commit 3b17178

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
4+
5+
final class EmptyIndex
6+
{
7+
public function run($param): void
8+
{
9+
if (! empty($param['index'])) {
10+
$index = $param['index'];
11+
} else {
12+
$index = 100;
13+
}
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
22+
23+
final class EmptyIndex
24+
{
25+
public function run(array $param): void
26+
{
27+
if (! empty($param['index'])) {
28+
$index = $param['index'];
29+
} else {
30+
$index = 100;
31+
}
32+
}
33+
}
34+
35+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_empty_index.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,14 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
132132

133133
$isParamAccessedArrayDimFetch = false;
134134
$this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use (
135-
$param,
136135
$paramName,
137136
&$isParamAccessedArrayDimFetch,
138137
): int|null {
139138
if ($node instanceof Class_ || $node instanceof FunctionLike) {
140139
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
141140
}
142141

143-
if ($this->shouldStop($node, $param, $paramName)) {
142+
if ($this->shouldStop($node, $paramName)) {
144143
// force set to false to avoid too early replaced
145144
$isParamAccessedArrayDimFetch = false;
146145
return NodeVisitor::STOP_TRAVERSAL;
@@ -209,16 +208,11 @@ private function isEchoed(Node $node, string $paramName): bool
209208
return false;
210209
}
211210

212-
private function shouldStop(Node $node, Param $param, string $paramName): bool
211+
private function shouldStop(Node $node, string $paramName): bool
213212
{
214213
$nodeToCheck = null;
215214

216-
if (! $param->default instanceof Expr && ($node instanceof Empty_ && $node->expr instanceof ArrayDimFetch && $node->expr->var instanceof Variable && $node->expr->var->name === $paramName)) {
217-
return true;
218-
}
219-
220-
if ($node instanceof FuncCall
221-
&& ! $node->isFirstClassCallable()
215+
if ($node instanceof FuncCall && ! $node->isFirstClassCallable()
222216
&& $this->isNames($node, ['is_array', 'is_string', 'is_int', 'is_bool', 'is_float'])) {
223217
$firstArg = $node->getArgs()[0];
224218
$nodeToCheck = $firstArg->value;

0 commit comments

Comments
 (0)