function TokenStream::getTokenCode
Get the code corresponding to a token offset range, optionally adjusted for indentation.
Parameters
int $from Token start position (inclusive):
int $to Token end position (exclusive):
int $indent By how much the code should be indented (can be negative as well):
Return value
string Code corresponding to token range, adjusted for indentation
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Internal/ TokenStream.php, line 223
Class
- TokenStream
- Provides operations on token streams, for use by pretty printer.
Namespace
PhpParser\InternalCode
public function getTokenCode(int $from, int $to, int $indent) : string {
$tokens = $this->tokens;
$result = '';
for ($pos = $from; $pos < $to; $pos++) {
$token = $tokens[$pos];
$id = $token->id;
$text = $token->text;
if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
$result .= $text;
}
else {
// TODO Handle non-space indentation
if ($indent < 0) {
$result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $text);
}
elseif ($indent > 0) {
$result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $text);
}
else {
$result .= $text;
}
}
}
return $result;
}