Skip to content

Commit a744af1

Browse files
committed
Added support for foreach with list statement (PHP 5.5)
1 parent b74f2bb commit a744af1

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,13 +3409,22 @@ private function parseForeachStatement()
34093409
if ($this->tokenizer->peek() === Tokens::T_BITWISE_AND) {
34103410
$foreach->addChild($this->parseVariableOrMemberByReference());
34113411
} else {
3412-
$foreach->addChild($this->parseVariableOrConstantOrPrimaryPrefix());
3412+
if ($this->tokenizer->peek() == Tokens::T_LIST) {
3413+
$foreach->addChild($this->parseListExpression());
3414+
} else {
3415+
$foreach->addChild($this->parseVariableOrConstantOrPrimaryPrefix());
34133416

3414-
if ($this->tokenizer->peek() === Tokens::T_DOUBLE_ARROW) {
3415-
$this->consumeToken(Tokens::T_DOUBLE_ARROW);
3416-
$foreach->addChild(
3417-
$this->parseVariableOrMemberOptionalByReference()
3418-
);
3417+
if ($this->tokenizer->peek() === Tokens::T_DOUBLE_ARROW) {
3418+
$this->consumeToken(Tokens::T_DOUBLE_ARROW);
3419+
3420+
if ($this->tokenizer->peek() == Tokens::T_LIST) {
3421+
$foreach->addChild($this->parseListExpression());
3422+
} else {
3423+
$foreach->addChild(
3424+
$this->parseVariableOrMemberOptionalByReference()
3425+
);
3426+
}
3427+
}
34193428
}
34203429
}
34213430

src/test/php/PDepend/Source/AST/ASTForeachStatementTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ public function testForeachStatementTerminatedByPhpCloseTag()
295295
$this->assertEquals(9, $stmt->getEndColumn());
296296
}
297297

298+
/**
299+
* testForeachStatementWithList
300+
*
301+
* @return void
302+
*/
303+
public function testForeachStatementWithList()
304+
{
305+
$stmt = $this->_getFirstForeachStatementInFunction(__METHOD__);
306+
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTListExpression', $stmt->getChild(1));
307+
}
308+
309+
/**
310+
* testForeachStatementWithKeyAndList
311+
*
312+
* @return void
313+
*/
314+
public function testForeachStatementWithKeyAndList()
315+
{
316+
$stmt = $this->_getFirstForeachStatementInFunction(__METHOD__);
317+
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTListExpression', $stmt->getChild(2));
318+
}
319+
298320
/**
299321
* Returns a node instance for the currently executed test case.
300322
*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
function testForeachStatementWithKeyAndList()
3+
{
4+
foreach ($expr as $key => list($a, $b)) {}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
function testForeachStatementWithList()
3+
{
4+
foreach ($expr as list($a, $b)) {}
5+
}

0 commit comments

Comments
 (0)