tolerant-php-parser
tolerant-php-parser copied to clipboard
ForeachStatement->foreachKey with an key in the statement returns `$key =>`
trafficstars
See:

Test script:
<?php
use Microsoft\PhpParser\Parser;
require_once './vendor/autoload.php';
$parser = new Parser;
$ast = $parser->parseSourceFile(file_get_contents('source.php'));
var_dump($ast->statementList[1]);
exit;
Source.php:
<?php
foreach ([0, 1, 2, 3, 4] as $array => $index) {
echo $array;
}
I don't see the bug. Shouldn't the code just use $ast->statementList[1]->foreachKey->expression->getText() instead?
getText seems like it's doing exactly what every other Node is doing: returning the concatenated text for the ranges of the child nodes.
$foreachStatement->forEachCollectionName = $this->parseExpression($foreachStatement);
$foreachStatement->asKeyword = $this->eat1(TokenKind::AsKeyword);
$foreachStatement->foreachKey = $this->tryParseForeachKey($foreachStatement);
$foreachStatement->foreachValue = $this->parseForeachValue($foreachStatement);
$foreachStatement->closeParen = $this->eat1(TokenKind::CloseParenToken);
$foreachStatement->colon = $this->eatOptional1(TokenKind::ColonToken);
class ForeachKey extends Node {
/** @var Expression */
public $expression;
/** @var Token */
public $arrow;
const CHILD_NAMES = [
'expression',
'arrow'
];
}