Control Structures
The controlFlow section decides on control structures: elseif as one word, an
else after a branch that always exits, an if that returns true and false, an
early return instead of an if at the end of a function, the comment on a case that falls through, and
the empty statement ;;.
Three decisions make the code flatter. elseAfterReturn and elseifAfterReturn remove an
else and an elseif when every branch before them exits through return, throw,
break or continue. trailingIf turns an if that wraps the end of a function or
a loop into an early return, when its body has at least as many statements as the parameter trailingIfMinStatements
says. The lines that move one level to the left are re-indented by the indentation section, so the example sets it too:
controlFlow:
elseif: 'elseif'
elseAfterReturn: forbidden
elseifAfterReturn: forbidden
ifReturningTrueFalse: forbidden
indentation:
unit: tab
function discount(int $count): float
{
if ($count > 10) {
return 0.1;
} else if ($count > 5) { // Useless else, because the branches before it always leave.
return 0.05;
} else { // Useless else, because the branches before it always leave.
return 0.0; // Expected the statement indented by 1 tab, 2 tabs found.
}
}
function isBig(int $count): bool
{
if ($count > 100) { // Useless `if`, because its condition itself is the result.
return true;
}
return false;
}
function discount(int $count): float
{
if ($count > 10) {
return 0.1;
}
if ($count > 5) {
return 0.05;
}
return 0.0;
}
function isBig(int $count): bool
{
return $count > 100;
}
controlFlow.trailingIf
An if ending a function or a loop body becomes a guard that leaves early, and an else that leaves
becomes the first branch.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep ·
covers SlevomatCodingStandard.ControlStructures.EarlyExit
controlFlow.trailingIfMinStatements
The statements the body of a trailing if has at least for it to become a guard.
- a number from 1
Default 2 · standards: perCs 2, psr12 2, nette 2,
symfony 2, laravel 2 · covers SlevomatCodingStandard.ControlStructures.EarlyExit
controlFlow.elseif
The keyword of a branch that follows if.
'elseif':elseifin one word, neverelse ifkeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs elseif, psr12 elseif, nette
elseif, symfony elseif, laravel elseif · covers
elseif, PSR2.ControlStructures.ElseIfDeclaration
controlFlow.switch.fallThroughComment
The text of the comment a non-empty case falling through to the next one carries, without the marker of the
comment, matched case-insensitively and reported only where none is found; a case that cannot fall through loses it.
- text
- a list of these words, where each one passes and code matching none of them is written like the first
keep: nothing is enforced, the code stays as it is
Default keep · standards: perCs no break, psr12 no break, nette
break omitted, symfony no break, laravel keep · covers
no_break_comment, PSR2.ControlStructures.SwitchDeclaration
controlFlow.emptyStatement
The second ; of foo();;.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs forbidden, psr12 forbidden,
nette forbidden, symfony forbidden, laravel forbidden · covers
no_empty_statement, SlevomatCodingStandard.PHP.UselessSemicolon
controlFlow.ifReturningTrueFalse
if ($a > 1) { return true; } return false; is return $a > 1;.
Not about branches returning other values, which controlFlow.ifElseReturning writes as a ternary.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs keep, psr12 keep, nette
forbidden, symfony keep, laravel keep · covers
simplified_if_return, SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn
controlFlow.ifElseReturning
if ($c) { return 1; } else { return 2; } is return $c ? 1 : 2;.
Not about the ternary of true and false it may write, which
expressions.ternary.ofTrueFalse writes as the condition.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep ·
covers SlevomatCodingStandard.ControlStructures.RequireTernaryOperator
controlFlow.elseAfterReturn
An else after branches that always leave, by return, throw, break,
continue, exit or goto, whose statements then follow the if, and an empty
else.
Not about an if and an else each returning a value, which controlFlow.ifElseReturning
writes as one return first.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs keep, psr12 keep, nette
keep, symfony forbidden, laravel keep · covers
no_useless_else, SlevomatCodingStandard.ControlStructures.EarlyExit
controlFlow.elseifAfterReturn
An elseif after an if that always leaves, which becomes an if of its own with the later
branches.
forbidden: never therekeep: nothing is enforced, the code stays as it is
Default keep · standards: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep · covers
no_superfluous_elseif, SlevomatCodingStandard.ControlStructures.EarlyExit