From 7d9853fe2874dfa2f32f34a38bd8232fc06e9b27 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Sep 2025 12:01:37 +0200 Subject: [PATCH 1/4] add fixture --- .../Fixture/override_bare_array.php.inc | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/override_bare_array.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/override_bare_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/override_bare_array.php.inc new file mode 100644 index 00000000000..4f150b2b07c --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/override_bare_array.php.inc @@ -0,0 +1,33 @@ + [true], + 'second' => [false], + ]; +} + +?> +----- + + */ +function overrideBareArray() +{ + return [ + 'first' => [true], + 'second' => [false], + ]; +} + +?> From 68117dc10d2f1b879575d6c54a2983089ee571c6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Sep 2025 12:08:01 +0200 Subject: [PATCH 2/4] change existing dummy array --- ...turnArrayFromDirectArrayInstanceRector.php | 22 +++++++--------- .../UsefulArrayTagNodeAnalyzer.php | 25 +++++++++++++++++++ .../PhpDocManipulator/PhpDocTypeChanger.php | 16 ++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 rules/TypeDeclarationDocblocks/TagNodeAnalyzer/UsefulArrayTagNodeAnalyzer.php diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php index a7dfae6c955..4fc92951360 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php @@ -9,12 +9,12 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use PHPStan\Type\Constant\ConstantArrayType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\Comments\NodeDocBlock\DocBlockUpdater; +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Rector\AbstractRector; use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; use Rector\TypeDeclarationDocblocks\TypeResolver\ConstantArrayTypeGeneralizer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -26,9 +26,10 @@ final class DocblockReturnArrayFromDirectArrayInstanceRector extends AbstractRec { public function __construct( private readonly PhpDocInfoFactory $phpDocInfoFactory, - private readonly DocBlockUpdater $docBlockUpdater, + private readonly PhpDocTypeChanger $phpDocTypeChanger, private readonly ConstantArrayTypeGeneralizer $constantArrayTypeGeneralizer, private readonly ReturnNodeFinder $returnNodeFinder, + private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer ) { } @@ -80,14 +81,13 @@ public function getItems(): array */ public function refactor(Node $node): ?Node { - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - - // return tag is already given - if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { + if ($node->stmts === null) { return null; } - if ($node->stmts === null) { + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) { return null; } @@ -107,11 +107,7 @@ public function refactor(Node $node): ?Node } $genericTypeNode = $this->constantArrayTypeGeneralizer->generalize($returnedType); - - $returnTagValueNode = new ReturnTagValueNode($genericTypeNode, ''); - $phpDocInfo->addTagValueNode($returnTagValueNode); - - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $genericTypeNode); return $node; } diff --git a/rules/TypeDeclarationDocblocks/TagNodeAnalyzer/UsefulArrayTagNodeAnalyzer.php b/rules/TypeDeclarationDocblocks/TagNodeAnalyzer/UsefulArrayTagNodeAnalyzer.php new file mode 100644 index 00000000000..e714fab753b --- /dev/null +++ b/rules/TypeDeclarationDocblocks/TagNodeAnalyzer/UsefulArrayTagNodeAnalyzer.php @@ -0,0 +1,25 @@ +type; + if (! $type instanceof IdentifierTypeNode) { + return true; + } + + return $type->name !== 'array'; + } +} diff --git a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php index 48981c2b83c..8760126acd1 100644 --- a/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php +++ b/src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php @@ -97,6 +97,22 @@ public function changeVarType(Stmt $stmt, PhpDocInfo $phpDocInfo, Type $newType) return true; } + public function changeReturnTypeNode( + FunctionLike $functionLike, + PhpDocInfo $phpDocInfo, + TypeNode $newTypeNode + ): void { + $existingReturnTagValueNode = $phpDocInfo->getReturnTagValue(); + if ($existingReturnTagValueNode instanceof ReturnTagValueNode) { + $existingReturnTagValueNode->type = $newTypeNode; + } else { + $returnTagValueNode = new ReturnTagValueNode($newTypeNode, ''); + $phpDocInfo->addTagValueNode($returnTagValueNode); + } + + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike); + } + public function changeReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo, Type $newType): bool { // better not touch this, can crash From d5d631c7c3733f1c81add5a41a242b9a020af643 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Sep 2025 12:14:50 +0200 Subject: [PATCH 3/4] update DocblockGetterReturnArrayFromPropertyDocblockVarRector too --- .../Fixture/override_dummy_array.php.inc | 37 ++++++++++++++++ .../Fixture/override_dummy_array.php.inc | 43 +++++++++++++++++++ .../AddReturnDocblockForJsonArrayRector.php | 14 +++--- ...turnArrayFromPropertyDocblockVarRector.php | 25 +++++++---- 4 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/override_dummy_array.php.inc create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/override_dummy_array.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/override_dummy_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/override_dummy_array.php.inc new file mode 100644 index 00000000000..f97f8ff2d72 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/override_dummy_array.php.inc @@ -0,0 +1,37 @@ + +----- + + */ + public function provide(string $contents): array + { + return json_decode($contents, true); + } +} + +?> diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/override_dummy_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/override_dummy_array.php.inc new file mode 100644 index 00000000000..9e95c3cb443 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector/Fixture/override_dummy_array.php.inc @@ -0,0 +1,43 @@ +names; + } +} + +?> +----- +names; + } +} + +?> diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php index 29d32ec4606..2351f65b957 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php @@ -12,7 +12,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Return_; -use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; use PHPStan\Type\StringType; @@ -22,6 +21,7 @@ use Rector\Rector\AbstractRector; use Rector\TypeDeclarationDocblocks\Enum\NetteClassName; use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -34,7 +34,8 @@ public function __construct( private readonly PhpDocInfoFactory $phpDocInfoFactory, private readonly ReturnNodeFinder $returnNodeFinder, private readonly PhpDocTypeChanger $phpDocTypeChanger, - private readonly ValueResolver $valueResolver + private readonly ValueResolver $valueResolver, + private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer ) { } @@ -85,11 +86,6 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $returnType = $phpDocInfo->getReturnType(); - - if (! $returnType instanceof MixedType || $returnType->isExplicitMixed()) { - return null; - } // definitely not an array return if ($node->returnType instanceof Node && ! $this->isName($node->returnType, 'array')) { @@ -111,8 +107,8 @@ public function refactor(Node $node): ?Node } $classMethodDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - // already filled - if ($classMethodDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { + + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($classMethodDocInfo->getReturnTagValue())) { return null; } diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php index 11af067110b..753aca29bab 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php @@ -8,14 +8,14 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; -use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use Rector\Comments\NodeDocBlock\DocBlockUpdater; +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -26,8 +26,9 @@ final class DocblockGetterReturnArrayFromPropertyDocblockVarRector extends Abstr { public function __construct( private readonly PhpDocInfoFactory $phpDocInfoFactory, - private readonly DocBlockUpdater $docBlockUpdater, - private readonly StaticTypeMapper $staticTypeMapper + private readonly StaticTypeMapper $staticTypeMapper, + private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer, + private readonly PhpDocTypeChanger $phpDocTypeChanger ) { } @@ -91,11 +92,15 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - // return tag is already given - if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) { return null; } + // // return tag is already given + // if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { + // return null; + // } + $propertyFetch = $this->matchReturnLocalPropertyFetch($node); if (! $propertyFetch instanceof PropertyFetch) { return null; @@ -115,10 +120,12 @@ public function refactor(Node $node): ?Node $propertyFetchDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($propertyFetchType); - $returnTagValueNode = new ReturnTagValueNode($propertyFetchDocTypeNode, ''); - $phpDocInfo->addTagValueNode($returnTagValueNode); + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $propertyFetchDocTypeNode); - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); + // $returnTagValueNode = new ReturnTagValueNode($propertyFetchDocTypeNode, ''); + // $phpDocInfo->addTagValueNode($returnTagValueNode); + // + // $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); return $node; } From 69a289240c1c4d1b2b6b732545d6a5b6fe20f2f2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Sep 2025 12:22:53 +0200 Subject: [PATCH 4/4] add same support to AddReturnDocblockForCommonObjectDenominatorRector --- .../Fixture/override_dummy_array.php.inc | 47 +++++++++ .../Fixture/override_dummy_array.php.inc | 45 +++++++++ ...ocblockForArrayDimAssignedObjectRector.php | 99 ++++++++++--------- ...cblockForCommonObjectDenominatorRector.php | 3 +- 4 files changed, 149 insertions(+), 45 deletions(-) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector/Fixture/override_dummy_array.php.inc create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/override_dummy_array.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector/Fixture/override_dummy_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector/Fixture/override_dummy_array.php.inc new file mode 100644 index 00000000000..e4f03c77039 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector/Fixture/override_dummy_array.php.inc @@ -0,0 +1,47 @@ + +----- + diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/override_dummy_array.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/override_dummy_array.php.inc new file mode 100644 index 00000000000..656e45cba58 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/override_dummy_array.php.inc @@ -0,0 +1,45 @@ + +----- + diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php index 44045adb972..c46ae24726e 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForArrayDimAssignedObjectRector.php @@ -23,8 +23,10 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Rector\AbstractRector; +use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType; use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -37,6 +39,8 @@ public function __construct( private readonly PhpDocInfoFactory $phpDocInfoFactory, private readonly ReturnNodeFinder $returnNodeFinder, private readonly PhpDocTypeChanger $phpDocTypeChanger, + private readonly StaticTypeMapper $staticTypeMapper, + private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer ) { } @@ -101,7 +105,11 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); $returnType = $phpDocInfo->getReturnType(); - if (! $returnType instanceof MixedType || $returnType->isExplicitMixed()) { + if ($returnType instanceof ArrayType && ! $returnType->getItemType() instanceof MixedType) { + return null; + } + + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) { return null; } @@ -124,10 +132,49 @@ public function refactor(Node $node): ?Node return null; } + if ($this->isVariableExclusivelyArrayDimAssigned($node, $returnedVariableName) === false) { + return null; + } + + $arrayObjectType = $this->matchArrayObjectType($returnedType); + if (! $arrayObjectType instanceof ObjectType) { + return null; + } + + $objectTypeArrayType = new ArrayType(new MixedType(), $arrayObjectType); + $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($objectTypeArrayType); + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $returnTypeNode); + + return $node; + } + + private function matchArrayObjectType(Type $returnedType): ?Type + { + if ($returnedType instanceof IntersectionType) { + foreach ($returnedType->getTypes() as $intersectionedType) { + if ($intersectionedType instanceof AccessoryArrayListType) { + continue; + } + + if ($intersectionedType instanceof ArrayType && $intersectionedType->getItemType() instanceof ObjectType) { + return $intersectionedType->getItemType(); + } + + return null; + } + } + + return null; + } + + private function isVariableExclusivelyArrayDimAssigned( + ClassMethod|Function_ $functionLike, + string $variableName + ): bool { $isVariableExclusivelyArrayDimAssigned = true; - $this->traverseNodesWithCallable((array) $node->stmts, function ($node) use ( - $returnedVariableName, + $this->traverseNodesWithCallable((array) $functionLike->stmts, function ($node) use ( + $variableName, &$isVariableExclusivelyArrayDimAssigned ): ?int { if ($node instanceof Assign) { @@ -139,7 +186,7 @@ public function refactor(Node $node): ?Node return null; } - if ($this->isName($arrayDimFetch->var, $returnedVariableName)) { + if ($this->isName($arrayDimFetch->var, $variableName)) { if ($arrayDimFetch->dim instanceof Expr) { $isVariableExclusivelyArrayDimAssigned = false; } @@ -160,7 +207,7 @@ public function refactor(Node $node): ?Node if ($node->var instanceof Variable && $this->isName( $node->var, - $returnedVariableName + $variableName ) && $node->expr instanceof Array_) { if ($node->expr->items === []) { // ignore empty array assignment @@ -172,7 +219,7 @@ public function refactor(Node $node): ?Node } if ($node instanceof Return_ && $node->expr instanceof Variable) { - if ($this->isName($node->expr, $returnedVariableName)) { + if ($this->isName($node->expr, $variableName)) { // ignore lower value return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; } @@ -180,49 +227,13 @@ public function refactor(Node $node): ?Node $isVariableExclusivelyArrayDimAssigned = false; } - if ($node instanceof Variable && $this->isName($node, $returnedVariableName)) { + if ($node instanceof Variable && $this->isName($node, $variableName)) { $isVariableExclusivelyArrayDimAssigned = false; } return null; }); - if ($isVariableExclusivelyArrayDimAssigned === false) { - return null; - } - - $arrayObjectType = $this->matchArrayObjectType($returnedType); - if (! $arrayObjectType instanceof ObjectType) { - return null; - } - - //$arrayReturnDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($arrayObjectType); - - $objectTypeArrayType = new ArrayType(new MixedType(), $arrayObjectType); - $hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $objectTypeArrayType); - if (! $hasChanged) { - return null; - } - - return $node; - } - - private function matchArrayObjectType(Type $returnedType): ?Type - { - if ($returnedType instanceof IntersectionType) { - foreach ($returnedType->getTypes() as $intersectionedType) { - if ($intersectionedType instanceof AccessoryArrayListType) { - continue; - } - - if ($intersectionedType instanceof ArrayType && $intersectionedType->getItemType() instanceof ObjectType) { - return $intersectionedType->getItemType(); - } - - return null; - } - } - - return null; + return $isVariableExclusivelyArrayDimAssigned; } } diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php index b12e0b160d4..b9f425a050c 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php @@ -110,7 +110,7 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); $returnType = $phpDocInfo->getReturnType(); - if (! $returnType instanceof MixedType || $returnType->isExplicitMixed()) { + if ($returnType instanceof ArrayType && ! $returnType->getItemType() instanceof MixedType) { return null; } @@ -175,6 +175,7 @@ public function refactor(Node $node): ?Node $this->resolveKeyType($returnedType), new FullyQualifiedObjectType($firstSharedType) ); + $hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $objectTypeArrayType); if (! $hasChanged) { return null;