@@ -132,40 +132,23 @@ public function process(File $phpcsFile, $stackPtr)
132132 if ($ startOfType !== $ constName ) {
133133 $ endOfType = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ constName - 1 ), null , true );
134134
135- $ type = '' ;
136- $ isUnionType = false ;
137- $ isIntersectionType = false ;
138- for ($ j = $ startOfType ; $ j <= $ endOfType ; $ j ++) {
139- if (isset ($ ignore [$ tokens [$ j ]['code ' ]]) === true ) {
140- continue ;
141- }
142-
143- if ($ tokens [$ j ]['code ' ] === T_TYPE_UNION ) {
144- $ isUnionType = true ;
145- }
146-
147- if ($ tokens [$ j ]['code ' ] === T_TYPE_INTERSECTION ) {
148- $ isIntersectionType = true ;
149- }
150-
151- $ type .= $ tokens [$ j ]['content ' ];
152- }
153-
154135 $ error = 'PHP constant type declarations must be lowercase; expected "%s" but found "%s" ' ;
155136 $ errorCode = 'ConstantTypeFound ' ;
156137
157- if ($ isIntersectionType === true ) {
158- // Intersection types don't support simple types.
159- } else if ($ isUnionType === true ) {
138+ if ($ startOfType !== $ endOfType ) {
139+ // Multi-token type.
160140 $ this ->processUnionType (
161141 $ phpcsFile ,
162142 $ startOfType ,
163143 $ endOfType ,
164144 $ error ,
165145 $ errorCode
166146 );
167- } else if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
168- $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
147+ } else {
148+ $ type = $ tokens [$ startOfType ]['content ' ];
149+ if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
150+ $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
151+ }
169152 }
170153 }//end if
171154
@@ -195,9 +178,8 @@ public function process(File $phpcsFile, $stackPtr)
195178 $ error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s" ' ;
196179 $ errorCode = 'PropertyTypeFound ' ;
197180
198- if (strpos ($ type , '& ' ) !== false ) {
199- // Intersection types don't support simple types.
200- } else if (strpos ($ type , '| ' ) !== false ) {
181+ if ($ props ['type_token ' ] !== $ props ['type_end_token ' ]) {
182+ // Multi-token type.
201183 $ this ->processUnionType (
202184 $ phpcsFile ,
203185 $ props ['type_token ' ],
@@ -227,9 +209,8 @@ public function process(File $phpcsFile, $stackPtr)
227209 $ error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s" ' ;
228210 $ errorCode = 'ReturnTypeFound ' ;
229211
230- if (strpos ($ returnType , '& ' ) !== false ) {
231- // Intersection types don't support simple types.
232- } else if (strpos ($ returnType , '| ' ) !== false ) {
212+ if ($ props ['return_type_token ' ] !== $ props ['return_type_end_token ' ]) {
213+ // Multi-token type.
233214 $ this ->processUnionType (
234215 $ phpcsFile ,
235216 $ props ['return_type_token ' ],
@@ -259,9 +240,8 @@ public function process(File $phpcsFile, $stackPtr)
259240 $ error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s" ' ;
260241 $ errorCode = 'ParamTypeFound ' ;
261242
262- if (strpos ($ typeHint , '& ' ) !== false ) {
263- // Intersection types don't support simple types.
264- } else if (strpos ($ typeHint , '| ' ) !== false ) {
243+ if ($ param ['type_hint_token ' ] !== $ param ['type_hint_end_token ' ]) {
244+ // Multi-token type.
265245 $ this ->processUnionType (
266246 $ phpcsFile ,
267247 $ param ['type_hint_token ' ],
@@ -279,7 +259,9 @@ public function process(File $phpcsFile, $stackPtr)
279259
280260
281261 /**
282- * Processes a union type declaration.
262+ * Processes a multi-token type declaration.
263+ *
264+ * {@internal The method name is superseded by the reality, but changing it would be a BC-break.}
283265 *
284266 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
285267 * @param int $typeDeclStart The position of the start of the type token.
@@ -291,37 +273,51 @@ public function process(File $phpcsFile, $stackPtr)
291273 */
292274 protected function processUnionType (File $ phpcsFile , $ typeDeclStart , $ typeDeclEnd , $ error , $ errorCode )
293275 {
294- $ tokens = $ phpcsFile ->getTokens ();
295- $ current = $ typeDeclStart ;
296-
297- do {
298- $ endOfType = $ phpcsFile ->findNext (T_TYPE_UNION , $ current , $ typeDeclEnd );
299- if ($ endOfType === false ) {
300- // This must be the last type in the union.
301- $ endOfType = ($ typeDeclEnd + 1 );
302- }
276+ $ tokens = $ phpcsFile ->getTokens ();
277+ $ typeTokenCount = 0 ;
278+ $ typeStart = null ;
279+ $ type = '' ;
303280
304- $ hasNsSep = $ phpcsFile ->findNext (T_NS_SEPARATOR , $ current , $ endOfType );
305- if ($ hasNsSep !== false ) {
306- // Multi-token class based type. Ignore.
307- $ current = ($ endOfType + 1 );
281+ for ($ i = $ typeDeclStart ; $ i <= $ typeDeclEnd ; $ i ++) {
282+ if (isset (Tokens::$ emptyTokens [$ tokens [$ i ]['code ' ]]) === true ) {
308283 continue ;
309284 }
310285
311- // Type consisting of a single token.
312- $ startOfType = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ current , $ endOfType , true );
313- if ($ startOfType === false ) {
314- // Parse error.
315- return ;
286+ if ($ tokens [$ i ]['code ' ] === T_TYPE_UNION
287+ || $ tokens [$ i ]['code ' ] === T_TYPE_INTERSECTION
288+ || $ tokens [$ i ]['code ' ] === T_TYPE_OPEN_PARENTHESIS
289+ || $ tokens [$ i ]['code ' ] === T_TYPE_CLOSE_PARENTHESIS
290+ ) {
291+ if ($ typeTokenCount === 1
292+ && $ type !== ''
293+ && isset ($ this ->phpTypes [strtolower ($ type )]) === true
294+ ) {
295+ $ this ->processType ($ phpcsFile , $ typeStart , $ type , $ error , $ errorCode );
296+ }
297+
298+ // Reset for the next type in the type string.
299+ $ typeTokenCount = 0 ;
300+ $ typeStart = null ;
301+ $ type = '' ;
302+
303+ continue ;
316304 }
317305
318- $ type = $ tokens [$ startOfType ]['content ' ];
319- if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
320- $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
306+ if (isset ($ typeStart ) === false ) {
307+ $ typeStart = $ i ;
321308 }
322309
323- $ current = ($ endOfType + 1 );
324- } while ($ current <= $ typeDeclEnd );
310+ ++$ typeTokenCount ;
311+ $ type .= $ tokens [$ i ]['content ' ];
312+ }//end for
313+
314+ // Handle type at end of type string.
315+ if ($ typeTokenCount === 1
316+ && $ type !== ''
317+ && isset ($ this ->phpTypes [strtolower ($ type )]) === true
318+ ) {
319+ $ this ->processType ($ phpcsFile , $ typeStart , $ type , $ error , $ errorCode );
320+ }
325321
326322 }//end processUnionType()
327323
0 commit comments