Skip to content

Commit fa8b70a

Browse files
committed
Fix #394: Broken array expansion in foreach.'
1 parent 0d58c43 commit fa8b70a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

jphp-core/src/org/develnext/jphp/core/syntax/generators/ExprGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ protected void processForeach(ForeachStmtToken result, ListIterator<Token> itera
158158
if (!isOpenedBrace(next, BraceExprToken.Kind.SIMPLE))
159159
unexpectedToken(next, "(");
160160

161-
ExprStmtToken expr = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.AS, null);
161+
ExprStmtToken expr = analyzer.generator(SimpleExprGenerator.class)
162+
.getToken(nextToken(iterator), iterator, Separator.AS, null);
162163

163164
if (expr == null) {
164165
unexpectedToken(iterator.previous());
@@ -188,7 +189,7 @@ protected void processForeach(ForeachStmtToken result, ListIterator<Token> itera
188189
next = nextToken(iterator);
189190
}
190191

191-
if (next instanceof ListExprToken) {
192+
if (next instanceof ListExprToken || isOpenedBrace(next, ARRAY)) {
192193
ListExprToken listExpr = analyzer.generator(SimpleExprGenerator.class)
193194
.processSingleList(next, iterator);
194195

jphp-core/tests/org/develnext/jphp/core/compiler/jvm/LoopsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public void testForeachList() {
4747
check("loops/foreach_list_003.php", true);
4848
check("loops/foreach_list_004.php", true);
4949
}
50+
51+
@Test
52+
public void testBug394() {
53+
check("loops/bug394.phpt", true);
54+
}
5055
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Broken array expansion in foreach
3+
--FILE--
4+
<?php
5+
6+
foreach ([['a', 'b'], ['c', 'd'], ['x', 'y']] as $index => [$name, $value]) {
7+
var_dump($index, $name, $value);
8+
}
9+
?>
10+
--EXPECTF--
11+
int(0)
12+
string(1) "a"
13+
string(1) "b"
14+
int(1)
15+
string(1) "c"
16+
string(1) "d"
17+
int(2)
18+
string(1) "x"
19+
string(1) "y"

0 commit comments

Comments
 (0)