@@ -105,6 +105,15 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
105105 continue ;
106106 }
107107
108+ /** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
109+ $ variableAnnotationType = $ variableAnnotationType ;
110+
111+ $ assertion = $ this ->createAssert ($ variableAnnotation ->getVariableName (), $ variableAnnotationType );
112+
113+ if ($ assertion === null ) {
114+ continue ;
115+ }
116+
108117 if ($ tokens [$ codePointer ]['code ' ] === T_VARIABLE ) {
109118 $ pointerAfterVariable = TokenHelper::findNextEffective ($ phpcsFile , $ codePointer + 1 );
110119 if ($ tokens [$ pointerAfterVariable ]['code ' ] !== T_EQUAL ) {
@@ -245,11 +254,6 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
245254 }
246255 }
247256
248- /** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
249- $ variableAnnotationType = $ variableAnnotationType ;
250-
251- $ assertion = $ this ->createAssert ($ variableAnnotation ->getVariableName (), $ variableAnnotationType );
252-
253257 if (
254258 $ pointerToAddAssertion < $ docCommentClosePointer
255259 && array_key_exists ($ pointerAfterDocComment + 1 , $ tokens )
@@ -265,15 +269,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
265269
266270 private function isValidTypeNode (TypeNode $ typeNode ): bool
267271 {
268- if ($ typeNode instanceof ThisTypeNode) {
269- return true ;
270- }
271-
272- if (!$ typeNode instanceof IdentifierTypeNode) {
273- return false ;
274- }
275-
276- return !in_array ($ typeNode ->name , ['mixed ' , 'static ' ], true );
272+ return $ typeNode instanceof ThisTypeNode || $ typeNode instanceof IdentifierTypeNode;
277273 }
278274
279275 private function getNextSemicolonInSameScope (File $ phpcsFile , int $ scopePointer , int $ searchAt ): int
@@ -297,7 +293,7 @@ private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer,
297293 /**
298294 * @param IdentifierTypeNode|ThisTypeNode|UnionTypeNode|IntersectionTypeNode $typeNode
299295 */
300- private function createAssert (string $ variableName , TypeNode $ typeNode ): string
296+ private function createAssert (string $ variableName , TypeNode $ typeNode ): ? string
301297 {
302298 $ conditions = [];
303299
@@ -310,6 +306,10 @@ private function createAssert(string $variableName, TypeNode $typeNode): string
310306 }
311307 }
312308
309+ if ($ conditions === []) {
310+ return null ;
311+ }
312+
313313 $ operator = $ typeNode instanceof IntersectionTypeNode ? '&& ' : '|| ' ;
314314
315315 return sprintf ('\assert(%s); ' , implode (sprintf (' %s ' , $ operator ), array_unique ($ conditions )));
@@ -357,6 +357,10 @@ private function createConditions(string $variableName, TypeNode $typeNode): arr
357357 ];
358358 }
359359
360+ if (TypeHintHelper::isSimpleUnofficialTypeHints ($ typeNode ->name )) {
361+ return [];
362+ }
363+
360364 return [sprintf ('%s instanceof %s ' , $ variableName , $ typeNode ->name )];
361365 }
362366
0 commit comments