diff --git a/lib/PhpParser/Internal/TokenStream.php b/lib/PhpParser/Internal/TokenStream.php index 39308ae629..766a5e5f4d 100644 --- a/lib/PhpParser/Internal/TokenStream.php +++ b/lib/PhpParser/Internal/TokenStream.php @@ -64,12 +64,19 @@ public function haveTokenImmediatelyBefore(int $pos, $expectedTokenType) : bool $tokens = $this->tokens; $pos--; for (; $pos >= 0; $pos--) { - $tokenType = $tokens[$pos][0]; + $token = $tokens[$pos]; + if (is_array($token)) { + $index = is_string($expectedTokenType) ? 1 : 0; + $tokenType = $token[$index]; + } else { + $tokenType = $token; + } if ($tokenType === $expectedTokenType) { return true; } - if ($tokenType !== \T_WHITESPACE - && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) { + if ( + $tokenType !== \T_WHITESPACE && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT && $tokenType !== ' ' + ) { break; } } @@ -90,12 +97,19 @@ public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType) : bool { $tokens = $this->tokens; $pos++; for (; $pos < \count($tokens); $pos++) { - $tokenType = $tokens[$pos][0]; + $token = $tokens[$pos]; + if (is_array($token)) { + $index = is_string($expectedTokenType) ? 1 : 0; + $tokenType = $token[$index]; + } else { + $tokenType = $token; + } if ($tokenType === $expectedTokenType) { return true; } - if ($tokenType !== \T_WHITESPACE - && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) { + if ( + $tokenType !== \T_WHITESPACE && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT && $tokenType !== ' ' + ) { break; } } diff --git a/test/code/formatPreservation/rewriteVariableInterpolationString.test b/test/code/formatPreservation/rewriteVariableInterpolationString.test new file mode 100644 index 0000000000..b37ff0b06f --- /dev/null +++ b/test/code/formatPreservation/rewriteVariableInterpolationString.test @@ -0,0 +1,9 @@ +Nop statement with comment at end (#513) +----- +expr->parts[0]->setAttribute('origNode', null); +----- +