Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for foreach with list statement (PHP 5.5)
  • Loading branch information
digilist committed Jul 22, 2014
1 parent b74f2bb commit a744af1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Expand Up @@ -3409,13 +3409,22 @@ private function parseForeachStatement()
if ($this->tokenizer->peek() === Tokens::T_BITWISE_AND) {
$foreach->addChild($this->parseVariableOrMemberByReference());
} else {
$foreach->addChild($this->parseVariableOrConstantOrPrimaryPrefix());
if ($this->tokenizer->peek() == Tokens::T_LIST) {
$foreach->addChild($this->parseListExpression());
} else {
$foreach->addChild($this->parseVariableOrConstantOrPrimaryPrefix());

if ($this->tokenizer->peek() === Tokens::T_DOUBLE_ARROW) {
$this->consumeToken(Tokens::T_DOUBLE_ARROW);
$foreach->addChild(
$this->parseVariableOrMemberOptionalByReference()
);
if ($this->tokenizer->peek() === Tokens::T_DOUBLE_ARROW) {
$this->consumeToken(Tokens::T_DOUBLE_ARROW);

if ($this->tokenizer->peek() == Tokens::T_LIST) {
$foreach->addChild($this->parseListExpression());
} else {
$foreach->addChild(
$this->parseVariableOrMemberOptionalByReference()
);
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTForeachStatementTest.php
Expand Up @@ -295,6 +295,28 @@ public function testForeachStatementTerminatedByPhpCloseTag()
$this->assertEquals(9, $stmt->getEndColumn());
}

/**
* testForeachStatementWithList
*
* @return void
*/
public function testForeachStatementWithList()
{
$stmt = $this->_getFirstForeachStatementInFunction(__METHOD__);
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTListExpression', $stmt->getChild(1));
}

/**
* testForeachStatementWithKeyAndList
*
* @return void
*/
public function testForeachStatementWithKeyAndList()
{
$stmt = $this->_getFirstForeachStatementInFunction(__METHOD__);
$this->assertInstanceOf('PDepend\\Source\\AST\\ASTListExpression', $stmt->getChild(2));
}

/**
* Returns a node instance for the currently executed test case.
*
Expand Down
@@ -0,0 +1,5 @@
<?php
function testForeachStatementWithKeyAndList()
{
foreach ($expr as $key => list($a, $b)) {}
}
@@ -0,0 +1,5 @@
<?php
function testForeachStatementWithList()
{
foreach ($expr as list($a, $b)) {}
}

0 comments on commit a744af1

Please sign in to comment.