Skip to content

Commit 8388130

Browse files
committed
Fix TokenParser
1 parent ec698f6 commit 8388130

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

src/PhpDocReader/PhpParser/TokenParser.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TokenParser
1515
/**
1616
* The token list.
1717
*
18-
* @var array
18+
* @var list<mixed[]>
1919
*/
2020
private $tokens;
2121

@@ -57,24 +57,25 @@ public function __construct($contents)
5757
*
5858
* @param string $namespaceName The namespace name of the reflected class.
5959
*
60-
* @return array A list with all found use statements.
60+
* @return array<string, string> A list with all found use statements.
6161
*/
6262
public function parseUseStatements($namespaceName)
6363
{
64-
$statements = array();
64+
$statements = [];
6565
while (($token = $this->next())) {
6666
if ($token[0] === T_USE) {
6767
$statements = array_merge($statements, $this->parseUseStatement());
6868
continue;
6969
}
70-
if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
70+
71+
if ($token[0] !== T_NAMESPACE || $this->parseNamespace() !== $namespaceName) {
7172
continue;
7273
}
7374

7475
// Get fresh array for new namespace. This is to prevent the parser to collect the use statements
7576
// for a previous namespace with the same name. This is the case if a namespace is defined twice
7677
// or if a namespace with the same name is commented out.
77-
$statements = array();
78+
$statements = [];
7879
}
7980

8081
return $statements;
@@ -83,19 +84,20 @@ public function parseUseStatements($namespaceName)
8384
/**
8485
* Gets the next non whitespace and non comment token.
8586
*
86-
* @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
87-
* If FALSE then only whitespace and normal comments are skipped.
87+
* @param bool $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
88+
* If FALSE then only whitespace and normal comments are skipped.
8889
*
89-
* @return array|null The token if exists, null otherwise.
90+
* @return mixed[]|string|null The token if exists, null otherwise.
9091
*/
9192
private function next($docCommentIsComment = true)
9293
{
9394
for ($i = $this->pointer; $i < $this->numTokens; $i++) {
9495
$this->pointer++;
95-
if ($this->tokens[$i][0] === T_WHITESPACE ||
96+
if (
97+
$this->tokens[$i][0] === T_WHITESPACE ||
9698
$this->tokens[$i][0] === T_COMMENT ||
97-
($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
98-
99+
($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)
100+
) {
99101
continue;
100102
}
101103

@@ -108,7 +110,7 @@ private function next($docCommentIsComment = true)
108110
/**
109111
* Parses a single use statement.
110112
*
111-
* @return array A list with all found class names for a use statement.
113+
* @return array<string, string> A list with all found class names for a use statement.
112114
*/
113115
private function parseUseStatement()
114116
{
@@ -118,12 +120,22 @@ private function parseUseStatement()
118120
$statements = [];
119121
$explicitAlias = false;
120122
while (($token = $this->next())) {
121-
$isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
122-
if (!$explicitAlias && $isNameToken) {
123+
if (! $explicitAlias && $token[0] === T_STRING) {
123124
$class .= $token[1];
124125
$alias = $token[1];
125-
} elseif ($explicitAlias && $isNameToken) {
126-
$alias .= $token[1];
126+
} elseif ($explicitAlias && $token[0] === T_STRING) {
127+
$alias = $token[1];
128+
} elseif (
129+
PHP_VERSION_ID >= 80000 &&
130+
($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED)
131+
) {
132+
$class .= $token[1];
133+
134+
$classSplit = explode('\\', $token[1]);
135+
$alias = $classSplit[count($classSplit) - 1];
136+
} elseif ($token[0] === T_NS_SEPARATOR) {
137+
$class .= '\\';
138+
$alias = '';
127139
} elseif ($token[0] === T_AS) {
128140
$explicitAlias = true;
129141
$alias = '';
@@ -135,10 +147,10 @@ private function parseUseStatement()
135147
} elseif ($token === ';') {
136148
$statements[strtolower($alias)] = $groupRoot . $class;
137149
break;
138-
} else if ($token === '{' ) {
150+
} elseif ($token === '{') {
139151
$groupRoot = $class;
140152
$class = '';
141-
} else if ($token === '}' ) {
153+
} elseif ($token === '}') {
142154
continue;
143155
} else {
144156
break;
@@ -156,7 +168,12 @@ private function parseUseStatement()
156168
private function parseNamespace()
157169
{
158170
$name = '';
159-
while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
171+
while (
172+
($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR || (
173+
PHP_VERSION_ID >= 80000 &&
174+
($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED)
175+
))
176+
) {
160177
$name .= $token[1];
161178
}
162179

0 commit comments

Comments
 (0)