Skip to content

Commit 5315b85

Browse files
committed
Extractor::prepareReplacements() iterates backwards
1 parent da08300 commit 5315b85

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,11 @@ private function prepareReplacements(array $statements): array
147147
private function performReplacements(string $s, array $replacements): string
148148
{
149149
usort($replacements, function ($a, $b) { // sort by position in file
150-
return $a[0] <=> $b[0];
150+
return $b[0] <=> $a[0];
151151
});
152152

153-
$correctiveOffset = 0;
154153
foreach ($replacements as [$start, $end, $replacement]) {
155-
$replacingStringLength = $end - $start + 1;
156-
$s = substr_replace(
157-
$s,
158-
$replacement,
159-
$correctiveOffset + $start,
160-
$replacingStringLength
161-
);
162-
$correctiveOffset += strlen($replacement) - $replacingStringLength;
154+
$s = substr_replace($s, $replacement, $start, $end - $start + 1);
163155
}
164156
return $s;
165157
}

0 commit comments

Comments
 (0)