Skip to content

Commit dbc8ee9

Browse files
jiripudilondrejmirtes
authored andcommitted
Support "null" and "false" pseudotypes in native unions
1 parent 12d5a12 commit dbc8ee9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Type/TypehintHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Type;
44

55
use PHPStan\Broker\Broker;
6+
use PHPStan\Type\Constant\ConstantBooleanType;
67
use PHPStan\Type\Generic\TemplateTypeHelper;
78
use ReflectionNamedType;
89
use ReflectionType;
@@ -18,6 +19,8 @@ private static function getTypeObjectFromTypehint(string $typeString, ?string $s
1819
return new IntegerType();
1920
case 'bool':
2021
return new BooleanType();
22+
case 'false':
23+
return new ConstantBooleanType(false);
2124
case 'string':
2225
return new StringType();
2326
case 'float':
@@ -47,6 +50,8 @@ private static function getTypeObjectFromTypehint(string $typeString, ?string $s
4750
return new NonexistentParentClassType();
4851
case 'static':
4952
return $selfClass !== null ? new StaticType($selfClass) : new ErrorType();
53+
case 'null':
54+
return new NullType();
5055
default:
5156
return new ObjectType($typeString);
5257
}
@@ -85,6 +90,12 @@ public static function decideTypeFromReflection(
8590
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\mixed')) {
8691
$reflectionTypeString = 'mixed';
8792
}
93+
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\false')) {
94+
$reflectionTypeString = 'false';
95+
}
96+
if (\Nette\Utils\Strings::endsWith(strtolower($reflectionTypeString), '\\null')) {
97+
$reflectionTypeString = 'null';
98+
}
8899

89100
$type = self::getTypeObjectFromTypehint($reflectionTypeString, $selfClass);
90101
if ($reflectionType->allowsNull()) {

tests/PHPStan/Reflection/data/unionTypes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ public function doLorem(): array|false
7878

7979
}
8080

81+
public function doIpsum(int|string|null $nullable): void
82+
{
83+
assertType('int|null|string', $nullable);
84+
assertNativeType('int|null|string', $nullable);
85+
assertType('int|null|string', $this->doDolor());
86+
}
87+
88+
public function doDolor(): int|string|null
89+
{
90+
91+
}
92+
8193
}

0 commit comments

Comments
 (0)