File tree Expand file tree Collapse file tree 4 files changed +59
-28
lines changed
Zend/tests/type_declarations/intersection_types Expand file tree Collapse file tree 4 files changed +59
-28
lines changed Original file line number Diff line number Diff line change 1+ --TEST--
2+ Intersection types and typed reference
3+ --FILE--
4+ <?php
5+
6+ interface X {}
7+ interface Y {}
8+ interface Z {}
9+
10+ class A implements X, Y, Z {}
11+ class B implements X, Y {}
12+
13+ class Test {
14+ public X &Y $ y ;
15+ public X &Z $ z ;
16+ }
17+ $ test = new Test ;
18+ $ r = new A ;
19+ $ test ->y =& $ r ;
20+ $ test ->z =& $ r ;
21+
22+ try {
23+ $ r = new B ;
24+ } catch (\TypeError $ e ) {
25+ echo $ e ->getMessage (), \PHP_EOL ;
26+ }
27+
28+ ?>
29+ --EXPECT--
30+ Cannot assign B to reference held by property Test::$z of type X&Z
Original file line number Diff line number Diff line change 11--TEST--
2- Basic tests for intersection types
2+ Valid inheritence - co-variance
33--FILE--
44<?php
55
Original file line number Diff line number Diff line change 11--TEST--
2- Intersection with a child type is valid
2+ Commutative intersection types
33--FILE--
44<?php
55
66interface A {}
7- interface B extends A{}
8- interface C {}
9-
10- class Test implements A, B {}
7+ interface B {}
118
129class Foo {
13- public function foo (): A &B {
14- return new Test ();
15- }
10+ public A &B $ prop ;
11+ public function foo (A &B $ v ): A &B {}
1612}
1713
1814class FooChild extends Foo {
19- public function foo (): A &B {
20- return new Test ();
21- }
22- }
23-
24- class FooChildAgain extends Foo {
25- public function foo (): B &A {
26- return new Test ();
27- }
15+ public B &A $ prop ;
16+ public function foo (B &A $ v ): B &A {}
2817}
2918
30- $ o = new FooChild ();
31- var_dump ($ o ->foo ());
32- $ o = new FooChildAgain ();
33- var_dump ($ o ->foo ());
34-
3519?>
36- --EXPECTF--
37- object(Test)#%d (0) {
38- }
39- object(Test)#%d (0) {
40- }
20+ ===DONE===
21+ --EXPECT--
22+ ===DONE===
Original file line number Diff line number Diff line change 1+ --TEST--
2+ Intersection type reduction invariant type check
3+ --FILE--
4+ <?php
5+
6+ class A {}
7+ class B extends A {}
8+
9+ class Test {
10+ public A &B $ prop ;
11+ }
12+ class Test2 extends Test {
13+ public B $ prop ;
14+ }
15+
16+ ?>
17+ ===DONE===
18+ --EXPECT--
19+ ===DONE===
You can’t perform that action at this time.
0 commit comments