tolerant-php-parser icon indicating copy to clipboard operation
tolerant-php-parser copied to clipboard

ForeachStatement->foreachKey with an key in the statement returns `$key =>`

Open jens1o opened this issue 8 years ago • 1 comments
trafficstars

See: image

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;
}

jens1o avatar Jun 10 '17 12:06 jens1o

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'
    ];
}

TysonAndre avatar Mar 17 '18 19:03 TysonAndre