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
9 changes: 5 additions & 4 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ public static function clearCache(): void

public static function addNull(Type $type): Type
{
$nullType = new NullType();

if ($nullType->isSuperTypeOf($type)->no()) {
return self::union($type, $nullType);
// asking the type itself is both cheaper than NullType::isSuperTypeOf()
// (UnionType memoizes isNull(), isSubTypeOf() recomputes) and right for
// `never`, of which null is a supertype without never containing it.
if ($type->isNull()->no()) {
return self::union($type, new NullType());
}

return $type;
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public static function dataAddNull(): array
NullType::class,
'null',
],
[
new NeverType(),
NullType::class,
'null',
],
[
new NeverType(true),
NullType::class,
'null',
],
[
new VoidType(),
UnionType::class,
Expand Down
Loading