Skip to content

Commit f4003bb

Browse files
authored
[CodeQuality] Handle crash on UnaryMinus/UnaryPlus on InlineArrayReturnAssignRector (#7277)
* [CodeQuality] Handle crash on UnaryMinus/UnaryPlus on InlineArrayReturnAssignRector * Fix
1 parent 8d84db7 commit f4003bb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class WithUnary
6+
{
7+
public function run()
8+
{
9+
$data[0]['date'] = '2025-09-15';
10+
$data[0]['plus'] = +50000;
11+
$data[1]['minus'] = -67500;
12+
return $data;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
21+
22+
final class WithUnary
23+
{
24+
public function run()
25+
{
26+
return [['date' => '2025-09-15', 'plus' => +50000], ['minus' => -67500]];
27+
}
28+
}
29+
30+
?>

src/PhpParser/Node/NodeFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use PhpParser\Node\Expr\PropertyFetch;
3131
use PhpParser\Node\Expr\StaticCall;
3232
use PhpParser\Node\Expr\StaticPropertyFetch;
33+
use PhpParser\Node\Expr\UnaryMinus;
34+
use PhpParser\Node\Expr\UnaryPlus;
3335
use PhpParser\Node\Expr\Variable;
3436
use PhpParser\Node\Identifier;
3537
use PhpParser\Node\Name;
@@ -403,6 +405,12 @@ private function createArrayItem(mixed $item, string | int | null $key = null):
403405
return $arrayItem;
404406
}
405407

408+
if ($item instanceof UnaryPlus || $item instanceof UnaryMinus) {
409+
$arrayItem = new ArrayItem($item);
410+
$this->decorateArrayItemWithKey($key, $arrayItem);
411+
return $arrayItem;
412+
}
413+
406414
$nodeClass = is_object($item) ? $item::class : $item;
407415
throw new NotImplementedYetException(sprintf(
408416
'Not implemented yet. Go to "%s()" and add check for "%s" node.',

0 commit comments

Comments
 (0)