Skip to content

Commit 21b6091

Browse files
[CodeQuality] Handle property fetch and common nodes crash on InlineArrayReturnAssignRector (#7437)
* [CodeQuality] Handle property fetch and common nodes crash on InlineArrayReturnAssignRector * [CodeQuality] Handle property fetch and common nodes crash on InlineArrayReturnAssignRector * fix rectify * [ci-review] Rector Rectify * fix rectify * fix * fix phpstan --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 24b76ca commit 21b6091

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

rector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
66
use Rector\Config\RectorConfig;
7+
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
78
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
89
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
910

@@ -52,4 +53,9 @@
5253
__DIR__ . '/src/Configuration/RectorConfigBuilder.php',
5354
__DIR__ . '/src/Console/Notifier.php',
5455
],
56+
57+
// todo: properly handle, substr() can return false on php 7.x
58+
RecastingRemovalRector::class => [
59+
__DIR__ . '/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php',
60+
],
5561
]);
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+
?>

rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
4646
$namespace = strtolower((string) $namespace);
4747

4848
$shortNameLowered = $fullyQualifiedObjectType->getShortNameLowered();
49-
$fullyQualifiedObjectTypeNamespace = strtolower(
50-
substr($fullyQualifiedObjectType->getClassName(), 0, -strlen($fullyQualifiedObjectType->getShortName()) - 1) ?: ''
51-
);
49+
$subClassName = substr($fullyQualifiedObjectType->getClassName(), 0, -strlen($fullyQualifiedObjectType->getShortName()) - 1);
50+
$fullyQualifiedObjectTypeNamespace = strtolower((string) $subClassName);
5251

5352
foreach ($classLikeNames as $classLikeName) {
5453
if (strtolower($classLikeName) !== $shortNameLowered) {

src/PhpParser/Node/NodeFactory.php

Lines changed: 23 additions & 7 deletions
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;
@@ -44,7 +48,6 @@
4448
use PHPStan\Type\Type;
4549
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
4650
use Rector\Enum\ObjectReference;
47-
use Rector\Exception\NotImplementedYetException;
4851
use Rector\Exception\ShouldNotHappenException;
4952
use Rector\NodeDecorator\PropertyTypeDecorator;
5053
use Rector\NodeTypeResolver\Node\AttributeKey;
@@ -377,6 +380,12 @@ private function createArrayItem(mixed $item, string | int | null $key = null):
377380
|| $item instanceof Scalar
378381
|| $item instanceof Cast
379382
|| $item instanceof ConstFetch
383+
|| $item instanceof PropertyFetch
384+
|| $item instanceof StaticPropertyFetch
385+
|| $item instanceof NullsafePropertyFetch
386+
|| $item instanceof NullsafeMethodCall
387+
|| $item instanceof Clone_
388+
|| $item instanceof Instanceof_
380389
) {
381390
$arrayItem = new ArrayItem($item);
382391
} elseif ($item instanceof Identifier) {
@@ -411,12 +420,19 @@ private function createArrayItem(mixed $item, string | int | null $key = null):
411420
return $arrayItem;
412421
}
413422

414-
$nodeClass = is_object($item) ? $item::class : $item;
415-
throw new NotImplementedYetException(sprintf(
416-
'Not implemented yet. Go to "%s()" and add check for "%s" node.',
417-
__METHOD__,
418-
(string) $nodeClass
419-
));
423+
// fallback to other nodes
424+
if ($item instanceof Expr) {
425+
$arrayItem = new ArrayItem($item);
426+
$this->decorateArrayItemWithKey($key, $arrayItem);
427+
428+
return $arrayItem;
429+
}
430+
431+
$itemValue = BuilderHelpers::normalizeValue($item);
432+
$arrayItem = new ArrayItem($itemValue);
433+
$this->decorateArrayItemWithKey($key, $arrayItem);
434+
435+
return $arrayItem;
420436
}
421437

422438
private function decorateArrayItemWithKey(int | string | null $key, ArrayItem $arrayItem): void

0 commit comments

Comments
 (0)