Permalink
Please sign in to comment.
Browse files
Allow specifying keys on list() elements
Squashed commit of the following: commit c56f6e3 Author: Andrea Faulds <[email protected]> Date: Wed Feb 24 13:49:17 2016 +0000 Import Zend Engine tests for list() commit 3afd58a Author: Andrea Faulds <[email protected]> Date: Wed Feb 24 13:49:05 2016 +0000 Update specification for list() keys
- Loading branch information...
Showing
with
888 additions
and 3 deletions.
- +38 −2 spec/10-expressions.md
- +9 −1 spec/19-grammar.md
- +14 −0 tests/expressions/list/list_001.phpt
- +20 −0 tests/expressions/list/list_002.phpt
- +24 −0 tests/expressions/list/list_003.phpt
- +21 −0 tests/expressions/list/list_004.phpt
- +50 −0 tests/expressions/list/list_005.phpt
- +12 −0 tests/expressions/list/list_006.phpt
- +15 −0 tests/expressions/list/list_007.phpt
- +49 −0 tests/expressions/list/list_destructuring_to_special_variables.phpt
- +10 −0 tests/expressions/list/list_empty_error.phpt
- +71 −0 tests/expressions/list/list_keyed.phpt
- +54 −0 tests/expressions/list/list_keyed_ArrayAccess.phpt
- +32 −0 tests/expressions/list/list_keyed_conversions.phpt
- +60 −0 tests/expressions/list/list_keyed_evaluation_order.inc
- +35 −0 tests/expressions/list/list_keyed_evaluation_order.phpt
- +77 −0 tests/expressions/list/list_keyed_evaluation_order_2.phpt
- +24 −0 tests/expressions/list/list_keyed_evaluation_order_3.phpt
- +77 −0 tests/expressions/list/list_keyed_evaluation_order_nested.phpt
- +30 −0 tests/expressions/list/list_keyed_non_literals.phpt
- +38 −0 tests/expressions/list/list_keyed_trailing_comma.phpt
- +22 −0 tests/expressions/list/list_keyed_undefined.phpt
- +16 −0 tests/expressions/list/list_mixed_keyed_unkeyed.phpt
- +34 −0 tests/expressions/list/list_mixed_nested_keyed_unkeyed.phpt
- +56 −0 tests/expressions/list/list_self_assign.phpt
| @@ -0,0 +1,14 @@ | ||
| +--TEST-- | ||
| +"Nested" list() | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +list($a, list($b)) = array(new stdclass, array(new stdclass)); | ||
| +var_dump($a, $b); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +object(stdClass)#1 (0) { | ||
| +} | ||
| +object(stdClass)#2 (0) { | ||
| +} |
| @@ -0,0 +1,20 @@ | ||
| +--TEST-- | ||
| +Testing full-reference on list() | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +error_reporting(E_ALL); | ||
| + | ||
| +$a = new stdclass; | ||
| +$b =& $a; | ||
| + | ||
| +list($a, list($b)) = array($a, array($b)); | ||
| +var_dump($a, $b, $a === $b); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +object(stdClass)#1 (0) { | ||
| +} | ||
| +object(stdClass)#1 (0) { | ||
| +} | ||
| +bool(true) |
| @@ -0,0 +1,24 @@ | ||
| +--TEST-- | ||
| +list() with non-array | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +list($a) = NULL; | ||
| + | ||
| +list($b) = 1; | ||
| + | ||
| +list($c) = 1.; | ||
| + | ||
| +list($d) = 'foo'; | ||
| + | ||
| +list($e) = print ''; | ||
| + | ||
| +var_dump($a, $b, $c, $d, $e); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +NULL | ||
| +NULL | ||
| +NULL | ||
| +NULL | ||
| +NULL |
| @@ -0,0 +1,21 @@ | ||
| +--TEST-- | ||
| +list() with array reference | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +$arr = array(2, 1); | ||
| +$b =& $arr; | ||
| + | ||
| +list(,$a) = $b; | ||
| + | ||
| +var_dump($a, $b); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +int(1) | ||
| +array(2) { | ||
| + [0]=> | ||
| + int(2) | ||
| + [1]=> | ||
| + int(1) | ||
| +} |
| @@ -0,0 +1,50 @@ | ||
| +--TEST-- | ||
| +Testing list() with several variables | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +$str = "foo"; | ||
| + | ||
| +list($a, $b, $c) = $str; | ||
| + | ||
| +var_dump($a, $b, $c); | ||
| + | ||
| +print "----\n"; | ||
| + | ||
| +$int = 1; | ||
| + | ||
| +list($a, $b, $c) = $int; | ||
| + | ||
| +var_dump($a, $b, $c); | ||
| + | ||
| +print "----\n"; | ||
| + | ||
| +$obj = new stdClass; | ||
| + | ||
| +list($a, $b, $c) = $obj; | ||
| + | ||
| +var_dump($a, $b, $c); | ||
| + | ||
| +print "----\n"; | ||
| + | ||
| +$arr = array(1, 2, 3); | ||
| + | ||
| +list($a, $b, $c) = $arr; | ||
| + | ||
| +var_dump($a, $b, $c); | ||
| + | ||
| +?> | ||
| +--EXPECTF-- | ||
| +NULL | ||
| +NULL | ||
| +NULL | ||
| +---- | ||
| +NULL | ||
| +NULL | ||
| +NULL | ||
| +---- | ||
| + | ||
| +Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d | ||
| +Stack trace: | ||
| +#0 {main} | ||
| + thrown in %s on line %d |
| @@ -0,0 +1,12 @@ | ||
| +--TEST-- | ||
| +Testing nested list() with empty array | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +list($a, list($b, list(list($d)))) = array(); | ||
| + | ||
| +?> | ||
| +--EXPECTF-- | ||
| +Notice: Undefined offset: 0 in %s on line %d | ||
| + | ||
| +Notice: Undefined offset: 1 in %s on line %d |
| @@ -0,0 +1,15 @@ | ||
| +--TEST-- | ||
| +Using lambda with list() | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +list($x, $y) = function() { }; | ||
| + | ||
| +var_dump($x, $y); | ||
| + | ||
| +?> | ||
| +--EXPECTF-- | ||
| +Fatal error: Uncaught Error: Cannot use object of type Closure as array in %slist_007.php:3 | ||
| +Stack trace: | ||
| +#0 {main} | ||
| + thrown in %slist_007.php on line 3 |
| @@ -0,0 +1,49 @@ | ||
| +--TEST-- | ||
| +list() can be used to destructure to string offsets, __set and ArrayAccess::offsetSet | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +class Obj { | ||
| + public $values = []; | ||
| + public function __set($name, $value) { | ||
| + $this->values[$name] = $value; | ||
| + } | ||
| +} | ||
| + | ||
| +class Arr implements ArrayAccess { | ||
| + public $values = []; | ||
| + public function offsetSet($name, $value) { | ||
| + $this->values[$name] = $value; | ||
| + } | ||
| + public function offsetGet($name) {} | ||
| + public function offsetExists($name) {} | ||
| + public function offsetUnset($name) {} | ||
| +} | ||
| + | ||
| +$str = 'ab'; | ||
| +list($str[0], $str[1]) = ['x', 'y']; | ||
| +var_dump($str); | ||
| + | ||
| +$obj = new Obj; | ||
| +list($obj->foo, $obj->bar) = ['foo', 'bar']; | ||
| +var_dump($obj->values); | ||
| + | ||
| +$arr = new Arr; | ||
| +list($arr['foo'], $arr['bar']) = ['foo', 'bar']; | ||
| +var_dump($arr->values); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +string(2) "xy" | ||
| +array(2) { | ||
| + ["foo"]=> | ||
| + string(3) "foo" | ||
| + ["bar"]=> | ||
| + string(3) "bar" | ||
| +} | ||
| +array(2) { | ||
| + ["foo"]=> | ||
| + string(3) "foo" | ||
| + ["bar"]=> | ||
| + string(3) "bar" | ||
| +} |
| @@ -0,0 +1,10 @@ | ||
| +--TEST-- | ||
| +Empty list() assignments are not allowed | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +list(,,,,,,,,,,) = []; | ||
| + | ||
| +?> | ||
| +--EXPECTF-- | ||
| +Fatal error: Cannot use empty list in %s on line %d |
| @@ -0,0 +1,71 @@ | ||
| +--TEST-- | ||
| +list() with keys | ||
| +--FILE-- | ||
| +<?php | ||
| + | ||
| +$antonyms = [ | ||
| + "good" => "bad", | ||
| + "happy" => "sad", | ||
| +]; | ||
| + | ||
| +list("good" => $good_antonym, "happy" => $happy_antonym) = $antonyms; | ||
| +var_dump($good_antonym, $happy_antonym); | ||
| + | ||
| +echo PHP_EOL; | ||
| + | ||
| +$powersOfTwo = [ | ||
| + 1 => 2, | ||
| + 2 => 4, | ||
| + 3 => 8 | ||
| +]; | ||
| + | ||
| +list(1 => $two_1, 2 => $two_2, 3 => $two_3) = $powersOfTwo; | ||
| +var_dump($two_1, $two_2, $two_3); | ||
| + | ||
| +echo PHP_EOL; | ||
| + | ||
| +$contrivedMixedKeyTypesExample = [ | ||
| + 7 => "the best PHP version", | ||
| + "elePHPant" => "the cutest mascot" | ||
| +]; | ||
| + | ||
| +list(7 => $seven, "elePHPant" => $elePHPant) = $contrivedMixedKeyTypesExample; | ||
| +var_dump($seven, $elePHPant); | ||
| + | ||
| +echo PHP_EOL; | ||
| + | ||
| +$allTogetherNow = [ | ||
| + "antonyms" => $antonyms, | ||
| + "powersOfTwo" => $powersOfTwo, | ||
| + "contrivedMixedKeyTypesExample" => $contrivedMixedKeyTypesExample | ||
| +]; | ||
| + | ||
| +list( | ||
| + "antonyms" => list("good" => $good_antonym, "happy" => $happy_antonym), | ||
| + "powersOfTwo" => list(1 => $two_1, 2 => $two_2, 3 => $two_3), | ||
| + "contrivedMixedKeyTypesExample" => list(7 => $seven, "elePHPant" => $elePHPant) | ||
| +) = $allTogetherNow; | ||
| + | ||
| +var_dump($good_antonym, $happy_antonym); | ||
| +var_dump($two_1, $two_2, $two_3); | ||
| +var_dump($seven, $elePHPant); | ||
| + | ||
| +?> | ||
| +--EXPECT-- | ||
| +string(3) "bad" | ||
| +string(3) "sad" | ||
| + | ||
| +int(2) | ||
| +int(4) | ||
| +int(8) | ||
| + | ||
| +string(20) "the best PHP version" | ||
| +string(17) "the cutest mascot" | ||
| + | ||
| +string(3) "bad" | ||
| +string(3) "sad" | ||
| +int(2) | ||
| +int(4) | ||
| +int(8) | ||
| +string(20) "the best PHP version" | ||
| +string(17) "the cutest mascot" |
Oops, something went wrong.
0 comments on commit
0b1a497