Skip to content

Commit cf004ed

Browse files
committed
Fix implicitly nullable type detection in stubs
1 parent 661c5ee commit cf004ed

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

build/gen_stub.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ public function isArray(): bool {
394394
return $this->isBuiltin && $this->name === 'array';
395395
}
396396

397+
public function isMixed(): bool {
398+
return $this->isBuiltin && $this->name === 'mixed';
399+
}
400+
397401
public function toTypeCode(): string {
398402
assert($this->isBuiltin);
399403
switch ($this->name) {
@@ -4233,7 +4237,7 @@ function parseFunctionLike(
42334237
$type && !$type->isNullable()
42344238
) {
42354239
$simpleType = $type->tryToSimpleType();
4236-
if ($simpleType === null) {
4240+
if ($simpleType === null || !$simpleType->isMixed()) {
42374241
throw new Exception("Parameter $varName has null default, but is not nullable");
42384242
}
42394243
}
@@ -4342,13 +4346,26 @@ function parseConstLike(
43424346
throw new Exception("Missing type for constant " . $name->__toString());
43434347
}
43444348

4349+
$constType = $type ? Type::fromNode($type) : null;
4350+
$constPhpDocType = $phpDocType ? Type::fromString($phpDocType) : null;
4351+
4352+
if ($const->value instanceof Expr\ConstFetch &&
4353+
$const->value->name->toLowerString() === "null" &&
4354+
$constType && !$constType->isNullable()
4355+
) {
4356+
$simpleType = $constType->tryToSimpleType();
4357+
if ($simpleType === null || !$simpleType->isMixed()) {
4358+
throw new Exception("Constant " . $name->__toString() . " has null value, but is not nullable");
4359+
}
4360+
}
4361+
43454362
return new ConstInfo(
43464363
$name,
43474364
$flags,
43484365
$const->value,
43494366
$prettyPrinter->prettyPrintExpr($const->value),
4350-
$type ? Type::fromNode($type) : null,
4351-
$phpDocType ? Type::fromString($phpDocType) : null,
4367+
$constType,
4368+
$constPhpDocType,
43524369
$deprecated,
43534370
$cond,
43544371
$cValue,
@@ -4401,9 +4418,8 @@ function parseProperty(
44014418
$propertyType && !$propertyType->isNullable()
44024419
) {
44034420
$simpleType = $propertyType->tryToSimpleType();
4404-
if ($simpleType === null) {
4405-
throw new Exception(
4406-
"Property $class::\$$property->name has null default, but is not nullable");
4421+
if ($simpleType === null || !$simpleType->isMixed()) {
4422+
throw new Exception("Property $class::\$$property->name has null default, but is not nullable");
44074423
}
44084424
}
44094425

0 commit comments

Comments
 (0)