Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universal/DisallowLonelyIf: bow out early for a specific situation (PHP close tag) #169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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