Skip to content

Commit d1f72d8

Browse files
Fix perf and mem issue when using token_get_all
1 parent bbbb079 commit d1f72d8

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function setPrefix($prefix)
8181
*/
8282
protected function normalizeToken($token)
8383
{
84-
if (is_array($token)) {
84+
if (isset($token[1]) && 'b"' !== $token) {
8585
return $token[1];
8686
}
8787

@@ -95,7 +95,7 @@ private function seekToNextRelevantToken(\Iterator $tokenIterator)
9595
{
9696
for (; $tokenIterator->valid(); $tokenIterator->next()) {
9797
$t = $tokenIterator->current();
98-
if (!is_array($t) || ($t[0] !== T_WHITESPACE)) {
98+
if (T_WHITESPACE !== $t[0]) {
9999
break;
100100
}
101101
}
@@ -112,7 +112,7 @@ private function getMessage(\Iterator $tokenIterator)
112112

113113
for (; $tokenIterator->valid(); $tokenIterator->next()) {
114114
$t = $tokenIterator->current();
115-
if (!is_array($t)) {
115+
if (!isset($t[1])) {
116116
break;
117117
}
118118

src/Symfony/Component/ClassLoader/ClassCollectionLoader.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ public static function fixNamespaceDeclarations($source)
149149
$inNamespace = false;
150150
$tokens = token_get_all($source);
151151

152-
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
153-
if (is_string($token)) {
152+
for ($i = 0; isset($tokens[$i]); ++$i) {
153+
$token = $tokens[$i];
154+
if (!isset($token[1]) || 'b"' === $token) {
154155
$rawChunk .= $token;
155156
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
156157
// strip comments
@@ -162,21 +163,21 @@ public static function fixNamespaceDeclarations($source)
162163
$rawChunk .= $token[1];
163164

164165
// namespace name and whitespaces
165-
while (($t = next($tokens)) && is_array($t) && in_array($t[0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
166-
$rawChunk .= $t[1];
166+
while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
167+
$rawChunk .= $tokens[$i][1];
167168
}
168-
if ('{' === $t) {
169+
if ('{' === $tokens[$i]) {
169170
$inNamespace = false;
170-
prev($tokens);
171+
--$i;
171172
} else {
172173
$rawChunk = rtrim($rawChunk)."\n{";
173174
$inNamespace = true;
174175
}
175176
} elseif (T_START_HEREDOC === $token[0]) {
176177
$output .= self::compressCode($rawChunk).$token[1];
177178
do {
178-
$token = next($tokens);
179-
$output .= is_string($token) ? $token : $token[1];
179+
$token = $tokens[++$i];
180+
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
180181
} while ($token[0] !== T_END_HEREDOC);
181182
$output .= "\n";
182183
$rawChunk = '';

src/Symfony/Component/ClassLoader/ClassMapGenerator.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ private static function findClasses($path)
9595
$classes = array();
9696

9797
$namespace = '';
98-
for ($i = 0, $max = count($tokens); $i < $max; ++$i) {
98+
for ($i = 0; isset($tokens[$i]); ++$i) {
9999
$token = $tokens[$i];
100100

101-
if (is_string($token)) {
101+
if (!isset($token[1])) {
102102
continue;
103103
}
104104

@@ -108,9 +108,9 @@ private static function findClasses($path)
108108
case T_NAMESPACE:
109109
$namespace = '';
110110
// If there is a namespace, extract it
111-
while (($t = $tokens[++$i]) && is_array($t)) {
112-
if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) {
113-
$namespace .= $t[1];
111+
while (isset($tokens[++$i][1])) {
112+
if (in_array($tokens[$i][0], array(T_STRING, T_NS_SEPARATOR))) {
113+
$namespace .= $tokens[$i][1];
114114
}
115115
}
116116
$namespace .= '\\';
@@ -121,7 +121,7 @@ private static function findClasses($path)
121121
// Skip usage of ::class constant
122122
$isClassConstant = false;
123123
for ($j = $i - 1; $j > 0; --$j) {
124-
if (is_string($tokens[$j])) {
124+
if (!isset($tokens[$j][1])) {
125125
break;
126126
}
127127

@@ -138,10 +138,11 @@ private static function findClasses($path)
138138
}
139139

140140
// Find the classname
141-
while (($t = $tokens[++$i]) && is_array($t)) {
141+
while (isset($tokens[++$i][1])) {
142+
$t = $tokens[$i];
142143
if (T_STRING === $t[0]) {
143144
$class .= $t[1];
144-
} elseif ($class !== '' && T_WHITESPACE == $t[0]) {
145+
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
145146
break;
146147
}
147148
}

src/Symfony/Component/HttpKernel/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]) {

src/Symfony/Component/HttpKernel/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;

src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ protected function findClass($file)
8888
$class = false;
8989
$namespace = false;
9090
$tokens = token_get_all(file_get_contents($file));
91-
for ($i = 0, $count = count($tokens); $i < $count; ++$i) {
91+
for ($i = 0; isset($tokens[$i]); ++$i) {
9292
$token = $tokens[$i];
9393

94-
if (!is_array($token)) {
94+
if (!isset($token[1])) {
9595
continue;
9696
}
9797

@@ -100,11 +100,11 @@ protected function findClass($file)
100100
}
101101

102102
if (true === $namespace && T_STRING === $token[0]) {
103-
$namespace = '';
104-
do {
105-
$namespace .= $token[1];
106-
$token = $tokens[++$i];
107-
} while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
103+
$namespace = $token[1];
104+
while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_NS_SEPARATOR, T_STRING))) {
105+
$namespace .= $tokens[$i][1];
106+
}
107+
$token = $tokens[$i];
108108
}
109109

110110
if (T_CLASS === $token[0]) {

0 commit comments

Comments
 (0)