Skip to content

Commit 6d61496

Browse files
committed
Squiz/SwitchDeclaration: make implied semicolon via close tag auto-fixable
The `Squiz.ControlStructures.SwitchDeclaration` sniff does _not_ include a rule/error for the scope opener not being a `:` colon, though all the error messages have the presumption of the scope opener being a colon in the textual content. The sniff does have tests with a semicolon scope opener and as there is no error related to the semicolon being a scope opener, I can only presume that it was previously decided to allow that in the Squiz standard. Considering that this is a code style sniff, it is not for this sniff to enforce PHP cross-version compatibility of code, so the PHP 8.5 deprecation is irrelevant and there is no reason to change the behaviour of the sniff for semicolon scope openers. However, the sniff definitely did not function correctly for code moving in and out of PHP within a `switch` statement. PR 1315 was a first step towards supporting this. This commit now adds a new error to disallow a PHP close tag as the "scope_opener" for `case`/`default` statements. The error is auto-fixable and will insert a colon before the PHP close tag as the fix. With this additional error in place, the sniff's handling of code within a `switch` moving in and out of PHP has greatly improved. Includes tests.
1 parent 21c0e80 commit 6d61496

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ public function process(File $phpcsFile, int $stackPtr)
141141
}
142142

143143
$opener = $tokens[$nextCase]['scope_opener'];
144+
145+
if ($tokens[$opener]['code'] === T_CLOSE_TAG) {
146+
$error = 'There should be a colon before the PHP close tag to end the %s statement';
147+
$code = 'WrongOpener' . $type;
148+
$data = [strtoupper($type)];
149+
150+
$fix = $phpcsFile->addFixableError($error, $nextCase, $code, $data);
151+
if ($fix === true) {
152+
$prevNonEmpty = $phpcsFile->findPrevious(T_WHITESPACE, ($opener - 1), null, true);
153+
$phpcsFile->fixer->addContent($prevNonEmpty, ':');
154+
}
155+
}
156+
144157
if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') {
145158
$error = 'There must be no space before the colon in a ' . strtoupper($type) . ' statement';
146159
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon' . $type);

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,19 @@ switch ($condition) {
380380
break;
381381

382382
}
383+
384+
// Verify that case/default statements with a PHP close tag as scope opener are handled correctly by the sniff.
385+
switch ($foo):
386+
case "foo"?>
387+
<div>Some html</div>
388+
<?php break; ?>
389+
<?php case "bar" ; ?>
390+
<div>Some html</div>
391+
<?php return; ?>
392+
<?php default ?>
393+
<div>Some html</div>
394+
<?php goto FOO;
395+
default ; ?>
396+
<div>Some html</div>
397+
<?php throw new Exception;
398+
endswitch;

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,33 @@ switch ($condition) {
404404
break;
405405

406406
}
407+
408+
// Verify that case/default statements with a PHP close tag as scope opener are handled correctly by the sniff.
409+
switch ($foo):
410+
case "foo":
411+
?>
412+
<div>Some html</div>
413+
<?php
414+
break;
415+
416+
?>
417+
<?php
418+
case "bar";
419+
?>
420+
<div>Some html</div>
421+
<?php
422+
return;
423+
424+
?>
425+
<?php
426+
default:
427+
?>
428+
<div>Some html</div>
429+
<?php
430+
goto FOO;
431+
default;
432+
?>
433+
<div>Some html</div>
434+
<?php
435+
throw new Exception;
436+
endswitch;

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public function getErrorList()
8888
375 => 2,
8989
377 => 1,
9090
380 => 1,
91+
386 => 1,
92+
388 => 2,
93+
389 => 3,
94+
391 => 2,
95+
392 => 3,
96+
394 => 1,
97+
395 => 2,
98+
397 => 1,
9199
];
92100
}
93101

0 commit comments

Comments
 (0)