Skip to content

Commit f6be3a5

Browse files
committed
[Php85] Skip inside expression assign on ArrayFirstLastRector
1 parent 2521e2e commit f6be3a5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;
6+
7+
final class SkipAsVarAssigned
8+
{
9+
public function run()
10+
{
11+
$myArray = [
12+
'test'=> ['label'=> ['show'=> false]]
13+
];
14+
15+
$myArray[array_key_last($myArray)]['label']['show'] = true;
16+
var_dump($myArray);
17+
}
18+
}

rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\ArrayDimFetch;
99
use PhpParser\Node\Expr\FuncCall;
10+
use Rector\PHPStan\ScopeFetcher;
1011
use Rector\Rector\AbstractRector;
1112
use Rector\ValueObject\PhpVersionFeature;
1213
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -82,6 +83,11 @@ public function refactor(Node $node): ?FuncCall
8283
return null;
8384
}
8485

86+
$scope = ScopeFetcher::fetch($node->var);
87+
if ($scope->isInExpressionAssign($node)) {
88+
return null;
89+
}
90+
8591
$functionName = $this->isName($node->dim, self::ARRAY_KEY_FIRST)
8692
? 'array_first'
8793
: 'array_last';

0 commit comments

Comments
 (0)