mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
Merge pull request #2646 from rectorphp/fix-var-const-unions
Fix Union Array type StaticTypeMapper to string
This commit is contained in:
commit
5d71630ae6
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;
|
||||
|
||||
class UnionNestedAndSingleLevelArrayScalars
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
public const ARRAY_CONST = [
|
||||
'key2' => ['value2', 1234],
|
||||
1 => null,
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;
|
||||
|
||||
class UnionNestedAndSingleLevelArrayScalars
|
||||
{
|
||||
/**
|
||||
* @var int[][]|string[][]|null[]
|
||||
*/
|
||||
public const ARRAY_CONST = [
|
||||
'key2' => ['value2', 1234],
|
||||
1 => null,
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;
|
||||
|
||||
class UnionNestedArrayScalars
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
public const STRING_ONLY = [
|
||||
'key' => 'value',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
public const NESTED_STRING_INT = [
|
||||
'key2' => ['value2', 1234],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
public const STRING_AND_NULL = [
|
||||
'key' => 'value',
|
||||
1 => null,
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;
|
||||
|
||||
class UnionNestedArrayScalars
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public const STRING_ONLY = [
|
||||
'key' => 'value',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int[][]|string[][]
|
||||
*/
|
||||
public const NESTED_STRING_INT = [
|
||||
'key2' => ['value2', 1234],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]|null[]
|
||||
*/
|
||||
public const STRING_AND_NULL = [
|
||||
'key' => 'value',
|
||||
1 => null,
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
@ -114,6 +114,11 @@ final class StaticTypeMapper
|
||||
|
||||
if ($phpStanType instanceof ArrayType || $phpStanType instanceof IterableType) {
|
||||
$itemTypeNode = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($phpStanType->getItemType());
|
||||
|
||||
if ($itemTypeNode instanceof UnionTypeNode) {
|
||||
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
|
||||
}
|
||||
|
||||
return new ArrayTypeNode($itemTypeNode);
|
||||
}
|
||||
|
||||
@ -868,4 +873,24 @@ final class StaticTypeMapper
|
||||
|
||||
return is_a($secondType->getClassName(), $firstType->getClassName(), true);
|
||||
}
|
||||
|
||||
private function convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes(
|
||||
UnionTypeNode $unionTypeNode
|
||||
): AttributeAwareUnionTypeNode {
|
||||
$unionedArrayType = [];
|
||||
foreach ($unionTypeNode->types as $unionedType) {
|
||||
if ($unionedType instanceof UnionTypeNode) {
|
||||
foreach ($unionedType->types as $key => $subUnionedType) {
|
||||
$unionedType->types[$key] = new ArrayTypeNode($subUnionedType);
|
||||
}
|
||||
|
||||
$unionedArrayType[] = $unionedType;
|
||||
continue;
|
||||
}
|
||||
|
||||
$unionedArrayType[] = new ArrayTypeNode($unionedType);
|
||||
}
|
||||
|
||||
return new AttributeAwareUnionTypeNode($unionedArrayType);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user