Řídicí struktury
Po větvi, která skončila příkazem return, je else navíc a odsazuje kód
o úroveň hlouběji, než je třeba. Sekce controlFlow takové řídicí struktury zplošťuje a rozhoduje i o
elseif jedním slovem, o if, který jen vrací true a false, o komentáři
u propadnutí case a o prázdném příkazu ;;.
Zploštění mají na starost tři rozhodnutí. elseAfterReturn a elseifAfterReturn odstraní
else a elseif, před kterými každá větev skončí příkazem return,
throw, break, continue, exit nebo goto; trailingIf
převede if, který obaluje konec funkce nebo cyklu, na časný návrat, pokud má jeho tělo aspoň tolik
příkazů, kolik říká parametr trailingIfMinStatements. Odsazení řádků, které se tím posunou o úroveň
doleva, srovná sekce indentation, proto ji ukázka
nastavuje taky:
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.elseif
The keyword of a branch that follows if.
'elseif':elseifin one word, neverelse ifkeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs elseif, psr12 elseif, nette
elseif, symfony elseif, laravel elseif · pokrývá
elseif, PSR2.ControlStructures.ElseIfDeclaration
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: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
keep, symfony forbidden, laravel keep · pokrývá
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: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep · pokrývá
no_superfluous_elseif, SlevomatCodingStandard.ControlStructures.EarlyExit
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: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep ·
pokrývá SlevomatCodingStandard.ControlStructures.EarlyExit
controlFlow.trailingIfMinStatements
The statements the body of a trailing if has at least for it to become a guard.
- počet od 1
Výchozí 2 · standardy: perCs 2, psr12 2, nette 2,
symfony 2, laravel 2 · pokrývá SlevomatCodingStandard.ControlStructures.EarlyExit
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: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
forbidden, symfony keep, laravel keep · pokrývá
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: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
keep, symfony keep, laravel keep ·
pokrývá SlevomatCodingStandard.ControlStructures.RequireTernaryOperator
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
- seznam těchto slov, kde projde každé a kód, který neodpovídá žádnému, se napíše podle prvního
keep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs no break, psr12 no break,
nette break omitted, symfony no break, laravel keep · pokrývá
no_break_comment, PSR2.ControlStructures.SwitchDeclaration
controlFlow.emptyStatement
The second ; of foo();;.
forbidden: never therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs forbidden, psr12 forbidden,
nette forbidden, symfony forbidden, laravel forbidden · pokrývá
no_empty_statement, SlevomatCodingStandard.PHP.UselessSemicolon