@@ -647,7 +647,7 @@ public static function getContainingArrowFunctionIndex(File $phpcsFile, $stackPt
647647
648648 // We found the closest arrow function before this token. If the token is
649649 // within the scope of that arrow function, then return it.
650- if ($ stackPtr > $ arrowFunctionInfo ['scope_opener ' ] && $ stackPtr < $ arrowFunctionInfo ['scope_closer ' ]) {
650+ if ($ stackPtr >= $ arrowFunctionInfo ['scope_opener ' ] && $ stackPtr <= $ arrowFunctionInfo ['scope_closer ' ]) {
651651 return $ arrowFunctionIndex ;
652652 }
653653
@@ -697,28 +697,7 @@ private static function getPreviousArrowFunctionIndex(File $phpcsFile, $stackPtr
697697 public static function isArrowFunction (File $ phpcsFile , $ stackPtr )
698698 {
699699 $ tokens = $ phpcsFile ->getTokens ();
700- if (defined ('T_FN ' ) && $ tokens [$ stackPtr ]['code ' ] === T_FN ) {
701- return true ;
702- }
703- if ($ tokens [$ stackPtr ]['content ' ] !== 'fn ' ) {
704- return false ;
705- }
706- // Make sure next non-space token is an open parenthesis
707- $ openParenIndex = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ stackPtr + 1 , null , true );
708- if (! is_int ($ openParenIndex ) || $ tokens [$ openParenIndex ]['code ' ] !== T_OPEN_PARENTHESIS ) {
709- return false ;
710- }
711- // Find the associated close parenthesis
712- $ closeParenIndex = $ tokens [$ openParenIndex ]['parenthesis_closer ' ];
713- // Make sure the next token is a fat arrow
714- $ fatArrowIndex = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ closeParenIndex + 1 , null , true );
715- if (! is_int ($ fatArrowIndex )) {
716- return false ;
717- }
718- if ($ tokens [$ fatArrowIndex ]['code ' ] !== T_DOUBLE_ARROW && $ tokens [$ fatArrowIndex ]['type ' ] !== 'T_FN_ARROW ' ) {
719- return false ;
720- }
721- return true ;
700+ return $ tokens [$ stackPtr ]['code ' ] === T_FN ;
722701 }
723702
724703 /**
@@ -739,108 +718,18 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr)
739718 public static function getArrowFunctionOpenClose (File $ phpcsFile , $ stackPtr )
740719 {
741720 $ tokens = $ phpcsFile ->getTokens ();
742- if ($ tokens [$ stackPtr ]['content ' ] !== 'fn ' ) {
743- return null ;
744- }
745- // Make sure next non-space token is an open parenthesis
746- $ openParenIndex = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ stackPtr + 1 , null , true );
747- if (! is_int ($ openParenIndex ) || $ tokens [$ openParenIndex ]['code ' ] !== T_OPEN_PARENTHESIS ) {
748- return null ;
749- }
750- // Find the associated close parenthesis
751- $ closeParenIndex = $ tokens [$ openParenIndex ]['parenthesis_closer ' ];
752- // Make sure the next token is a fat arrow or a return type
753- $ fatArrowIndex = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ closeParenIndex + 1 , null , true );
754- if (! is_int ($ fatArrowIndex )) {
755- return null ;
756- }
757- if (
758- $ tokens [$ fatArrowIndex ]['code ' ] !== T_DOUBLE_ARROW &&
759- $ tokens [$ fatArrowIndex ]['type ' ] !== 'T_FN_ARROW ' &&
760- $ tokens [$ fatArrowIndex ]['code ' ] !== T_COLON
761- ) {
762- return null ;
763- }
764-
765- // Find the scope closer
766- $ scopeCloserIndex = null ;
767- $ foundCurlyPairs = 0 ;
768- $ foundArrayPairs = 0 ;
769- $ foundParenPairs = 0 ;
770- $ arrowBodyStart = $ tokens [$ stackPtr ]['parenthesis_closer ' ] + 1 ;
771- $ lastToken = self ::getLastNonEmptyTokenIndexInFile ($ phpcsFile );
772- for ($ index = $ arrowBodyStart ; $ index < $ lastToken ; $ index ++) {
773- $ token = $ tokens [$ index ];
774- if (empty ($ token ['code ' ])) {
775- $ scopeCloserIndex = $ index ;
776- break ;
777- }
778-
779- $ code = $ token ['code ' ];
780-
781- // A semicolon is always a closer.
782- if ($ code === T_SEMICOLON ) {
783- $ scopeCloserIndex = $ index ;
784- break ;
785- }
786-
787- // Track pair opening tokens.
788- if ($ code === T_OPEN_CURLY_BRACKET ) {
789- $ foundCurlyPairs += 1 ;
790- continue ;
791- }
792- if ($ code === T_OPEN_SHORT_ARRAY || $ code === T_OPEN_SQUARE_BRACKET ) {
793- $ foundArrayPairs += 1 ;
794- continue ;
795- }
796- if ($ code === T_OPEN_PARENTHESIS ) {
797- $ foundParenPairs += 1 ;
798- continue ;
799- }
800721
801- // A pair closing is only an arrow func closer if there was no matching opening token.
802- if ($ code === T_CLOSE_CURLY_BRACKET ) {
803- if ($ foundCurlyPairs === 0 ) {
804- $ scopeCloserIndex = $ index ;
805- break ;
806- }
807- $ foundCurlyPairs -= 1 ;
808- continue ;
809- }
810- if ($ code === T_CLOSE_SHORT_ARRAY || $ code === T_CLOSE_SQUARE_BRACKET ) {
811- if ($ foundArrayPairs === 0 ) {
812- $ scopeCloserIndex = $ index ;
813- break ;
814- }
815- $ foundArrayPairs -= 1 ;
816- continue ;
817- }
818- if ($ code === T_CLOSE_PARENTHESIS ) {
819- if ($ foundParenPairs === 0 ) {
820- $ scopeCloserIndex = $ index ;
821- break ;
822- }
823- $ foundParenPairs -= 1 ;
824- continue ;
825- }
826-
827- // A comma is a closer only if we are not inside an opening token.
828- if ($ code === T_COMMA ) {
829- if (empty ($ foundArrayPairs ) && empty ($ foundParenPairs ) && empty ($ foundCurlyPairs )) {
830- $ scopeCloserIndex = $ index ;
831- break ;
832- }
833- continue ;
834- }
722+ if ($ tokens [$ stackPtr ]['code ' ] !== T_FN ) {
723+ return null ;
835724 }
836725
837- if (! is_int ( $ scopeCloserIndex )) {
726+ if (!isset ( $ tokens [ $ stackPtr ][ ' scope_closer ' ] )) {
838727 return null ;
839728 }
840729
841730 return [
842- 'scope_opener ' => $ stackPtr ,
843- 'scope_closer ' => $ scopeCloserIndex ,
731+ 'scope_opener ' => $ tokens [ $ stackPtr][ ' scope_opener ' ] ,
732+ 'scope_closer ' => $ tokens [ $ stackPtr ][ ' scope_closer ' ] ,
844733 ];
845734 }
846735
0 commit comments