Skip to content

Commit e3d06fc

Browse files
committed
Ensure correct signatures for magic methods
1 parent 650801c commit e3d06fc

30 files changed

+506
-3
lines changed

UPGRADING

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,26 @@ PHP 8.0 UPGRADE NOTES
236236
"Illegal string offset 'string'" for illegal string offsets. The behavior
237237
of explicit casts to int/float from strings has not been changed.
238238
RFC: https://wiki.php.net/rfc/saner-numeric-strings
239+
. Magic Methods will now have their arguments and return types
240+
checked if they have them declared. The signatures should
241+
match the following list:
242+
243+
__call(string $name, array $arguments): mixed
244+
__callStatic(string $name, array $arguments): mixed
245+
__clone(): void
246+
__debugInfo(): ?array
247+
__get(string $name): mixed
248+
__invoke(mixed $arguments): mixed
249+
__isset(string $name): bool
250+
__serialize(): array
251+
__set(string $name, mixed $value): void
252+
__set_state(array $properties): object
253+
__sleep(): array
254+
__unserialize(array $data): void
255+
__unset(string $name): void
256+
__wakeup(): void
257+
258+
RFC: https://wiki.php.net/rfc/magic-methods-signature
239259

240260
- COM:
241261
. Removed the ability to import case-insensitive constants from type

Zend/tests/magic_methods_011.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__set first parameter should be a string when typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __set(\Countable $name, $value) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__set(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_012.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__get first parameter should be a string when typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __get(int $name) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__get(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_013.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__isset first parameter should be a string when typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __isset(\stdClass $name) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__isset(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_014.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__unset first parameter should be a string when typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __unset(array $name) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__unset(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_015.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__call first parameter should be a string typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __call(int $name, array $arguments) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__call(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_016.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__call second parameter should be an array when typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
function __call(string $name, \Arguments $arguments) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__call(): Parameter #2 ($arguments) must be of type array when declared in %s on line %d

Zend/tests/magic_methods_017.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__callStatic first parameter should be a string typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
static function __callStatic(int $name, array $arguments) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__callStatic(): Parameter #1 ($name) must be of type string when declared in %s on line %d

Zend/tests/magic_methods_018.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__callStatic second parameter should be an array typed
3+
--FILE--
4+
<?php
5+
class Foo {
6+
static function __callStatic(string $name, \Arguments $args) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__callStatic(): Parameter #2 ($args) must be of type array when declared in %s on line %d

Zend/tests/magic_methods_019.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
__unserialize first parameter must be an array
3+
--FILE--
4+
<?php
5+
class Foo {
6+
public function __unserialize(string $name) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Foo::__unserialize(): Parameter #1 ($name) must be of type array when declared in %s on line %d

0 commit comments

Comments
 (0)