diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/strip_empty_combine.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/strip_empty_combine.php.inc new file mode 100644 index 00000000000..99b4b5c8fc4 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/strip_empty_combine.php.inc @@ -0,0 +1,78 @@ + false, + 'b' => 'b', + 'c' => true, + 'd' => $this->getNullable(), + 'class' => StripEmptyCombine::class, + 'opt' => [], + 'init' => false, + 'attrs' => [ + 'x' => 'x', + 'class' => 'pick', + 'apply' => 'true', + 'head' => $this->trans('foo'), + ], + ]; + } + + private function getNullable(): ?string + { + return null; + } + + function trans(): string + { + return 'foo'; + } +} + +?> +----- +|null> + */ + public function get(): array + { + return [ + 'a' => false, + 'b' => 'b', + 'c' => true, + 'd' => $this->getNullable(), + 'class' => StripEmptyCombine::class, + 'opt' => [], + 'init' => false, + 'attrs' => [ + 'x' => 'x', + 'class' => 'pick', + 'apply' => 'true', + 'head' => $this->trans('foo'), + ], + ]; + } + + private function getNullable(): ?string + { + return null; + } + + function trans(): string + { + return 'foo'; + } +} + +?> diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php index 339745d66ef..e900d9e01eb 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php @@ -13,6 +13,9 @@ use PhpParser\Node\NullableType; use PhpParser\Node\UnionType as PhpParserUnionType; use PHPStan\PhpDocParser\Ast\Type\TypeNode; +use PHPStan\Type\ArrayType; +use PHPStan\Type\MixedType; +use PHPStan\Type\NeverType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode; @@ -55,6 +58,10 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode { $unionTypesNodes = []; foreach ($type->getTypes() as $unionedType) { + if ($unionedType instanceof ArrayType && $unionedType->getItemType() instanceof NeverType) { + $unionedType = new ArrayType($unionedType->getKeyType(), new MixedType()); + } + $unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType); }