Skip to content

Full variance support if autoloading is used #4194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/bug30922.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ var_dump($a instanceOf A);
echo "ok\n";
?>
--EXPECTF--
Fatal error: Interface 'RecurisiveFooFar' not found in %sbug30922.php on line %d
Fatal error: Interface RecurisiveFooFar cannot implement itself in %s on line %d
37 changes: 37 additions & 0 deletions Zend/tests/type_declarations/variance/class_order_autoload1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Class order allowed with autoloading (1)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class === 'A') {
class A {
public function method() : B {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method() : C {}
}
var_dump(new B);
} else {
class C extends B {
}
var_dump(new C);
}
});

var_dump(new C);

?>
===DONE===
--EXPECT--
object(A)#2 (0) {
}
object(B)#2 (0) {
}
object(C)#2 (0) {
}
object(C)#2 (0) {
}
===DONE===
38 changes: 38 additions & 0 deletions Zend/tests/type_declarations/variance/class_order_autoload2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Class order allowed with autoloading (2)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class === 'A') {
class A {
public function method() : B {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method() : C {}
}
var_dump(new B);
} else {
class C extends B {
}
var_dump(new C);
}
});

// Same as autoload1 test case, but with a different autoloading root
var_dump(new B);

?>
===DONE===
--EXPECT--
object(A)#2 (0) {
}
object(C)#2 (0) {
}
object(B)#2 (0) {
}
object(B)#2 (0) {
}
===DONE===
45 changes: 45 additions & 0 deletions Zend/tests/type_declarations/variance/class_order_autoload3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
Class order allowed with autoloading (3)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class == 'A') {
class A {
public function method(): X {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method(): Y {}
}
var_dump(new B);
} else if ($class == 'X') {
class X {
public function method(): A {}
}
var_dump(new X);
} else if ($class == 'Y') {
class Y extends X {
public function method(): B {}
}
var_dump(new Y);
}
});

var_dump(new B);

?>
===DONE===
--EXPECT--
object(A)#2 (0) {
}
object(X)#2 (0) {
}
object(Y)#2 (0) {
}
object(B)#2 (0) {
}
object(B)#2 (0) {
}
===DONE===
44 changes: 44 additions & 0 deletions Zend/tests/type_declarations/variance/class_order_autoload4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Class order allowed with autoloading (4)
--FILE--
<?php

// Same as autoload3 test case, but with X, Y being interfaces.
spl_autoload_register(function($class) {
if ($class == 'A') {
class A {
public function method(): X {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method(): Y {}
}
var_dump(new B);
} else if ($class == 'X') {
interface X {
public function method(): A;
}
var_dump(interface_exists('X'));
} else if ($class == 'Y') {
interface Y extends X {
public function method(): B;
}
var_dump(interface_exists('Y'));
}
});

var_dump(new B);

?>
===DONE===
--EXPECT--
object(A)#2 (0) {
}
bool(true)
bool(true)
object(B)#2 (0) {
}
object(B)#2 (0) {
}
===DONE===
60 changes: 60 additions & 0 deletions Zend/tests/type_declarations/variance/class_order_autoload5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Class order allowed with autoloading (5)
--FILE--
<?php

// Similar to variance3, but one more class hierarchy in the cycle
spl_autoload_register(function($class) {
if ($class == 'A') {
class A {
public function method(): X {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method(): Y {}
}
var_dump(new B);
} else if ($class == 'X') {
class X {
public function method(): Q {}
}
var_dump(new X);
} else if ($class == 'Y') {
class Y extends X {
public function method(): R {}
}
var_dump(new Y);
} else if ($class == 'Q') {
class Q {
public function method(): A {}
}
var_dump(new Q);
} else if ($class == 'R') {
class R extends Q {
public function method(): B {}
}
var_dump(new R);
}
});

var_dump(new B);

?>
===DONE===
--EXPECT--
object(A)#2 (0) {
}
object(X)#2 (0) {
}
object(Q)#2 (0) {
}
object(R)#2 (0) {
}
object(Y)#2 (0) {
}
object(B)#2 (0) {
}
object(B)#2 (0) {
}
===DONE===
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
--TEST--
Returns are covariant, but we don't allow the code due to class ordering (autoload variation)
Variance error in the presence of autoloading (1)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class === 'A') {
class A {
public function method() : B {}
public function method() : C {}
}
} else if ($class == 'B') {
class B extends A {
public function method() : C {}
public function method() : B {}
}
} else {
class C extends B {
}
}
});

$c = new C;
$b = new B;

?>
--EXPECTF--
Fatal error: Could not check compatibility between B::method(): C and A::method(): B, because class C is not available in %s on line %d
Fatal error: Declaration of B::method(): B must be compatible with A::method(): C in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Variance error in the presence of autoloading (2)
--FILE--
<?php

// Same as autoload_error1, but for argument types.
spl_autoload_register(function($class) {
if ($class === 'A') {
class A {
public function method(B $x) {}
}
} else if ($class == 'B') {
class B extends A {
public function method(C $x) {}
}
} else {
class C extends B {
}
}
});

$b = new B;
$c = new C;

?>
--EXPECTF--
Warning: Declaration of B::method(C $x) should be compatible with A::method(B $x) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Variance error in the presence of autoloading (3)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class == 'A') {
class A {
public function method(): X {}
}
} else if ($class == 'B') {
class B extends A {
public function method(): Y {}
}
} else if ($class == 'X') {
class X {
public function method(): Q {}
}
} else if ($class == 'Y') {
class Y extends X {
public function method(): R {}
}
} else if ($class == 'Q') {
class Q {
public function method(): B {}
}
} else if ($class == 'R') {
class R extends Q {
public function method(): A {}
}
}
});

$b = new B;

?>
--EXPECTF--
Fatal error: Declaration of R::method(): A must be compatible with Q::method(): B in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Variance error in the presence of autoloading (4)
--FILE--
<?php

spl_autoload_register(function($class) {
if ($class == 'A') {
class A {
public function method(): X {}
}
var_dump(new A);
} else if ($class == 'B') {
class B extends A {
public function method(): Y {}
}
var_dump(new B);
} else if ($class == 'X') {
class X {
public function method(): B {}
}
var_dump(new X);
} else if ($class == 'Y') {
class Y extends X {
public function method(): A {}
}
var_dump(new Y);
}
});

var_dump(new B);

?>
--EXPECTF--
object(A)#2 (0) {
}
object(X)#2 (0) {
}

Fatal error: Declaration of Y::method(): A must be compatible with X::method(): B in %s on line %d
Loading