1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

gen_stub: Infer constants' types from values (#19568)

Don't require `@var` with a type when the type can be inferred from a literal
value in the stub file.
This commit is contained in:
Daniel Scherzer
2025-09-03 18:26:43 -07:00
committed by GitHub
parent 9b960713c6
commit 3f3a266a2b
27 changed files with 40 additions and 106 deletions

View File

@@ -4874,7 +4874,32 @@ function parseConstLike(
}
if ($type === null && $phpDocType === null) {
throw new Exception("Missing type for constant " . $name->__toString());
if ($const->value instanceof Node\Scalar\Float_) {
$phpDocType = 'float';
} elseif ($const->value instanceof Node\Scalar\Int_
|| ($const->value instanceof Expr\UnaryMinus
&& $const->value->expr instanceof Node\Scalar\Int_
)
) {
$phpDocType = 'int';
} elseif ($const->value instanceof Node\Scalar\String_) {
$phpDocType = 'string';
} elseif ($const->value instanceof Expr\ConstFetch
&& $const->value->name instanceof Node\Name\FullyQualified
&& (
$const->value->name->name === 'false'
|| $const->value->name->name === 'true'
)
) {
$phpDocType = 'bool';
} elseif ($const->value instanceof Expr\ConstFetch
&& $const->value->name instanceof Node\Name\FullyQualified
&& $const->value->name->name === 'null'
) {
$phpDocType = 'null';
} else {
throw new Exception("Missing type for constant " . $name->__toString());
}
}
$constType = $type ? Type::fromNode($type) : null;