Skip to content

Commit 9924dcf

Browse files
committed
skip as array key
1 parent cb6819b commit 9924dcf

File tree

2 files changed

+26
-1
lines changed

2 files changed

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

rules/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\ArrayItem;
910
use PhpParser\Node\Expr;
1011
use PhpParser\Node\Expr\AssignOp;
1112
use PhpParser\Node\Expr\BinaryOp\Concat;
@@ -77,12 +78,13 @@ public function getNodeTypes(): array
7778
If_::class,
7879
While_::class,
7980
Do_::class,
81+
ArrayItem::class,
8082
FuncCall::class,
8183
];
8284
}
8385

8486
/**
85-
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_|While_|Do_|FuncCall $node
87+
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_|While_|Do_|ArrayItem|FuncCall $node
8688
*/
8789
public function refactor(Node $node): ?Node
8890
{
@@ -122,6 +124,14 @@ public function refactor(Node $node): ?Node
122124
return null;
123125
}
124126

127+
if ($node instanceof ArrayItem) {
128+
if ($node->key instanceof Expr) {
129+
$node->key->setAttribute(self::IS_UNCASTABLE, true);
130+
}
131+
132+
return null;
133+
}
134+
125135
if ($node instanceof CallLike) {
126136
if ($node->isFirstClassCallable()) {
127137
return null;

0 commit comments

Comments
 (0)