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..36c0a730442 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) { + // 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