Skip to content

Commit 636f001

Browse files
committed
allow empty in StrictArrayParamDimFetchRector to make rule more practical
1 parent 0279c00 commit 636f001

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
140140
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
141141
}
142142

143-
if ($this->shouldStop($node, $param, $paramName)) {
143+
if ($this->shouldStop($node, $paramName)) {
144144
// force set to false to avoid too early replaced
145145
$isParamAccessedArrayDimFetch = false;
146146
return NodeVisitor::STOP_TRAVERSAL;
@@ -209,16 +209,11 @@ private function isEchoed(Node $node, string $paramName): bool
209209
return false;
210210
}
211211

212-
private function shouldStop(Node $node, Param $param, string $paramName): bool
212+
private function shouldStop(Node $node, string $paramName): bool
213213
{
214214
$nodeToCheck = null;
215215

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()
216+
if ($node instanceof FuncCall && ! $node->isFirstClassCallable()
222217
&& $this->isNames($node, ['is_array', 'is_string', 'is_int', 'is_bool', 'is_float'])) {
223218
$firstArg = $node->getArgs()[0];
224219
$nodeToCheck = $firstArg->value;

0 commit comments

Comments
 (0)