From b41bd1a0ac4b55653940390e794953f0a016dca5 Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Tue, 18 Aug 2020 15:01:29 +1200 Subject: [PATCH] Add support for new PHP8 T_NAME_xxx token PHP8 related changes to namespace parsing. --- src/StaticAnalyser.php | 19 +++++++++++++------ src/functions.php | 8 ++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/StaticAnalyser.php b/src/StaticAnalyser.php index adcaad382..30d8b064c 100644 --- a/src/StaticAnalyser.php +++ b/src/StaticAnalyser.php @@ -450,7 +450,7 @@ private function parseNamespace(&$tokens, &$token, $parseContext) $namespace = ''; while ($token !== false) { $token = $this->nextToken($tokens, $parseContext); - if ($token[0] !== T_STRING && $token[0] !== T_NS_SEPARATOR) { + if (!in_array($token[0], [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED])) { break; } $namespace .= $token[1]; @@ -480,13 +480,20 @@ private function parseNamespaceList(&$tokens, &$token, $parseContext) */ private function parseUseStatement(&$tokens, &$token, $parseContext) { + $normalizeAlias = function ($alias) { + $alias = ltrim($alias, '\\'); + $elements = explode('\\', $alias); + + return array_pop($elements); + }; + $class = ''; $alias = ''; $statements = []; $explicitAlias = false; while ($token !== false) { $token = $this->nextToken($tokens, $parseContext); - $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; + $isNameToken = in_array($token[0], [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED]); if (!$explicitAlias && $isNameToken) { $class .= $token[1]; $alias = $token[1]; @@ -496,12 +503,12 @@ private function parseUseStatement(&$tokens, &$token, $parseContext) $explicitAlias = true; $alias = ''; } elseif ($token === ',') { - $statements[$alias] = $class; + $statements[$normalizeAlias($alias)] = $class; $class = ''; $alias = ''; $explicitAlias = false; } elseif ($token === ';') { - $statements[$alias] = $class; + $statements[$normalizeAlias($alias)] = $class; break; } else { break; @@ -530,8 +537,8 @@ private function parseTypeAndNextToken(array &$tokens, Context $parseContext): a } // drill down namespace segments to basename property type declaration - while (in_array($token[0], [T_NS_SEPARATOR, T_STRING, T_ARRAY])) { - if ($token[0] === T_STRING) { + while (in_array($token[0], [T_NS_SEPARATOR, T_STRING, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED, T_ARRAY])) { + if (in_array($token[0], [T_STRING, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED])) { $type = $token[1]; } $token = $this->nextToken($tokens, $parseContext); diff --git a/src/functions.php b/src/functions.php index 6ebc86c55..c9004d7ad 100644 --- a/src/functions.php +++ b/src/functions.php @@ -18,6 +18,14 @@ define('OpenApi\Processors\UNDEFINED', UNDEFINED); } +if (false === defined('T_NAME_QUALIFIED')) { + /* + * PHP8 only token. + */ + define('T_NAME_QUALIFIED', -1); + define('T_NAME_FULLY_QUALIFIED', -1); +} + if (function_exists('OpenApi\scan') === false) { /** * Scan the filesystem for OpenAPI annotations and build openapi-documentation.