Skip to content

Commit

Permalink
Merge pull request #169 from PHPCSStandards/universal/disallowlonelyi…
Browse files Browse the repository at this point in the history
…f-bow-out-early-for-close-tag

Universal/DisallowLonelyIf: bow out early for a specific situation (PHP close tag)
  • Loading branch information
jrfnl committed Dec 1, 2022
2 parents e5fa39e + 09df531 commit 3d4bb19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Universal/Sniffs/ControlStructures/DisallowLonelyIfSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,15 @@ public function process(File $phpcsFile, $stackPtr)
/*
* Find the end of an if - else chain.
*/

$innerIfPtr = $nextNonEmpty;
$innerIfToken = $tokens[$innerIfPtr];
$autoFixable = true;
$innerScopeCloser = $innerIfToken['scope_closer'];

/*
* For alternative syntax fixer only.
* Remember the individual inner scope opener and closers so the fixer doesn't need
* to do the same walking over the if/else chain again.
*/
// For alternative syntax fixer only.
// Remember the individual inner scope opener and closers so the fixer doesn't need
// to do the same walking over the if/else chain again.
$innerScopes = [
$innerIfToken['scope_opener'] => $innerScopeCloser,
];
Expand All @@ -116,6 +115,11 @@ public function process(File $phpcsFile, $stackPtr)
true
);

if ($tokens[$nextAfter]['code'] === \T_CLOSE_TAG) {
// Not "lonely" as at the very least there must be a PHP open tag before the outer closer.
return;
}

if ($tokens[$nextAfter]['code'] === \T_SEMICOLON) {
$innerScopeCloser = $nextAfter;
} else {
Expand Down
15 changes: 15 additions & 0 deletions Universal/Tests/ControlStructures/DisallowLonelyIfUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ if ($condition) {
doSomething();
}

/*
* Tests involving PHP close tags to close the inner control structure.
* Ignore as these will always need an open tag to reach the outer control structure again,
* so are never the "only" thing within the `else` (so not a lonely if).
*/
if ($condition):
doSomething();
else:
if ($anotherCondition):
doSomething();
endif ?>
Inline HTML
<?php
endif;

// Live coding.
// Intentional parse error. This test has to be the last in the file.
if ($a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ if ($condition) {
doSomething();
}

/*
* Tests involving PHP close tags to close the inner control structure.
* Ignore as these will always need an open tag to reach the outer control structure again,
* so are never the "only" thing within the `else` (so not a lonely if).
*/
if ($condition):
doSomething();
else:
if ($anotherCondition):
doSomething();
endif ?>
Inline HTML
<?php
endif;

// Live coding.
// Intentional parse error. This test has to be the last in the file.
if ($a) {
Expand Down

0 comments on commit 3d4bb19

Please sign in to comment.