test(compiler): cover cross-namespace parameter variance

pull/21/head
韩天峰 2 months ago
parent 72cbdc500a
commit e231aa2404
  1. 5
      src/Translator.php
  2. 18
      tests/compiler/namespace/interface-impl-param-type-cross-ns.phpt

@ -3242,6 +3242,7 @@ CODE;
return $arg->typeCheck; return $arg->typeCheck;
} }
$declaredClass = $arg->declaredClass ?: $arg->class;
return match ($arg->type) { return match ($arg->type) {
Type::INT => [['kind' => 'isInt']], Type::INT => [['kind' => 'isInt']],
Type::FLOAT => [['kind' => 'isFloat']], Type::FLOAT => [['kind' => 'isFloat']],
@ -3249,8 +3250,8 @@ CODE;
Type::STR => [['kind' => 'isString']], Type::STR => [['kind' => 'isString']],
Type::ARRAY => [['kind' => 'isArray']], Type::ARRAY => [['kind' => 'isArray']],
Type::RESOURCE => [['kind' => 'isResource']], Type::RESOURCE => [['kind' => 'isResource']],
Type::OBJECT => $arg->declaredClass Type::OBJECT => $declaredClass
? [['kind' => 'instanceof', 'class' => $arg->declaredClass]] ? [['kind' => 'instanceof', 'class' => $declaredClass]]
: [['kind' => 'isObject']], : [['kind' => 'isObject']],
default => null, default => null,
}; };

@ -1,8 +1,9 @@
--TEST-- --TEST--
Cross-namespace interface implementation with an interface-typed parameter must not be reported as incompatible Cross-namespace interface implementation parameter compatibility is declaration-order independent
--FILE-- --FILE--
<?php <?php
namespace A { namespace A {
use B\I0;
use B\I1; use B\I1;
use B\I2; use B\I2;
@ -12,17 +13,28 @@ namespace A {
{ {
return true; return true;
} }
public function testParent(I0 $a): bool
{
return true;
}
} }
} }
namespace B { namespace B {
interface I1 interface I0
{
}
interface I1 extends I0
{ {
} }
interface I2 interface I2
{ {
public function test(I1 $a): bool; public function test(I1 $a): bool;
public function testParent(I1 $a): bool;
} }
class Impl1 implements I1 class Impl1 implements I1
@ -39,10 +51,12 @@ namespace {
{ {
$obj = new Concrete(); $obj = new Concrete();
var_dump($obj->test(new \B\Impl1())); var_dump($obj->test(new \B\Impl1()));
var_dump($obj->testParent(new \B\Impl1()));
echo "done\n"; echo "done\n";
} }
} }
?> ?>
--EXPECT-- --EXPECT--
bool(true) bool(true)
bool(true)
done done

Loading…
Cancel
Save