From 21cf67f1b4ae1fa2fa6c784ef7b0a546d6da1df8 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Thu, 7 Jul 2016 12:14:54 +0100 Subject: [PATCH] Minor refactor of SS_ConfigStaticManifest_Parser::parse() (#5782) --- core/manifest/ConfigStaticManifest.php | 28 +++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/core/manifest/ConfigStaticManifest.php b/core/manifest/ConfigStaticManifest.php index 36497c87c..c3ce9d6d1 100644 --- a/core/manifest/ConfigStaticManifest.php +++ b/core/manifest/ConfigStaticManifest.php @@ -198,6 +198,25 @@ class SS_ConfigStaticManifest_Parser { return $next; } + /** + * Get the previous token processed. Does *not* decrement the pointer + * + * @param bool $ignoreWhitespace - if true will skip any whitespace tokens & only return non-whitespace ones + * @return null | mixed - Either the previous token or null if there isn't one + */ + protected function lastToken($ignoreWhitespace = true) { + // Subtract 1 as the pointer is always 1 place ahead of the current token + $pos = $this->pos - 1; + do { + if($pos <= 0) return null; + $pos--; + $prev = $this->tokens[$pos]; + } + while($ignoreWhitespace && is_array($prev) && $prev[0] == T_WHITESPACE); + + return $prev; + } + /** * Get the next set of tokens that form a string to process, * incrementing the pointer @@ -242,15 +261,10 @@ class SS_ConfigStaticManifest_Parser { $type = ($token === (array)$token) ? $token[0] : $token; if($type == T_CLASS) { - // Fetch the last token - $pos = $this->pos - 1; // Subtract 1 as the pointer is always 1 place ahead of the current token - do { - $pos--; - $prev = $this->tokens[$pos]; - } while ($pos > 0 && is_array($prev) && $prev[0] == T_WHITESPACE); + $lastToken = $this->lastToken(); + $lastType = ($lastToken === (array)$lastToken) ? $lastToken[0] : $lastToken; // Ignore class keyword if it's being used for class name resolution: ClassName::class - $lastType = ($prev === (array)$prev) ? $prev[0] : $prev; if ($lastType === T_PAAMAYIM_NEKUDOTAYIM) { continue; }