Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension;
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension;

final class WithStringKey
{
public function getExtensions(): array
{
return [
'a' => new FirstExtension(),
'b' => new SecondExtension()
];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension;
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension;

final class WithStringKey
{
/**
* @return array<string, \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\ExtensionInterface>
*/
public function getExtensions(): array
{
return [
'a' => new FirstExtension(),
'b' => new SecondExtension()
];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
Expand All @@ -176,6 +183,36 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @return UnionType|IntegerType|StringType|MixedType
*/
private function resolveKeyType(ConstantArrayType $constantArrayType): Type
{
$keyType = $constantArrayType->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[]
*/
Expand Down
Loading