@@ -15,7 +15,7 @@ class TokenParser
15
15
/**
16
16
* The token list.
17
17
*
18
- * @var array
18
+ * @var list<mixed[]>
19
19
*/
20
20
private $ tokens ;
21
21
@@ -57,24 +57,25 @@ public function __construct($contents)
57
57
*
58
58
* @param string $namespaceName The namespace name of the reflected class.
59
59
*
60
- * @return array A list with all found use statements.
60
+ * @return array<string, string> A list with all found use statements.
61
61
*/
62
62
public function parseUseStatements ($ namespaceName )
63
63
{
64
- $ statements = array () ;
64
+ $ statements = [] ;
65
65
while (($ token = $ this ->next ())) {
66
66
if ($ token [0 ] === T_USE ) {
67
67
$ statements = array_merge ($ statements , $ this ->parseUseStatement ());
68
68
continue ;
69
69
}
70
- if ($ token [0 ] !== T_NAMESPACE || $ this ->parseNamespace () != $ namespaceName ) {
70
+
71
+ if ($ token [0 ] !== T_NAMESPACE || $ this ->parseNamespace () !== $ namespaceName ) {
71
72
continue ;
72
73
}
73
74
74
75
// Get fresh array for new namespace. This is to prevent the parser to collect the use statements
75
76
// for a previous namespace with the same name. This is the case if a namespace is defined twice
76
77
// or if a namespace with the same name is commented out.
77
- $ statements = array () ;
78
+ $ statements = [] ;
78
79
}
79
80
80
81
return $ statements ;
@@ -83,19 +84,20 @@ public function parseUseStatements($namespaceName)
83
84
/**
84
85
* Gets the next non whitespace and non comment token.
85
86
*
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.
88
89
*
89
- * @return array |null The token if exists, null otherwise.
90
+ * @return mixed[]|string |null The token if exists, null otherwise.
90
91
*/
91
92
private function next ($ docCommentIsComment = true )
92
93
{
93
94
for ($ i = $ this ->pointer ; $ i < $ this ->numTokens ; $ i ++) {
94
95
$ this ->pointer ++;
95
- if ($ this ->tokens [$ i ][0 ] === T_WHITESPACE ||
96
+ if (
97
+ $ this ->tokens [$ i ][0 ] === T_WHITESPACE ||
96
98
$ 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
+ ) {
99
101
continue ;
100
102
}
101
103
@@ -108,7 +110,7 @@ private function next($docCommentIsComment = true)
108
110
/**
109
111
* Parses a single use statement.
110
112
*
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.
112
114
*/
113
115
private function parseUseStatement ()
114
116
{
@@ -118,12 +120,22 @@ private function parseUseStatement()
118
120
$ statements = [];
119
121
$ explicitAlias = false ;
120
122
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 ) {
123
124
$ class .= $ token [1 ];
124
125
$ 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 = '' ;
127
139
} elseif ($ token [0 ] === T_AS ) {
128
140
$ explicitAlias = true ;
129
141
$ alias = '' ;
@@ -135,10 +147,10 @@ private function parseUseStatement()
135
147
} elseif ($ token === '; ' ) {
136
148
$ statements [strtolower ($ alias )] = $ groupRoot . $ class ;
137
149
break ;
138
- } else if ($ token === '{ ' ) {
150
+ } elseif ($ token === '{ ' ) {
139
151
$ groupRoot = $ class ;
140
152
$ class = '' ;
141
- } else if ($ token === '} ' ) {
153
+ } elseif ($ token === '} ' ) {
142
154
continue ;
143
155
} else {
144
156
break ;
@@ -156,7 +168,12 @@ private function parseUseStatement()
156
168
private function parseNamespace ()
157
169
{
158
170
$ 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
+ ) {
160
177
$ name .= $ token [1 ];
161
178
}
162
179
0 commit comments