Skip to content

Commit

Permalink
Merge pull request #830 from DerManoMann/php8-token
Browse files Browse the repository at this point in the history
Add support for new PHP8 T_NAME_xxx token
  • Loading branch information
DerManoMann authored Aug 24, 2020
2 parents 7a1ffb2 + b41bd1a commit 7f4b9e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/StaticAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7f4b9e0

Please sign in to comment.