Skip to content

Commit 037487d

Browse files
committed
Zend: Warn when destructuring non-array values
RFC: https://wiki.php.net/rfc/warnings-php-8-5#destructuring_non-array_values
1 parent e990b69 commit 037487d

15 files changed

+144
-79
lines changed

Zend/tests/foreach/foreach_list_002.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ foreach($array as list(, $a)) {
1717
--EXPECT--
1818
int(1)
1919
int(3)
20-
string(1) "b"
20+
string(1)
21+
22+
Warning: Cannot use string as array in %s on line %d
2123
NULL
2224
NULL

Zend/tests/list/bug39304.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ Bug #39304 (Segmentation fault with list unpacking of string offset)
88
?>
99
--EXPECTF--
1010
Warning: Uninitialized string offset 0 in %s on line %d
11+
12+
Warning: Cannot use string as array in %s on line %d
13+
14+
Warning: Cannot use string as array in %s on line %d
1115
NULL
1216
NULL

Zend/tests/list/destruct_bool.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Destructuring with list() a value of type bool
3+
--FILE--
4+
<?php
5+
6+
$v = true;
7+
8+
list($a, $b) = $v;
9+
10+
var_dump($a, $b);
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Cannot use bool as array in %s on line %d
15+
16+
Warning: Cannot use bool as array in %s on line %d
17+
NULL
18+
NULL

Zend/tests/list/destruct_float.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Destructuring with list() a value of type float
3+
--FILE--
4+
<?php
5+
6+
$v = 25.64;
7+
8+
list($a, $b) = $v;
9+
10+
var_dump($a, $b);
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Cannot use float as array in %s on line %d
15+
16+
Warning: Cannot use float as array in %s on line %d
17+
NULL
18+
NULL

Zend/tests/list/destruct_int.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Destructuring with list() a value of type int
3+
--FILE--
4+
<?php
5+
6+
$v = 5;
7+
8+
list($a, $b) = $v;
9+
10+
var_dump($a, $b);
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Cannot use int as array in %s on line %d
15+
16+
Warning: Cannot use int as array in %s on line %d
17+
NULL
18+
NULL

Zend/tests/list/destruct_null.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Destructuring with list() a value of type null
3+
--FILE--
4+
<?php
5+
6+
list($a) = null;
7+
8+
var_dump($a);
9+
10+
?>
11+
--EXPECT--
12+
NULL
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Destructuring with list() a value of type object (that does not implement ArrayAccess)
3+
--FILE--
4+
<?php
5+
6+
$v = new stdClass();
7+
8+
list($a, $b) = $v;
9+
10+
var_dump($a, $b);
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d
15+
Stack trace:
16+
#0 {main}
17+
thrown in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Destructuring with list() a value of type resource
3+
--FILE--
4+
<?php
5+
6+
$v = STDERR;
7+
8+
list($a, $b) = $v;
9+
10+
var_dump($a, $b);
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Cannot use resource as array in %s on line %d
15+
16+
Warning: Cannot use resource as array in %s on line %d
17+
NULL
18+
NULL

Zend/tests/list/destruct_string.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Destructuring with list() a value of type string
3+
--FILE--
4+
<?php
5+
6+
$v = 'hi';
7+
8+
list($a, $b, $c) = $v;
9+
10+
var_dump($a, $b, $c);
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Cannot use string as array in %s on line %d
15+
16+
Warning: Cannot use string as array in %s on line %d
17+
18+
Warning: Cannot use string as array in %s on line %d
19+
NULL
20+
NULL
21+
NULL

Zend/tests/list/list_003.phpt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)