1313use SlevomatCodingStandard \Helpers \SuppressHelper ;
1414use SlevomatCodingStandard \Helpers \TokenHelper ;
1515use function array_keys ;
16+ use function array_merge ;
1617use function array_reverse ;
1718use function count ;
1819use function in_array ;
@@ -455,7 +456,7 @@ private function getProperties(File $phpcsFile, int $classTokenPointer): array
455456 break ;
456457 }
457458
458- $ visibilityModifierTokenPointer = $ this ->findVisibilityModifierTokenPointer ($ phpcsFile, $ tokens , $ propertyTokenPointer );
459+ $ visibilityModifierTokenPointer = $ this ->findVisibilityPointer ($ phpcsFile , $ propertyTokenPointer );
459460 if ($ visibilityModifierTokenPointer === null || $ tokens [$ visibilityModifierTokenPointer ]['code ' ] !== T_PRIVATE ) {
460461 $ findPropertiesStartTokenPointer = $ propertyTokenPointer + 1 ;
461462 continue ;
@@ -526,7 +527,7 @@ private function getMethods(File $phpcsFile, int $classTokenPointer): array
526527 break ;
527528 }
528529
529- $ visibilityModifierTokenPointer = $ this ->findVisibilityModifierTokenPointer ($ phpcsFile, $ tokens , $ methodTokenPointer );
530+ $ visibilityModifierTokenPointer = $ this ->findVisibilityPointer ($ phpcsFile , $ methodTokenPointer );
530531 if ($ visibilityModifierTokenPointer === null || $ tokens [$ visibilityModifierTokenPointer ]['code ' ] !== T_PRIVATE ) {
531532 $ findMethodsStartTokenPointer = $ methodTokenPointer + 1 ;
532533 continue ;
@@ -592,7 +593,7 @@ private function getConstants(File $phpcsFile, int $classTokenPointer): array
592593 break ;
593594 }
594595
595- $ visibilityModifierTokenPointer = $ this ->findVisibilityModifierTokenPointer ($ phpcsFile, $ tokens , $ constantTokenPointer );
596+ $ visibilityModifierTokenPointer = $ this ->findVisibilityPointer ($ phpcsFile , $ constantTokenPointer );
596597 if ($ visibilityModifierTokenPointer === null || $ tokens [$ visibilityModifierTokenPointer ]['code ' ] !== T_PRIVATE ) {
597598 $ findConstantsStartTokenPointer = $ constantTokenPointer + 1 ;
598599 continue ;
@@ -608,33 +609,34 @@ private function getConstants(File $phpcsFile, int $classTokenPointer): array
608609 return $ reportedConstants ;
609610 }
610611
611- /**
612- * @param File $phpcsFile
613- * @param array<int, array<string, array<int, int|string>|int|string>> $tokens
614- * @param int $methodTokenPointer
615- * @return int|null
616- */
617- private function findVisibilityModifierTokenPointer (File $ phpcsFile , array $ tokens , int $ methodTokenPointer ): ?int
612+ private function findVisibilityPointer (File $ phpcsFile , int $ pointer ): ?int
618613 {
619- /** @var int $visibilityModifiedTokenPointer */
620- $ visibilityModifiedTokenPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ methodTokenPointer - 1 );
621- $ visibilityModifiedToken = $ tokens [$ visibilityModifiedTokenPointer ];
622- if (in_array ($ visibilityModifiedToken ['code ' ], [T_PUBLIC , T_PROTECTED , T_PRIVATE ], true )) {
623- return $ visibilityModifiedTokenPointer ;
624- }
614+ $ tokens = $ phpcsFile ->getTokens ();
615+
616+ $ visibilityPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ pointer - 1 );
625617
626- if (in_array ($ visibilityModifiedToken ['code ' ], [T_SELF , T_STRING ], true )) {
627- $ mightBeNullableTokenPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ visibilityModifiedTokenPointer - 1 );
628- $ mightBeNullableToken = $ tokens [$ mightBeNullableTokenPointer ];
629- if ($ mightBeNullableToken ['code ' ] === T_NULLABLE ) {
630- $ visibilityModifiedTokenPointer = $ mightBeNullableTokenPointer ;
618+ if (in_array ($ tokens [$ visibilityPointer ]['code ' ], [T_SELF , T_STRING ], true )) {
619+ $ visibilityPointer = TokenHelper::findPreviousExcluding (
620+ $ phpcsFile ,
621+ array_merge (TokenHelper::$ nameTokenCodes , TokenHelper::$ ineffectiveTokenCodes ),
622+ $ visibilityPointer - 1
623+ );
624+
625+ if ($ tokens [$ visibilityPointer ]['code ' ] === T_NULLABLE ) {
626+ $ visibilityPointer = TokenHelper::findPreviousEffective ($ phpcsFile , $ visibilityPointer - 1 );
631627 }
628+ }
632629
633- return $ this ->findVisibilityModifierTokenPointer ($ phpcsFile , $ tokens , $ visibilityModifiedTokenPointer );
630+ if (in_array ($ tokens [$ visibilityPointer ]['code ' ], [T_ABSTRACT , T_STATIC ], true )) {
631+ $ visibilityPointer = TokenHelper::findPreviousExcluding (
632+ $ phpcsFile ,
633+ array_merge ([T_ABSTRACT , T_STATIC ], TokenHelper::$ ineffectiveTokenCodes ),
634+ $ visibilityPointer - 1
635+ );
634636 }
635637
636- if (in_array ($ visibilityModifiedToken [ 'code ' ], [T_ABSTRACT , T_STATIC ], true )) {
637- return $ this -> findVisibilityModifierTokenPointer ( $ phpcsFile , $ tokens , $ visibilityModifiedTokenPointer ) ;
638+ if (in_array ($ tokens [ $ visibilityPointer ][ 'code ' ], [T_PUBLIC , T_PROTECTED , T_PRIVATE ], true )) {
639+ return $ visibilityPointer ;
638640 }
639641
640642 return null ;
0 commit comments