33namespace SlevomatCodingStandard \Sniffs \ControlStructures ;
44
55use PHP_CodeSniffer \Files \File ;
6+ use PHP_CodeSniffer \Util \Tokens ;
67use SlevomatCodingStandard \Helpers \TokenHelper ;
78use function abs ;
89use function in_array ;
910use const T_BREAK ;
1011use const T_CONTINUE ;
11- use const T_EQUAL ;
1212use const T_GOTO ;
13+ use const T_OPEN_PARENTHESIS ;
1314use const T_RETURN ;
1415use const T_THROW ;
1516use const T_YIELD ;
@@ -28,11 +29,7 @@ class JumpStatementsSpacingSniff extends AbstractControlStructureSpacingSniff
2829 */
2930 public function process (File $ phpcsFile , $ controlStructurePointer ): void
3031 {
31- if ($ this ->isYieldWithAssigment ($ phpcsFile , $ controlStructurePointer )) {
32- return ;
33- }
34-
35- if ($ this ->isYieldFromWithReturn ($ phpcsFile , $ controlStructurePointer )) {
32+ if ($ this ->isOneOfYieldSpecialCases ($ phpcsFile , $ controlStructurePointer )) {
3633 return ;
3734 }
3835
@@ -75,30 +72,29 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
7572 parent ::checkLinesAfter ($ phpcsFile , $ controlStructurePointer );
7673 }
7774
78- private function isYieldWithAssigment (File $ phpcsFile , int $ controlStructurePointer ): bool
75+ private function isOneOfYieldSpecialCases (File $ phpcsFile , int $ controlStructurePointer ): bool
7976 {
8077 $ tokens = $ phpcsFile ->getTokens ();
8178
82- if ($ tokens [$ controlStructurePointer ]['code ' ] !== T_YIELD ) {
79+ $ controlStructure = $ tokens [$ controlStructurePointer ];
80+ if ($ controlStructure ['code ' ] !== T_YIELD && $ controlStructure ['code ' ] !== T_YIELD_FROM ) {
8381 return false ;
8482 }
8583
8684 $ pointerBefore = TokenHelper::findPreviousEffective ($ phpcsFile , $ controlStructurePointer - 1 );
8785
88- return $ tokens [$ pointerBefore ]['code ' ] === T_EQUAL ;
89- }
90-
91- private function isYieldFromWithReturn (File $ phpcsFile , int $ controlStructurePointer ): bool
92- {
93- $ tokens = $ phpcsFile ->getTokens ();
94-
95- if ($ tokens [$ controlStructurePointer ]['code ' ] !== T_YIELD_FROM ) {
96- return false ;
86+ // check if yield is used in assignment
87+ if (in_array ($ tokens [$ pointerBefore ]['code ' ], Tokens::$ assignmentTokens , true )) {
88+ return true ;
9789 }
9890
99- $ pointerBefore = TokenHelper::findPreviousEffective ($ phpcsFile , $ controlStructurePointer - 1 );
91+ // check if yield is used in a return statement
92+ if ($ tokens [$ pointerBefore ]['code ' ] === T_RETURN ) {
93+ return true ;
94+ }
10095
101- return $ tokens [$ pointerBefore ]['code ' ] === T_RETURN ;
96+ // check if yield is used inside parentheses (function call, while, ...)
97+ return $ tokens [$ pointerBefore ]['code ' ] === T_OPEN_PARENTHESIS ;
10298 }
10399
104100 private function isStackedSingleLineYield (File $ phpcsFile , int $ controlStructureStartPointer , bool $ previous ): bool
0 commit comments