Skip to content

Commit 5eae089

Browse files
committed
skip as array dim fetch dim
1 parent 9924dcf commit 5eae089

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipAsArrayDimFetchDim
6+
{
7+
public function run()
8+
{
9+
$data[substr('a', 2)] = 'foo';
10+
var_dump($data);
11+
}
12+
}

rules/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\ArrayItem;
1010
use PhpParser\Node\Expr;
11+
use PhpParser\Node\Expr\ArrayDimFetch;
1112
use PhpParser\Node\Expr\AssignOp;
1213
use PhpParser\Node\Expr\BinaryOp\Concat;
1314
use PhpParser\Node\Expr\BinaryOp\Identical;
@@ -79,12 +80,13 @@ public function getNodeTypes(): array
7980
While_::class,
8081
Do_::class,
8182
ArrayItem::class,
83+
ArrayDimFetch::class,
8284
FuncCall::class,
8385
];
8486
}
8587

8688
/**
87-
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_|While_|Do_|ArrayItem|FuncCall $node
89+
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_|While_|Do_|ArrayItem|ArrayDimFetch|FuncCall $node
8890
*/
8991
public function refactor(Node $node): ?Node
9092
{
@@ -132,6 +134,14 @@ public function refactor(Node $node): ?Node
132134
return null;
133135
}
134136

137+
if ($node instanceof ArrayDimFetch) {
138+
if ($node->dim instanceof Expr) {
139+
$node->dim->setAttribute(self::IS_UNCASTABLE, true);
140+
}
141+
142+
return null;
143+
}
144+
135145
if ($node instanceof CallLike) {
136146
if ($node->isFirstClassCallable()) {
137147
return null;

0 commit comments

Comments
 (0)