Skip to content

Commit 5bec94d

Browse files
Fix perf and mem issue when using token_get_all
1 parent 56b9bb0 commit 5bec94d

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Kernel.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,14 +699,15 @@ public static function stripComments($source)
699699
$output = '';
700700
$tokens = token_get_all($source);
701701
$ignoreSpace = false;
702-
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
703-
if (is_string($token)) {
702+
for ($i = 0; isset($tokens[$i]); ++$i) {
703+
$token = $tokens[$i];
704+
if (!isset($token[1]) || 'b"' === $token) {
704705
$rawChunk .= $token;
705706
} elseif (T_START_HEREDOC === $token[0]) {
706707
$output .= $rawChunk.$token[1];
707708
do {
708-
$token = next($tokens);
709-
$output .= $token[1];
709+
$token = $tokens[++$i];
710+
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
710711
} while ($token[0] !== T_END_HEREDOC);
711712
$rawChunk = '';
712713
} elseif (T_WHITESPACE === $token[0]) {

Tests/KernelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function testStripComments()
307307
$heredoc = <<<HD
308308
309309
310-
Heredoc should not be modified
310+
Heredoc should not be modified {$a[1+$b]}
311311
312312
313313
HD;
@@ -343,7 +343,7 @@ public function doStuff()
343343
$heredoc = <<<HD
344344
345345
346-
Heredoc should not be modified
346+
Heredoc should not be modified {$a[1+$b]}
347347
348348
349349
HD;

0 commit comments

Comments
 (0)