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,48 @@
<?php

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

final class TwoNestedLevels
{
public function run(): array
{
return [
'key' => [
'id' => 1,
'name' => '111',
],
'another_key' => [
'id' => 1,
'name' => '111',
],
];
}
}

?>
-----
<?php

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

final class TwoNestedLevels
{
/**
* @return array<string, array<string, int|string>>
*/
public function run(): array
{
return [
'key' => [
'id' => 1,
'name' => '111',
],
'another_key' => [
'id' => 1,
'name' => '111',
],
];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ private function isEnumCase(ClassReflection $classReflection, string $name, stri
if ($classReflection->hasEnumCase($name)) {
return true;
}

return $classReflection->hasEnumCase($pascalName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -35,7 +38,8 @@ final class DocblockReturnArrayFromDirectArrayInstanceRector extends AbstractRec
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly StaticTypeMapper $staticTypeMapper
private readonly StaticTypeMapper $staticTypeMapper,
private readonly TypeFactory $typeFactory
) {
}

Expand Down Expand Up @@ -145,6 +149,20 @@ private function constantToGenericType(Type $type): Type
return new FloatType();
}

if ($type instanceof UnionType || $type instanceof IntersectionType) {
$genericComplexTypes = [];
foreach ($type->getTypes() as $splitType) {
$genericComplexTypes[] = $this->constantToGenericType($splitType);
}

$genericComplexTypes = $this->typeFactory->uniquateTypes($genericComplexTypes);
if (count($genericComplexTypes) > 1) {
return new UnionType($genericComplexTypes);
}

return $genericComplexTypes[0];
}

// unclear
return new MixedType();
}
Expand Down
Loading