From 10efc61d2d48c0049da4c2ea32f4396b7ee02a21 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 1 Oct 2025 00:13:20 +0200 Subject: [PATCH 1/2] Fix nested keys assign in InlineArrayReturnAssignRector --- .../Fixture/skip_nested_dim_fetch.php.inc | 21 +++++++++++++++++++ .../Fixture/with_unary.php.inc | 2 +- .../VariableDimFetchAssignResolver.php | 8 ++++++- .../InlineArrayReturnAssignRector.php | 15 +++++++++---- .../PhpDocManipulator/PhpDocTypeChanger.php | 4 ++-- 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_nested_dim_fetch.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_nested_dim_fetch.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_nested_dim_fetch.php.inc new file mode 100644 index 00000000000..cf21a82f5e0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_nested_dim_fetch.php.inc @@ -0,0 +1,21 @@ + 123, + ]; + + $data['info']['nested'] = 123; + + return $data; + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/with_unary.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/with_unary.php.inc index eaa1d7a10d0..cce6690a2ae 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/with_unary.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/with_unary.php.inc @@ -27,4 +27,4 @@ final class WithUnary } } -?> \ No newline at end of file +?> diff --git a/rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php b/rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php index 7c3afedf383..5e64b79acf6 100644 --- a/rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php +++ b/rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php @@ -5,12 +5,14 @@ namespace Rector\CodeQuality\NodeAnalyzer; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use Rector\CodeQuality\ValueObject\KeyAndExpr; +use Rector\Exception\NotImplementedYetException; use Rector\NodeAnalyzer\ExprAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; @@ -86,7 +88,11 @@ private function setNestedKeysExpr(array &$exprsByKeys, array $keys, Expr $expr) $keys = array_reverse($keys); foreach ($keys as $key) { - // create intermediate arrays automatically + if ($reference instanceof Array_) { + // currently it fails here with Cannot use object of type PhpParser\Node\Expr\Array_ as array + throw new NotImplementedYetException(); + } + $reference = &$reference[$key]; } diff --git a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php index 1a915a6156f..bb9dfc361c8 100644 --- a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php @@ -16,6 +16,7 @@ use PhpParser\Node\Stmt\Return_; use Rector\CodeQuality\NodeAnalyzer\VariableDimFetchAssignResolver; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Exception\NotImplementedYetException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -111,12 +112,18 @@ public function refactor(Node $node): ?Node return null; } - $keysAndExprsByKey = $this->variableDimFetchAssignResolver->resolveFromStmtsAndVariable( - $stmts, - $emptyArrayAssign, - ); + try { + $keysAndExprsByKey = $this->variableDimFetchAssignResolver->resolveFromStmtsAndVariable( + $stmts, + $emptyArrayAssign, + ); + } catch (NotImplementedYetException $notImplementedYetException) { + // dim fetch assign of nested arrays is hard to resolve + return null; + } if ($keysAndExprsByKey === []) { + return null; } diff --git a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php index 42acfaf2964..af706e71cda 100644 --- a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php +++ b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php @@ -115,7 +115,7 @@ public function changeReturnTypeNode( } public function changeParamTypeNode( - ClassMethod $functionLike, + ClassMethod $classMethod, PhpDocInfo $phpDocInfo, Param $param, string $paramName, @@ -129,7 +129,7 @@ public function changeParamTypeNode( $phpDocInfo->addTagValueNode($paramTagValueNode); } - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike); + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); } public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool From f6082acee705e589b0f64b5768ec90f00d774460 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 1 Oct 2025 06:59:13 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/ClassMethod/InlineArrayReturnAssignRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php index bb9dfc361c8..36c0a730442 100644 --- a/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php @@ -117,7 +117,7 @@ public function refactor(Node $node): ?Node $stmts, $emptyArrayAssign, ); - } catch (NotImplementedYetException $notImplementedYetException) { + } catch (NotImplementedYetException) { // dim fetch assign of nested arrays is hard to resolve return null; }