Skip to content

Commit c648be5

Browse files
committed
[CodeQuality] Handle property fetch and common nodes crash on InlineArrayReturnAssignRector
1 parent 24b76ca commit c648be5

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class WithPropertyFetch
6+
{
7+
private string $name = 'test';
8+
9+
public function foo($obj): array
10+
{
11+
$event['name'] = $this->name;
12+
$event['payload'] = $this->bar();
13+
$event['data'] = $obj?->data;
14+
$event['item'] = $obj?->getItem();
15+
$event['list'] = clone $obj;
16+
$event['verify'] = $obj instanceof \stdClass;
17+
18+
return $event;
19+
}
20+
21+
private function bar(): array
22+
{
23+
return [];
24+
}
25+
}
26+
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
32+
33+
final class WithPropertyFetch
34+
{
35+
private string $name = 'test';
36+
37+
public function foo($obj): array
38+
{
39+
return ['name' => $this->name, 'payload' => $this->bar(), 'data' => $obj?->data, 'item' => $obj?->getItem(), 'list' => clone $obj, 'verify' => $obj instanceof \stdClass];
40+
}
41+
42+
private function bar(): array
43+
{
44+
return [];
45+
}
46+
}
47+
48+
?>

src/PhpParser/Node/NodeFactory.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
2424
use PhpParser\Node\Expr\Cast;
2525
use PhpParser\Node\Expr\ClassConstFetch;
26+
use PhpParser\Node\Expr\Clone_;
2627
use PhpParser\Node\Expr\ConstFetch;
2728
use PhpParser\Node\Expr\FuncCall;
29+
use PhpParser\Node\Expr\Instanceof_;
2830
use PhpParser\Node\Expr\MethodCall;
2931
use PhpParser\Node\Expr\New_;
32+
use PhpParser\Node\Expr\NullsafeMethodCall;
33+
use PhpParser\Node\Expr\NullsafePropertyFetch;
3034
use PhpParser\Node\Expr\PropertyFetch;
3135
use PhpParser\Node\Expr\StaticCall;
3236
use PhpParser\Node\Expr\StaticPropertyFetch;
@@ -377,6 +381,12 @@ private function createArrayItem(mixed $item, string | int | null $key = null):
377381
|| $item instanceof Scalar
378382
|| $item instanceof Cast
379383
|| $item instanceof ConstFetch
384+
|| $item instanceof PropertyFetch
385+
|| $item instanceof StaticPropertyFetch
386+
|| $item instanceof NullsafePropertyFetch
387+
|| $item instanceof NullsafeMethodCall
388+
|| $item instanceof Clone_
389+
|| $item instanceof Instanceof_
380390
) {
381391
$arrayItem = new ArrayItem($item);
382392
} elseif ($item instanceof Identifier) {
@@ -411,7 +421,12 @@ private function createArrayItem(mixed $item, string | int | null $key = null):
411421
return $arrayItem;
412422
}
413423

414-
$nodeClass = is_object($item) ? $item::class : $item;
424+
// fallback to other nodes
425+
if (is_object($item)) {
426+
return new ArrayItem($item);
427+
}
428+
429+
$nodeClass = $item;
415430
throw new NotImplementedYetException(sprintf(
416431
'Not implemented yet. Go to "%s()" and add check for "%s" node.',
417432
__METHOD__,

0 commit comments

Comments
 (0)