- 在 arginfo 生成中添加对可变参数的支持 - 实现可变参数联合类型的运行时类型检查逻辑 - 添加循环遍历可变参数并逐个验证类型的功能 - 生成适当的错误消息以指示可变参数中的具体错误位置 - 添加测试用例验证可变参数联合类型和可空类型的运行时检查功能pull/1/head
parent
8c411085d1
commit
b89dd177b3
3 changed files with 117 additions and 6 deletions
@ -0,0 +1,55 @@ |
|||||||
|
--TEST-- |
||||||
|
Variadic union and nullable parameter runtime type checking |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function collect_scalars(int|string ...$values): array |
||||||
|
{ |
||||||
|
return $values; |
||||||
|
} |
||||||
|
|
||||||
|
function collect_nullable(?int ...$values): array |
||||||
|
{ |
||||||
|
return $values; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(collect_scalars(1, "two", named: 3)); |
||||||
|
var_dump(collect_nullable(1, null, 3)); |
||||||
|
|
||||||
|
$errors = []; |
||||||
|
try { |
||||||
|
collect_scalars(1, "two", []); |
||||||
|
} catch (\TypeError $e) { |
||||||
|
$errors[] = $e->getMessage(); |
||||||
|
} |
||||||
|
try { |
||||||
|
collect_nullable(ok: 1, bad: "x"); |
||||||
|
} catch (\TypeError $e) { |
||||||
|
$errors[] = $e->getMessage(); |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($errors as $error) { |
||||||
|
var_dump($error); |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
array(3) { |
||||||
|
[0]=> |
||||||
|
int(1) |
||||||
|
[1]=> |
||||||
|
string(3) "two" |
||||||
|
["named"]=> |
||||||
|
int(3) |
||||||
|
} |
||||||
|
array(3) { |
||||||
|
[0]=> |
||||||
|
int(1) |
||||||
|
[1]=> |
||||||
|
NULL |
||||||
|
[2]=> |
||||||
|
int(3) |
||||||
|
} |
||||||
|
string(80) "collect_scalars(): Argument #3 ($values) must be of type int|string, array given" |
||||||
|
string(76) "collect_nullable(): Argument #2 ($values) must be of type ?int, string given" |
||||||
Loading…
Reference in new issue