From 893bd02b63c4f0a60d8398a843099492dbd06aee Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 20:11:35 +0700 Subject: [PATCH 1/3] [TypeDeclarationDocblocks] Handle with string key on AddReturnDocblockForCommonObjectDenominatorRector --- .../Fixture/with_string_key.php.inc | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/with_string_key.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/with_string_key.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/with_string_key.php.inc new file mode 100644 index 00000000000..fc6e7ecd09d --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/with_string_key.php.inc @@ -0,0 +1,42 @@ + new FirstExtension(), + 'b' => new SecondExtension() + ]; + } +} + +?> +----- + + */ + public function getExtensions(): array + { + return [ + 'a' => new FirstExtension(), + 'b' => new SecondExtension() + ]; + } +} + +?> From 6b60acbb44ad4a823cb913114d5c2153fa856b65 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 20:28:01 +0700 Subject: [PATCH 2/3] Fix --- ...cblockForCommonObjectDenominatorRector.php | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php index b699c423dbc..1369b4eaeb6 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php @@ -12,7 +12,11 @@ use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; +use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; +use PHPStan\Type\StringType; +use PHPStan\Type\Type; +use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\Rector\AbstractRector; @@ -30,7 +34,7 @@ public function __construct( private readonly PhpDocInfoFactory $phpDocInfoFactory, private readonly ReturnNodeFinder $returnNodeFinder, private readonly ReflectionProvider $reflectionProvider, - private readonly PhpDocTypeChanger $phpDocTypeChanger, + private readonly PhpDocTypeChanger $phpDocTypeChanger ) { } @@ -167,7 +171,10 @@ public function refactor(Node $node): ?Node return null; } - $objectTypeArrayType = new ArrayType(new MixedType(), new FullyQualifiedObjectType($firstSharedType)); + $objectTypeArrayType = new ArrayType( + $this->resolveKeyType($returnedType), + new FullyQualifiedObjectType($firstSharedType) + ); $hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $objectTypeArrayType); if (! $hasChanged) { return null; @@ -176,6 +183,36 @@ public function refactor(Node $node): ?Node return $node; } + /** + * @return UnionType|IntegerType|StringType|MixedType + */ + private function resolveKeyType(ConstantArrayType $returnedType): Type + { + $keyType = $returnedType->getKeyType(); + + if ($keyType instanceof UnionType) { + $types = []; + foreach ($keyType->getTypes() as $type) { + if ($type->isString()->yes()) { + $types[] = new StringType(); + } elseif ($type->isInteger()->yes()) { + $types[] = new IntegerType(); + } else { + return new MixedType(); + } + } + + $uniqueKeyTypes = array_unique($types, SORT_REGULAR); + if (count($uniqueKeyTypes) === 1) { + return $uniqueKeyTypes[0]; + } + + return new UnionType($uniqueKeyTypes); + } + + return new MixedType(); + } + /** * @return string[] */ From dc85332d000cc914d853402b14ed15af1da044bb Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 12 Sep 2025 13:30:35 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../AddReturnDocblockForCommonObjectDenominatorRector.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php index 1369b4eaeb6..b12e0b160d4 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php @@ -186,9 +186,9 @@ public function refactor(Node $node): ?Node /** * @return UnionType|IntegerType|StringType|MixedType */ - private function resolveKeyType(ConstantArrayType $returnedType): Type + private function resolveKeyType(ConstantArrayType $constantArrayType): Type { - $keyType = $returnedType->getKeyType(); + $keyType = $constantArrayType->getKeyType(); if ($keyType instanceof UnionType) { $types = [];