diff --git a/src/Token/Stream.php b/src/Token/Stream.php index 2721f82..1295ec3 100644 --- a/src/Token/Stream.php +++ b/src/Token/Stream.php @@ -150,6 +150,7 @@ public function getFilename() */ protected function scan($sourceCode) { + $id = 0; $line = 1; $tokens = token_get_all($sourceCode); $numTokens = count($tokens); @@ -158,7 +159,7 @@ protected function scan($sourceCode) for ($i = 0; $i < $numTokens; ++$i) { $token = $tokens[$i]; - unset($tokens[$i]); + $skip = 0; if (is_array($token)) { $name = substr(token_name($token[0]), 2); @@ -167,12 +168,8 @@ protected function scan($sourceCode) if ($lastNonWhitespaceTokenWasDoubleColon && $name == 'CLASS') { $name = 'CLASS_NAME_CONSTANT'; } elseif ($name == 'USE' && isset($tokens[$i + 2][0]) && $tokens[$i + 2][0] == T_FUNCTION) { - unset($tokens[$i + 1]); - unset($tokens[$i + 2]); - - $i += 2; - $name = 'USE_FUNCTION'; + $skip = 2; } $tokenClass = 'PHP_Token_' . $name; @@ -181,7 +178,7 @@ protected function scan($sourceCode) $tokenClass = self::$customTokens[$token]; } - $this->tokens[] = new $tokenClass($text, $line, $this, $i); + $this->tokens[] = new $tokenClass($text, $line, $this, $id++); $lines = substr_count($text, "\n"); $line += $lines; @@ -197,6 +194,8 @@ protected function scan($sourceCode) } elseif ($name != 'WHITESPACE') { $lastNonWhitespaceTokenWasDoubleColon = false; } + + $i += $skip; } $this->linesOfCode['loc'] = substr_count($sourceCode, "\n"); diff --git a/tests/Token/ClassTest.php b/tests/Token/ClassTest.php index a5f0ace..300d8f0 100644 --- a/tests/Token/ClassTest.php +++ b/tests/Token/ClassTest.php @@ -115,5 +115,6 @@ public function testImportedFunctionsAreHandledCorrectly() $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php'); $this->assertEmpty($ts->getFunctions()); + $this->assertCount(1, $ts->getClasses()); } }