@@ -574,7 +574,7 @@ protected function checkUseForReturnTypeHint(File $phpcsFile, int $stackPtr): vo
574574 $ extractedUseStatement = '' ;
575575 $ lastSeparatorIndex = null ;
576576 while (true ) {
577- if (!$ this ->isGivenKind ([T_NS_SEPARATOR , T_STRING , T_RETURN_TYPE ], $ tokens [$ j ])) {
577+ if (!$ this ->isGivenKind ([T_NS_SEPARATOR , T_STRING ], $ tokens [$ j ])) {
578578 break ;
579579 }
580580
@@ -639,32 +639,45 @@ protected function checkPropertyForInstanceOf(File $phpcsFile, int $stackPtr): v
639639 return ;
640640 }
641641
642- $ lastIndex = null ;
643- $ j = $ startIndex ;
644- $ extractedUseStatement = '' ;
645- $ lastSeparatorIndex = null ;
646- while (true ) {
647- if (!$ this ->isGivenKind ([T_NS_SEPARATOR , T_STRING , T_RETURN_TYPE ], $ tokens [$ j ])) {
648- break ;
642+ // Handle T_NAME_FULLY_QUALIFIED token (PHP CodeSniffer v4)
643+ $ className = '' ;
644+ if ($ tokens [$ startIndex ]['code ' ] === T_NAME_FULLY_QUALIFIED ) {
645+ $ extractedUseStatement = ltrim ($ tokens [$ startIndex ]['content ' ], '\\' );
646+ if (strpos ($ extractedUseStatement , '\\' ) === false ) {
647+ return ; // Not a namespaced class
649648 }
649+ $ lastSeparatorPos = strrpos ($ extractedUseStatement , '\\' );
650+ $ className = substr ($ extractedUseStatement , $ lastSeparatorPos + 1 );
651+ $ lastIndex = $ startIndex ;
652+ $ lastSeparatorIndex = null ; // No separate separator token in v4
653+ } else {
654+ // Handle separate tokens (T_NS_SEPARATOR and T_STRING)
655+ $ lastIndex = null ;
656+ $ j = $ startIndex ;
657+ $ extractedUseStatement = '' ;
658+ $ lastSeparatorIndex = null ;
659+ while (true ) {
660+ if (!$ this ->isGivenKind ([T_NS_SEPARATOR , T_STRING ], $ tokens [$ j ])) {
661+ break ;
662+ }
650663
651- $ lastIndex = $ j ;
652- $ extractedUseStatement .= $ tokens [$ j ]['content ' ];
653- if ($ this ->isGivenKind ([T_NS_SEPARATOR ], $ tokens [$ j ])) {
654- $ lastSeparatorIndex = $ j ;
664+ $ lastIndex = $ j ;
665+ $ extractedUseStatement .= $ tokens [$ j ]['content ' ];
666+ if ($ this ->isGivenKind ([T_NS_SEPARATOR ], $ tokens [$ j ])) {
667+ $ lastSeparatorIndex = $ j ;
668+ }
669+ ++$ j ;
655670 }
656- ++$ j ;
657- }
658671
659- if ($ lastIndex === null || $ lastSeparatorIndex === null ) {
660- return ;
661- }
662-
663- $ extractedUseStatement = ltrim ($ extractedUseStatement , '\\' );
672+ if ($ lastIndex === null || $ lastSeparatorIndex === null ) {
673+ return ;
674+ }
664675
665- $ className = '' ;
666- for ($ k = $ lastSeparatorIndex + 1 ; $ k <= $ lastIndex ; ++$ k ) {
667- $ className .= $ tokens [$ k ]['content ' ];
676+ $ extractedUseStatement = ltrim ($ extractedUseStatement , '\\' );
677+ $ className = '' ;
678+ for ($ k = $ lastSeparatorIndex + 1 ; $ k <= $ lastIndex ; ++$ k ) {
679+ $ className .= $ tokens [$ k ]['content ' ];
680+ }
668681 }
669682
670683 $ error = 'Use statement ' . $ extractedUseStatement . ' for class ' . $ className . ' should be in use block. ' ;
@@ -677,13 +690,23 @@ protected function checkPropertyForInstanceOf(File $phpcsFile, int $stackPtr): v
677690
678691 $ addedUseStatement = $ this ->addUseStatement ($ phpcsFile , $ className , $ extractedUseStatement );
679692
680- for ($ k = $ lastSeparatorIndex ; $ k > $ startIndex ; --$ k ) {
681- $ phpcsFile ->fixer ->replaceToken ($ k , '' );
682- }
683- $ phpcsFile ->fixer ->replaceToken ($ startIndex , '' );
693+ if ($ lastSeparatorIndex !== null ) {
694+ // Legacy: remove individual tokens
695+ for ($ k = $ lastSeparatorIndex ; $ k > $ startIndex ; --$ k ) {
696+ $ phpcsFile ->fixer ->replaceToken ($ k , '' );
697+ }
698+ $ phpcsFile ->fixer ->replaceToken ($ startIndex , '' );
684699
685- if ($ addedUseStatement ['alias ' ] !== null ) {
686- $ phpcsFile ->fixer ->replaceToken ($ lastIndex , $ addedUseStatement ['alias ' ]);
700+ if ($ addedUseStatement ['alias ' ] !== null ) {
701+ $ phpcsFile ->fixer ->replaceToken ($ lastIndex , $ addedUseStatement ['alias ' ]);
702+ }
703+ } else {
704+ // PHP CodeSniffer v4: replace single T_NAME_FULLY_QUALIFIED token
705+ if ($ addedUseStatement ['alias ' ] !== null ) {
706+ $ phpcsFile ->fixer ->replaceToken ($ startIndex , $ addedUseStatement ['alias ' ]);
707+ } else {
708+ $ phpcsFile ->fixer ->replaceToken ($ startIndex , $ className );
709+ }
687710 }
688711
689712 $ phpcsFile ->fixer ->endChangeset ();
0 commit comments