44
55namespace InfinityloopCodingStandard \Sniffs \ControlStructures ;
66
7- use PHP_CodeSniffer \Files \File ;
8- use PHP_CodeSniffer \Sniffs \Sniff ;
9- use SlevomatCodingStandard \Helpers \TokenHelper ;
10- use function array_merge ;
11- use function in_array ;
12- use function strlen ;
13- use function substr ;
14- use const T_COALESCE ;
15- use const T_OPEN_TAG ;
16- use const T_OPEN_TAG_WITH_ECHO ;
17- use const T_WHITESPACE ;
18-
19- class RequireMultiLineNullCoalesceSniff implements Sniff
20- {
7+ use \SlevomatCodingStandard \Helpers \TokenHelper ;
218
9+ class RequireMultiLineNullCoalesceSniff implements \PHP_CodeSniffer \Sniffs \Sniff
10+ {
2211 public const CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED = 'MultiLineNullCoalesceOperatorNotUsed ' ;
2312
2413 private const TAB_INDENT = "\t" ;
@@ -27,30 +16,30 @@ class RequireMultiLineNullCoalesceSniff implements Sniff
2716 /**
2817 * @return array<int, (int|string)>
2918 */
30- public function register (): array
19+ public function register () : array
3120 {
3221 return [
33- T_COALESCE ,
22+ \ T_COALESCE ,
3423 ];
3524 }
3625
37- /**
38- * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
39- * @param File $phpcsFile
40- * @param int $coalescePointer
41- */
42- public function process (File $ phpcsFile , $ coalescePointer ): void
26+ //@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
27+ public function process (\PHP_CodeSniffer \Files \File $ phpcsFile , $ coalescePointer ) : void
4328 {
4429 $ tokens = $ phpcsFile ->getTokens ();
4530
46- /** @var int $variablePointer */
47- $ variablePointer = TokenHelper:: findPrevious ( $ phpcsFile , T_VARIABLE , $ coalescePointer + 1 );
31+ $ variablePointer = TokenHelper:: findPrevious ( $ phpcsFile , \ T_VARIABLE , $ coalescePointer + 1 );
32+ \assert ( \is_int ( $ variablePointer ) );
4833
4934 if ($ tokens [$ coalescePointer ]['line ' ] !== $ tokens [$ variablePointer ]['line ' ]) {
5035 return ;
5136 }
5237
53- $ fix = $ phpcsFile ->addFixableError ('Null coalesce operator should be reformatted to next line. ' , $ coalescePointer , self ::CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED );
38+ $ fix = $ phpcsFile ->addFixableError (
39+ 'Null coalesce operator should be reformatted to next line. ' ,
40+ $ coalescePointer ,
41+ self ::CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED ,
42+ );
5443
5544 if (!$ fix ) {
5645 return ;
@@ -65,55 +54,67 @@ public function process(File $phpcsFile, $coalescePointer): void
6554 $ phpcsFile ->fixer ->endChangeset ();
6655 }
6756
68- private function getEndOfLineBefore (File $ phpcsFile , int $ pointer ): int
57+ private function getEndOfLineBefore (\ PHP_CodeSniffer \ Files \ File $ phpcsFile , int $ pointer ) : int
6958 {
7059 $ tokens = $ phpcsFile ->getTokens ();
7160
7261 $ endOfLineBefore = null ;
7362
7463 $ startPointer = $ pointer - 1 ;
64+
7565 while (true ) {
7666 $ possibleEndOfLinePointer = TokenHelper::findPrevious (
7767 $ phpcsFile ,
78- array_merge ([T_WHITESPACE , T_OPEN_TAG , T_OPEN_TAG_WITH_ECHO ], TokenHelper::$ inlineCommentTokenCodes ),
79- $ startPointer
68+ \ array_merge ([\ T_WHITESPACE , \ T_OPEN_TAG , \ T_OPEN_TAG_WITH_ECHO ], TokenHelper::$ inlineCommentTokenCodes ),
69+ $ startPointer,
8070 );
81- if ($ tokens [$ possibleEndOfLinePointer ]['code ' ] === T_WHITESPACE && $ tokens [$ possibleEndOfLinePointer ]['content ' ] === $ phpcsFile ->eolChar ) {
71+
72+ if (
73+ $ tokens [$ possibleEndOfLinePointer ]['code ' ] === \T_WHITESPACE
74+ && $ tokens [$ possibleEndOfLinePointer ]['content ' ] === $ phpcsFile ->eolChar
75+ ) {
8276 $ endOfLineBefore = $ possibleEndOfLinePointer ;
77+
8378 break ;
8479 }
8580
86- if ($ tokens [$ possibleEndOfLinePointer ]['code ' ] === T_OPEN_TAG || $ tokens [$ possibleEndOfLinePointer ]['code ' ] === T_OPEN_TAG_WITH_ECHO ) {
81+ if ($ tokens [$ possibleEndOfLinePointer ]['code ' ] === \ T_OPEN_TAG || $ tokens [$ possibleEndOfLinePointer ]['code ' ] === \ T_OPEN_TAG_WITH_ECHO ) {
8782 $ endOfLineBefore = $ possibleEndOfLinePointer ;
83+
8884 break ;
8985 }
9086
9187 if (
92- in_array ($ tokens [$ possibleEndOfLinePointer ]['code ' ], TokenHelper::$ inlineCommentTokenCodes , true )
93- && substr ($ tokens [$ possibleEndOfLinePointer ]['content ' ], -1 ) === $ phpcsFile ->eolChar
88+ \ in_array ($ tokens [$ possibleEndOfLinePointer ]['code ' ], TokenHelper::$ inlineCommentTokenCodes , true )
89+ && \ substr ($ tokens [$ possibleEndOfLinePointer ]['content ' ], -1 ) === $ phpcsFile ->eolChar
9490 ) {
9591 $ endOfLineBefore = $ possibleEndOfLinePointer ;
92+
9693 break ;
9794 }
9895
9996 $ startPointer = $ possibleEndOfLinePointer - 1 ;
10097 }
10198
102- /** @var int $endOfLineBefore */
10399 $ endOfLineBefore = $ endOfLineBefore ;
100+ \assert (\is_int ($ endOfLineBefore ));
101+
104102 return $ endOfLineBefore ;
105103 }
106104
107- private function getIndentation (File $ phpcsFile , int $ endOfLinePointer ): string
105+ private function getIndentation (\ PHP_CodeSniffer \ Files \ File $ phpcsFile , int $ endOfLinePointer ) : string
108106 {
109- $ pointerAfterWhitespace = TokenHelper::findNextExcluding ($ phpcsFile , T_WHITESPACE , $ endOfLinePointer + 1 );
107+ $ pointerAfterWhitespace = TokenHelper::findNextExcluding ($ phpcsFile , \ T_WHITESPACE , $ endOfLinePointer + 1 );
110108 $ actualIndentation = TokenHelper::getContent ($ phpcsFile , $ endOfLinePointer + 1 , $ pointerAfterWhitespace - 1 );
111109
112- if (strlen ($ actualIndentation ) !== 0 ) {
113- return $ actualIndentation . (substr ($ actualIndentation , -1 ) === self ::TAB_INDENT ? self ::TAB_INDENT : self ::SPACES_INDENT );
110+ if (\ strlen ($ actualIndentation ) !== 0 ) {
111+ return $ actualIndentation . (\ substr ($ actualIndentation , -1 ) === self ::TAB_INDENT ? self ::TAB_INDENT : self ::SPACES_INDENT );
114112 }
115113
116- $ tabPointer = TokenHelper::findPreviousContent ($ phpcsFile , T_WHITESPACE , self ::TAB_INDENT , $ endOfLinePointer - 1 );
117- return $ tabPointer !== null ? self ::TAB_INDENT : self ::SPACES_INDENT ;
114+ $ tabPointer = TokenHelper::findPreviousContent ($ phpcsFile , \T_WHITESPACE , self ::TAB_INDENT , $ endOfLinePointer - 1 );
115+
116+ return $ tabPointer !== null
117+ ? self ::TAB_INDENT
118+ : self ::SPACES_INDENT ;
118119 }
119120}
0 commit comments