/** * @dataProvider provideTestLexNewFeatures */ public function testLeaveStuffAloneInStrings($code) { $stringifiedToken = '"' . addcslashes($code, '"\\') . '"'; $this->lexer->startLexing('<?php ' . $stringifiedToken); $this->assertEquals(Parser::T_CONSTANT_ENCAPSED_STRING, $this->lexer->getNextToken($text)); $this->assertEquals($stringifiedToken, $text); $this->assertEquals(0, $this->lexer->getNextToken()); }
public function getNextToken(&$value = NULL, &$startAttributes = NULL, &$endAttributes = NULL) { $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes); if ($tokenId == Tokens::T_CONSTANT_ENCAPSED_STRING || $tokenId == Tokens::T_LNUMBER || $tokenId == Tokens::T_DNUMBER) { $endAttributes['originalValue'] = $value; } return $tokenId; }
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) { $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes); if ($tokenId == Parser::T_CONSTANT_ENCAPSED_STRING || $tokenId == Parser::T_LNUMBER || $tokenId == Parser::T_DNUMBER) { // could also use $startAttributes, doesn't really matter here $endAttributes['originalValue'] = $value; } return $tokenId; }
/** * Backports PHP7 code to PHP5. * * @param string $code * @return string ported code * @throws PhpParser\Error */ public function port($code) { $lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array('startFilePos', 'endFilePos', 'startTokenPos'))); $parser = new PhpParser\Parser($lexer); $traverser = new PhpParser\NodeTraverser(); $parsedStatements = $parser->parse($code); $tokens = new Tokens($lexer->getTokens()); $patchFactory = new PatchFactory($tokens); $patches = new PatchCollection(); $traverser->addVisitor(new Visitor\Coalesce($patchFactory, $patches)); $traverser->addVisitor(new Visitor\ReturnType($patchFactory, $patches)); $traverser->addVisitor(new Visitor\ScalarTypehint($patchFactory, $patches)); $traverser->addVisitor(new Visitor\Spaceship($patchFactory, $patches)); $traverser->addVisitor(new Visitor\AnonymousClass($patchFactory, $patches)); $traverser->addVisitor(new Visitor\IntDiv($patchFactory, $patches)); $traverser->traverse($parsedStatements); $patcher = new Patcher($code); $patcher->apply($patches); return $patcher->getCode(); }
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) { $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes); if ($tokenId == Tokens::T_ARRAY) { $startAttributes['traditionalArray'] = true; } if ($tokenId == Tokens::T_EXIT) { $startAttributes['isDie'] = strtolower($value) === 'die'; } if ($tokenId == Tokens::T_CONSTANT_ENCAPSED_STRING) { $endAttributes['originalValue'] = $value; } return $tokenId; }
/** * Override to the native getNextToken method * * This method override ensures that the original value of tokens that would be transformed is stored * besides them in the result ast. Depending on the token type various attributes will be added to the token * and produced ast. These modifications are required to ensure the correct behaviour of the binary number * detection, the detection of booth flavors of the doc syntax, short array syntax and short echo tags. * * @param null|string $value Value of the token * @param null|array $startAttributes * @param null|array $endAttributes * @return int Retrieved token id * @see https://github.com/nikic/PHP-Parser/issues/26#issuecomment-6150035 Original implementation */ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) { $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes); if ($tokenId === Tokens::T_LNUMBER || $tokenId === Tokens::T_DNUMBER) { // could also use $startAttributes, doesn't really matter here $endAttributes['originalValue'] = $value; } elseif ($tokenId === Tokens::T_START_HEREDOC) { $startAttributes['isDocSyntax'] = true; if (substr($this->code, $startAttributes['startFilePos'] + 3, 1) === '\'') { $startAttributes['isNowDoc'] = true; } else { $startAttributes['isHereDoc'] = true; } } elseif ($tokenId === Tokens::T_ARRAY) { $startAttributes['traditionalArray'] = true; } elseif ($tokenId === Tokens::T_ECHO) { if (substr($this->code, $startAttributes['startFilePos'], 3) === '<?=') { $startAttributes['isShortEchoTag'] = true; } } return $tokenId; }