diff --git a/PHPCSUtils/Tokens/Collections.php b/PHPCSUtils/Tokens/Collections.php index f680d2d3..2045a6a2 100644 --- a/PHPCSUtils/Tokens/Collections.php +++ b/PHPCSUtils/Tokens/Collections.php @@ -202,6 +202,7 @@ class Collections * DEPRECATED: Control structure tokens. * * @since 1.0.0-alpha2 + * @since 1.0.0-alpha4 Added the T_MATCH token for PHP 8.0 match expressions. * * @deprecated 1.0.0-alpha4 Use the {@see Collections::controlStructureTokens()} method instead. * @@ -217,6 +218,7 @@ class Collections \T_DO => \T_DO, \T_WHILE => \T_WHILE, \T_DECLARE => \T_DECLARE, + \T_MATCH => \T_MATCH, ]; /** diff --git a/PHPCSUtils/Utils/ControlStructures.php b/PHPCSUtils/Utils/ControlStructures.php index f351ed83..802c6214 100644 --- a/PHPCSUtils/Utils/ControlStructures.php +++ b/PHPCSUtils/Utils/ControlStructures.php @@ -37,6 +37,7 @@ class ControlStructures * regarded as empty. * * @since 1.0.0 + * @since 1.0.0-alpha4 Added support for PHP 8.0 match control structures. * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param int $stackPtr The position of the token we are checking. diff --git a/Tests/Utils/ControlStructures/HasBodyTest.inc b/Tests/Utils/ControlStructures/HasBodyTest.inc index e0363bb0..d68d3b7d 100644 --- a/Tests/Utils/ControlStructures/HasBodyTest.inc +++ b/Tests/Utils/ControlStructures/HasBodyTest.inc @@ -217,6 +217,28 @@ do echo $i; while (++$i <= 10); + +/* + * PHP 8.0 match expressions. + */ + +/* testMatchEmptyBody */ +// Intentional fatal error, "unhandled match case", but not the concern of this method. +$match = match($a) {}; + +/* testMatchEmptyBodyWithComment */ +// Intentional fatal error, "unhandled match case", but not the concern of this method. +$match = match($a) { + // Deliberately empty. +}; + +/* testMatchWithCode */ +$match = match ($a) { + 0 => 'Foo', + 1 => 'Bar', + 2 => 'Baz', +}; + // Live coding. // Intentional parse error. This test has to be the last in the file. if ($a) { diff --git a/Tests/Utils/ControlStructures/HasBodyTest.php b/Tests/Utils/ControlStructures/HasBodyTest.php index 89cdaeb8..4c8989c3 100644 --- a/Tests/Utils/ControlStructures/HasBodyTest.php +++ b/Tests/Utils/ControlStructures/HasBodyTest.php @@ -311,6 +311,29 @@ public function dataHasBody() 'hasBody' => true, 'hasNonEmptyBody' => true, ], + + /* + * Match without body cannot be tested as, in that case, `match` will tokenize as `T_STRING`. + * Without body (`match();`), match will either yield a parse error + * or be interpreted as a function call (`\match();` or `self::match()` etc). + */ + + 'match-empty-body' => [ + 'testMarker' => '/* testMatchEmptyBody */', + 'hasBody' => true, + 'hasNonEmptyBody' => false, + ], + 'match-empty-body-comment-only' => [ + 'testMarker' => '/* testMatchEmptyBodyWithComment */', + 'hasBody' => true, + 'hasNonEmptyBody' => false, + ], + 'match-with-code' => [ + 'testMarker' => '/* testMatchWithCode */', + 'hasBody' => true, + 'hasNonEmptyBody' => true, + ], + 'else-live-coding' => [ 'testMarker' => '/* testElseLiveCoding */', 'hasBody' => true,