fix(compiler): 处理未定义变量被引用赋值后跨类方法参数匹配类型不正确

pull/17/head
Yurun 1 month ago
parent 02e16794b8
commit da93efcb69
  1. 6
      src/TypeSystem/CompositeTypeCheckerTrait.php
  2. 38
      tests/compiler/static/static-call-byref-undefined-var.phpt

@ -35,7 +35,11 @@ trait CompositeTypeCheckerTrait
// TYPE_VAR means that the expression is dynamic or its result cannot
// be represented by the current scalar type system. It must retain the
// runtime type check.
if ($this->detectTypeOfExpr($value) === Type::VAR && !$this->isNullExpr($value)) {
// A reference (TYPE_REF) is a Variant reference whose concrete type is
// only known at runtime (e.g. an undefined variable auto-created by a
// by-reference argument), so it is treated the same way.
$valueType = $this->detectTypeOfExpr($value);
if (($valueType === Type::VAR || $valueType === Type::REF) && !$this->isNullExpr($value)) {
return self::COMPOSITE_TYPE_UNKNOWN;
}

@ -0,0 +1,38 @@
--TEST--
Static method call passing an undefined variable by reference (late static binding)
--FILE--
<?php
class Test
{
public static function getName(?string &$name)
{
$name = 'test';
}
public static function run()
{
// $fileName is undefined; passing it by reference must auto-create it
// instead of raising an "Undefined variable" fatal error.
static::getName($fileName);
var_dump($fileName);
Test2::dump($fileName);
}
}
class Test2
{
public static function dump(?string $value)
{
var_dump($value);
}
}
function main()
{
Test::run();
}
?>
--EXPECT--
string(4) "test"
string(4) "test"
Loading…
Cancel
Save