Skip to content

Commit

Permalink
PHP 8.0 | Squiz/ControlStructureSpacing: check match expressions
Browse files Browse the repository at this point in the history
This adds support for checking the spacing for `match` expressions to the Squiz sniff.

Includes unit test.
  • Loading branch information
jrfnl committed Feb 23, 2021
1 parent 8b0137a commit a1bee01
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function register()
T_TRY,
T_CATCH,
T_FINALLY,
T_MATCH,
];

}//end register()
Expand Down Expand Up @@ -232,6 +233,16 @@ public function process(File $phpcsFile, $stackPtr)
}//end if
}//end if

if ($tokens[$stackPtr]['code'] === T_MATCH) {
// Move the scope closer to the semicolon/comma.
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($scopeCloser + 1), null, true);
if ($next !== false
&& ($tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['code'] === T_COMMA)
) {
$scopeCloser = $next;
}
}

$trailingContent = $phpcsFile->findNext(
T_WHITESPACE,
($scopeCloser + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,13 @@ echo 'hi';
?>
<?php endforeach; ?>
<?php endforeach; ?>

<?php

$expr = match( $foo ){

1 => 1,
2 => 2,

};
echo $expr;
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ echo 'hi';
?>
<?php endforeach; ?>
<?php endforeach; ?>

<?php

$expr = match($foo){
1 => 1,
2 => 2,
};

echo $expr;
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function getErrorList($testFile='ControlStructureSpacingUnitTest.inc')
242 => 1,
246 => 1,
248 => 1,
257 => 3,
261 => 1,
262 => 1,
];
break;
case 'ControlStructureSpacingUnitTest.js':
Expand Down

0 comments on commit a1bee01

Please sign in to comment.