22
33namespace Flyeralarm \CodingGuidelines \Flyeralarm \Sniffs \ControlStructures ;
44
5- use PHP_CodeSniffer \Sniffs \Sniff ;
65use PHP_CodeSniffer \Files \File ;
6+ use PHP_CodeSniffer \Sniffs \Sniff ;
77
88class YodaSniff implements Sniff
99{
@@ -15,6 +15,7 @@ public function register()
1515 return [T_IF , T_ELSEIF , T_WHILE ];
1616 }
1717
18+
1819 /**
1920 * @param File $phpcsFile
2021 * @param int $stackPtr
@@ -25,7 +26,7 @@ public function process(File $phpcsFile, $stackPtr)
2526 $ tokens = $ phpcsFile ->getTokens ();
2627
2728 $ startOfConditionPtr = $ stackPtr ;
28- $ endPtr = $ tokens [$ startOfConditionPtr ][ ' scope_opener ' ] + 1 ;
29+ $ endPtr = $ this -> getConditionStartPointerDoWhileStable ( $ tokens [$ startOfConditionPtr ]) ;
2930
3031 $ logicalOperatorTokenIds = [T_BOOLEAN_AND , T_BOOLEAN_OR , T_LOGICAL_AND , T_LOGICAL_OR , T_LOGICAL_XOR ];
3132 $ scopeOpenerTokenIds = [T_OPEN_CURLY_BRACKET ];
@@ -37,6 +38,7 @@ public function process(File $phpcsFile, $stackPtr)
3738 }
3839 }
3940
41+
4042 /**
4143 * @param File $phpcsFile
4244 * @param $startOfConditionPtr
@@ -115,4 +117,14 @@ private function checkConditionalOrderForArithmeticExpression(
115117 'ConditionalOrder '
116118 );
117119 }
120+
121+
122+ private function getConditionStartPointerDoWhileStable (array $ array ): ?int
123+ {
124+ if (array_key_exists ('scope_opener ' , $ array )) {
125+ return $ array ['scope_opener ' ] + 1 ;
126+ }
127+
128+ return null ;
129+ }
118130}
0 commit comments