Skip to content

Commit cd7d1a8

Browse files
committed
Consolidate, rename, and add new variance tests
1 parent b781f9f commit cd7d1a8

17 files changed

+100
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Variance checks work across same-file namespaces (brace style)
3+
--FILE--
4+
<?php
5+
6+
namespace Foo {
7+
class A {
8+
function foo(B $x): A {
9+
return new self();
10+
}
11+
}
12+
}
13+
14+
namespace Bar {
15+
class B extends \Foo\A {
16+
function foo(\stdClass $x): B {
17+
return new self();
18+
}
19+
}
20+
}
21+
22+
namespace {
23+
echo get_class((new \Bar\B())->foo(new \stdClass()));
24+
}
25+
?>
26+
--EXPECTF--
27+
Warning: Declaration of Bar\B::foo(stdClass $x): Bar\B should be compatible with Foo\A::foo(Foo\B $x): Foo\A in %s on line %d
28+
Bar\B
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Variance checks work across same-file namespaces
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
class A {
9+
function foo(B $x): A {
10+
return new self();
11+
}
12+
}
13+
14+
namespace Bar;
15+
16+
class B extends \Foo\A {
17+
function foo(\stdClass $x): B {
18+
return new self();
19+
}
20+
}
21+
22+
echo get_class((new \Bar\B())->foo(new \stdClass()));
23+
?>
24+
--EXPECTF--
25+
Warning: Declaration of Bar\B::foo(stdClass $x): Bar\B should be compatible with Foo\A::foo(Foo\B $x): Foo\A in %s on line %d
26+
Bar\B
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Delayed variance checks work with conditional statements
3+
--FILE--
4+
<?php
5+
6+
interface A {
7+
static function m(Y $z): A;
8+
}
9+
10+
if (true) {
11+
class B implements A {
12+
static function m(X $z): B {
13+
return new self();
14+
}
15+
}
16+
class X {}
17+
class Y extends X {}
18+
}
19+
20+
echo get_class(B::m(new X()));
21+
?>
22+
--EXPECTF--
23+
B
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Delayed variance checks work with conditional statements
3+
--FILE--
4+
<?php
5+
6+
interface A {
7+
static function m(Y $z): A;
8+
}
9+
10+
if (true) {
11+
class B implements A {
12+
static function m(X $z): B {
13+
return new self();
14+
}
15+
}
16+
class X {}
17+
//class Y extends X {}
18+
}
19+
20+
echo get_class(B::m(new X()));
21+
?>
22+
--EXPECTF--
23+
Fatal error: Declaration of B::m(X $z): B must be compatible with A::m(Y $z): A in %s on line %d

0 commit comments

Comments
 (0)