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

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

final class StripEmptyCombine
{
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';
}
}

?>
-----
<?php

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

final class StripEmptyCombine
{
/**
* @return array<string, string|bool|mixed[]|array<string, string>|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';
}
}

?>
7 changes: 7 additions & 0 deletions src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Loading