Skip to content

Commit 549899b

Browse files
jrfnlgsherwood
authored andcommitted
PHP 8.0 | Squiz/ControlStructureSpacing: check match expressions
This adds support for checking the spacing for `match` expressions to the Squiz sniff. Includes unit test.
1 parent d1a7027 commit 549899b

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function register()
3636
T_TRY,
3737
T_CATCH,
3838
T_FINALLY,
39+
T_MATCH,
3940
];
4041

4142
}//end register()
@@ -222,6 +223,16 @@ public function process(File $phpcsFile, $stackPtr)
222223
}//end if
223224
}//end if
224225

226+
if ($tokens[$stackPtr]['code'] === T_MATCH) {
227+
// Move the scope closer to the semicolon/comma.
228+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($scopeCloser + 1), null, true);
229+
if ($next !== false
230+
&& ($tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['code'] === T_COMMA)
231+
) {
232+
$scopeCloser = $next;
233+
}
234+
}
235+
225236
$trailingContent = $phpcsFile->findNext(
226237
T_WHITESPACE,
227238
($scopeCloser + 1),

src/Standards/Squiz/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,13 @@ echo 'hi';
251251
?>
252252
<?php endforeach; ?>
253253
<?php endforeach; ?>
254+
255+
<?php
256+
257+
$expr = match( $foo ){
258+
259+
1 => 1,
260+
2 => 2,
261+
262+
};
263+
echo $expr;

src/Standards/Squiz/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,12 @@ echo 'hi';
244244
?>
245245
<?php endforeach; ?>
246246
<?php endforeach; ?>
247+
248+
<?php
249+
250+
$expr = match($foo){
251+
1 => 1,
252+
2 => 2,
253+
};
254+
255+
echo $expr;

src/Standards/Squiz/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function getErrorList()
5353
242 => 1,
5454
246 => 1,
5555
248 => 1,
56+
257 => 3,
57+
261 => 1,
58+
262 => 1,
5659
];
5760

5861
}//end getErrorList()

0 commit comments

Comments
 (0)