Skip to content

Commit

Permalink
Fixup line numbers when applying emulator patches
Browse files Browse the repository at this point in the history
This fixes the test failures on PHP 7.2.
  • Loading branch information
nikic committed Aug 13, 2023
1 parent 62853b1 commit d1d784a
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/PhpParser/Lexer/Emulative.php
Expand Up @@ -132,10 +132,12 @@ private function fixupTokens(): void {

// We use a manual loop over the tokens, because we modify the array on the fly
$posDelta = 0;
$lineDelta = 0;
for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
$token = $this->tokens[$i];
$pos = $token->pos;
$token->pos += $posDelta;
$token->line += $lineDelta;
$localPosDelta = 0;
$len = \strlen($token->text);
while ($patchPos >= $pos && $patchPos < $pos + $len) {
Expand All @@ -153,12 +155,14 @@ private function fixupTokens(): void {
);
$localPosDelta -= $patchTextLen;
}
$lineDelta -= \substr_count($patchText, "\n");
} elseif ($patchType === 'add') {
// Insert into the token string
$token->text = substr_replace(
$token->text, $patchText, $patchPos - $pos + $localPosDelta, 0
);
$localPosDelta += $patchTextLen;
$lineDelta += \substr_count($patchText, "\n");
} elseif ($patchType === 'replace') {
// Replace inside the token string
$token->text = substr_replace(
Expand Down

0 comments on commit d1d784a

Please sign in to comment.