Skip to content

Commit 70942e4

Browse files
Add test for evaluation order of nested list() keys
1 parent ed3592e commit 70942e4

File tree

3 files changed

+146
-59
lines changed

3 files changed

+146
-59
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types=1);
2+
3+
// Observer objects for the Zend/tests/list_keyed_evaluation_order.* tests
4+
5+
class Stringable
6+
{
7+
private $name;
8+
public function __construct(string $name) {
9+
$this->name = $name;
10+
}
11+
12+
public function __toString(): string {
13+
echo "$this->name evaluated.", PHP_EOL;
14+
return $this->name;
15+
}
16+
}
17+
18+
class Indexable implements ArrayAccess
19+
{
20+
private $array;
21+
public function __construct(array $array) {
22+
$this->array = $array;
23+
}
24+
25+
public function offsetExists($offset): bool {
26+
echo "Existence of offset $offset checked for.", PHP_EOL;
27+
return isset($this->array[$offset]);
28+
}
29+
30+
public function offsetUnset($offset): void {
31+
unset($this->array[$offset]);
32+
echo "Offset $offset removed.", PHP_EOL;
33+
}
34+
35+
public function offsetGet($offset) {
36+
echo "Offset $offset retrieved.", PHP_EOL;
37+
return $this->array[$offset];
38+
}
39+
40+
public function offsetSet($offset, $value): void {
41+
$this->array[$offset] = $value;
42+
echo "Offset $offset set to $value.", PHP_EOL;
43+
}
44+
}
45+
46+
class IndexableRetrievable
47+
{
48+
private $label;
49+
private $indexable;
50+
51+
public function __construct(string $label, Indexable $indexable) {
52+
$this->label = $label;
53+
$this->indexable = $indexable;
54+
}
55+
56+
public function getIndexable(): Indexable {
57+
echo "Indexable $this->label retrieved.", PHP_EOL;
58+
return $this->indexable;
59+
}
60+
}

Zend/tests/list_keyed_evaluation_order.phpt

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ list() with keys, evaluation order
33
--FILE--
44
<?php
55

6+
require_once "list_keyed_evaluation_order.inc";
7+
8+
$a = new Stringable("A");
9+
$c = new Stringable("C");
10+
11+
$e = new IndexableRetrievable("E", new Indexable(["A" => "value for offset A", "C" => "value for offset C"]));
12+
13+
$store = new Indexable([]);
14+
615
// list($a => $b, $c => $d) = $e;
716
// Should be evaluated in the order:
817
// 1. Evaluate $e
@@ -13,65 +22,6 @@ list() with keys, evaluation order
1322
// 6. Evaluate $e[$c]
1423
// 7. Assign $c from $e[$a]
1524

16-
// In order to observe this evaluation order, let's use some observer objects!
17-
18-
class Stringable
19-
{
20-
private $name;
21-
public function __construct(string $name) {
22-
$this->name = $name;
23-
}
24-
25-
public function __toString(): string {
26-
echo "$this->name evaluated.", PHP_EOL;
27-
return $this->name;
28-
}
29-
}
30-
31-
class Indexable implements ArrayAccess
32-
{
33-
public function offsetExists($offset): bool {
34-
echo "Existence of offset $offset checked for.", PHP_EOL;
35-
return true;
36-
}
37-
38-
public function offsetUnset($offset): void {
39-
echo "Offset $offset removed.", PHP_EOL;
40-
}
41-
42-
public function offsetGet($offset) {
43-
echo "Offset $offset retrieved.", PHP_EOL;
44-
return "value for offset $offset";
45-
}
46-
47-
public function offsetSet($offset, $value): void {
48-
echo "Offset $offset set to $value.", PHP_EOL;
49-
}
50-
}
51-
52-
class IndexableRetrievable
53-
{
54-
private $label;
55-
private $indexable;
56-
57-
public function __construct(string $label, Indexable $indexable) {
58-
$this->label = $label;
59-
$this->indexable = $indexable;
60-
}
61-
62-
public function getIndexable(): Indexable {
63-
echo "Indexable $this->label retrieved.", PHP_EOL;
64-
return $this->indexable;
65-
}
66-
}
67-
68-
$a = new Stringable("A");
69-
$c = new Stringable("C");
70-
71-
$e = new IndexableRetrievable("E", new Indexable(["A" => "value for A", "C" => "value for C"]));
72-
73-
$store = new Indexable;
74-
7525
list((string)$a => $store["B"], (string)$c => $store["D"]) = $e->getIndexable();
7626

7727
?>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--TEST--
2+
list() with keys, evaluation order: nested
3+
--FILE--
4+
<?php
5+
6+
require_once "list_keyed_evaluation_order.inc";
7+
8+
$a = new Stringable("A");
9+
$c = new Stringable("C");
10+
$f = new Stringable("F");
11+
$g = new Stringable("G");
12+
$i = new Stringable("I");
13+
14+
$k = new IndexableRetrievable("K", new Indexable([
15+
"A" => "offset value for A",
16+
"C" => new Indexable([
17+
0 => "offset value for 0",
18+
1 => "offset value for 1"
19+
]),
20+
"F" => new Indexable([
21+
"G" => "offset value for G",
22+
"I" => "offset value for I"
23+
])
24+
]));
25+
26+
$store = new Indexable([]);
27+
28+
// list($a => $b, $c => list($d, $e), $f => list($g => $h, $i => $j)) = $k;
29+
// Should be evaluated in the order:
30+
// 1. Evaluate $k
31+
// 2. Evaluate $a
32+
// 3. Evaluate $k[$a]
33+
// 4. Assign $b from $k[$a]
34+
// 5. Evaluate $c
35+
// 6. Evaluate $k[$c]
36+
// 7. Evaluate $k[$c][0]
37+
// 8. Assign $d from $k[$c][0]
38+
// 9. Evaluate $k[$c][1]
39+
// 10. Assign $e from $k[$c][1]
40+
// 11. Evaluate $f
41+
// 12. Evaluate $k[$f]
42+
// 13. Evaluate $g
43+
// 14. Evaluate $k[$f][$g]
44+
// 15. Assign $h from $k[$f][$g]
45+
// 16. Evaluate $i
46+
// 17. Evaluate $k[$f][$i]
47+
// 18. Assign $j from $k[$f][$i]
48+
49+
list(
50+
(string)$a => $store["B"],
51+
(string)$c => list($store["D"], $store["E"]),
52+
(string)$f => list(
53+
(string)$g => $store["H"],
54+
(string)$i => $store["J"]
55+
)
56+
) = $k->getIndexable();
57+
58+
?>
59+
--EXPECT--
60+
Indexable K retrieved.
61+
A evaluated.
62+
Offset A retrieved.
63+
Offset B set to offset value for A.
64+
C evaluated.
65+
Offset C retrieved.
66+
Offset 0 retrieved.
67+
Offset D set to offset value for 0.
68+
Offset 1 retrieved.
69+
Offset E set to offset value for 1.
70+
F evaluated.
71+
Offset F retrieved.
72+
G evaluated.
73+
Offset G retrieved.
74+
Offset H set to offset value for G.
75+
I evaluated.
76+
Offset I retrieved.
77+
Offset J set to offset value for I.

0 commit comments

Comments
 (0)