control-structures.md
April 11, 2025 ยท View on GitHub
Control structures
SlevomatCodingStandard.ControlStructures.AssignmentInCondition
Disallows assignments in if, elseif and do-while loop conditions:
if ($file = findFile($path)) {
}
Assignment in while loop condition is specifically allowed because it's commonly used.
This is a great addition to already existing SlevomatCodingStandard.ControlStructures.DisallowYodaComparison because it prevents the danger of assigning something by mistake instead of using a comparison operator like ===.
Sniff provides the following settings:
ignoreAssignmentsInsideFunctionCalls: ignores assignment inside function calls, like this:
if (in_array(1, $haystack, $strict = true)) {
}
SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing ๐ง
Enforces configurable number of lines around block control structures (if, foreach, ...).
Sniff provides the following settings:
linesCountBefore: allows to configure the number of lines before control structure.linesCountBeforeFirst: allows to configure the number of lines before first control structure.linesCountAfter: allows to configure the number of lines after control structure.linesCountAfterLast: allows to configure the number of lines after last control structure.controlStructures: allows to narrow the list of checked control structures.
For example, with the following setting, only if and switch keywords are checked.
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing">
<properties>
<property name="controlStructures" type="array">
<element value="if"/>
<element value="switch"/>
</property>
</properties>
</rule>
SlevomatCodingStandard.ControlStructures.EarlyExit ๐ง
Requires use of early exit.
Sniff provides the following settings:
ignoreStandaloneIfInScope: ignoresifthat is standalone in scope, like this:
foreach ($values as $value) {
if ($value) {
doSomething();
}
}
ignoreOneLineTrailingIf: ignoresifthat has one line content and is on the last position in scope, like this:
foreach ($values as $value) {
$value .= 'whatever';
if ($value) {
doSomething();
}
}
ignoreTrailingIfWithOneInstruction: ignoresifthat has only one instruction and is on the last position in scope, like this:
foreach ($values as $value) {
$value .= 'whatever';
if ($value) {
doSomething(function () {
// Anything
});
}
}
SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch ๐ง
Disallows use of continue without integer operand in switch because it emits a warning in PHP 7.3 and higher.
SlevomatCodingStandard.ControlStructures.DisallowEmpty
Disallows use of empty().
SlevomatCodingStandard.ControlStructures.DisallowNullSafeObjectOperator
Disallows using ?-> operator.
SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator ๐ง
Disallows short ternary operator ?:.
Sniff provides the following settings:
fixable: the sniff is fixable by default, however in strict code it makes sense to forbid this weakly typed form of ternary altogether, you can disable fixability with this option.
SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperator ๐ง
Ternary operator has to be reformatted when the operator is not leading the line.
# wrong
$t = $someCondition ?
$thenThis :
$otherwiseThis;
# correct
$t = $someCondition
? $thenThis
: $otherwiseThis;
SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing ๐ง
Enforces configurable number of lines around jump statements (continue, return, ...).
Sniff provides the following settings:
allowSingleLineYieldStacking: whether or not to allow multiple yield/yield from statements in a row without blank lines.linesCountBefore: allows to configure the number of lines before jump statement.linesCountBeforeFirst: allows to configure the number of lines before first jump statement.linesCountBeforeWhenFirstInCaseOrDefault: allows to configure the number of lines before jump statement that is first incaseordefaultlinesCountAfter: allows to configure the number of lines after jump statement.linesCountAfterLast: allows to configure the number of lines after last jump statement.linesCountAfterWhenLastInCaseOrDefault: allows to configure the number of lines after jump statement that is last incaseordefaultlinesCountAfterWhenLastInLastCaseOrDefault: allows to configure the number of lines after jump statement that is last in lastcaseordefaultjumpStatements: allows to narrow the list of checked jump statements.
For example, with the following setting, only continue and break keywords are checked.
<rule ref="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing">
<properties>
<property name="jumpStatements" type="array">
<element value="continue"/>
<element value="break"/>
</property>
</properties>
</rule>
SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses ๐ง
LanguageConstructWithParenthesesSniff checks and fixes language construct used with parentheses.
SlevomatCodingStandard.ControlStructures.NewWithParentheses ๐ง
Requires new with parentheses.
SlevomatCodingStandard.ControlStructures.NewWithoutParentheses ๐ง
Reports new with useless parentheses.
SlevomatCodingStandard.ControlStructures.RequireMultiLineCondition ๐ง
Enforces conditions of if, elseif, while and do-while with one or more boolean operators to be split to more lines
so each condition part is on its own line.
Sniff provides the following settings:
minLineLength: specifies minimum line length to enforce condition to be split. Use 0 value to enforce for all conditions, regardless of length.booleanOperatorOnPreviousLine: boolean operator is placed at the end of previous line when fixing.alwaysSplitAllConditionParts: require all condition parts to be on its own line - it reports error even if condition is already multi-line but there are some condition parts on the same line.
SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator ๐ง
Ternary operator has to be reformatted to more lines when the line length exceeds the given limit.
Sniff provides the following settings:
lineLengthLimit(default:0)minExpressionsLength(default:null): when the expressions after?are shorter than this length, the ternary operator does not have to be reformatted.
SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator ๐ง
Requires use of null coalesce equal operator when possible.
This sniff provides the following setting:
enable: either to enable or not this sniff. By default, it is enabled for PHP versions 7.4 or higher.checkIfConditions(default:false): will checkifconditions too.
SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator ๐ง
Requires use of null coalesce operator when possible.
SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator ๐ง
Requires using ?-> operator.
Sniff provides the following settings:
enable: either to enable or not this sniff. By default, it is enabled for PHP versions 8.0 or higher.
SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition ๐ง
Enforces conditions of if, elseif, while and do-while to be on a single line.
Sniff provides the following settings:
maxLineLength: specifies max allowed line length. If condition (and the rest of the line) would fit on it, it's enforced. Use 0 value to enforce for all conditions, regardless of length.alwaysForSimpleConditions: allows to enforce single line for all simple conditions (i.e no&&,||orxor), regardless of length.
SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator ๐ง
Requires short ternary operator ?: when possible.
SlevomatCodingStandard.ControlStructures.RequireTernaryOperator ๐ง
Requires ternary operator when possible.
Sniff provides the following settings:
ignoreMultiLine(default:false): ignores multi-line statements.
SlevomatCodingStandard.ControlStructures.DisallowYodaComparison ๐ง
SlevomatCodingStandard.ControlStructures.RequireYodaComparison ๐ง
Yoda conditions decrease code comprehensibility and readability by switching operands around comparison operators forcing the reader to read the code in an unnatural way.
Sniff provides the following settings:
alwaysVariableOnRight(default:false): moves variables always to right.
DisallowYodaComparison looks for and fixes such comparisons not only in if statements but in the whole code.
However, if you prefer Yoda conditions, you can use RequireYodaComparison.
SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn ๐ง
Reports useless conditions where both branches return true or false.
Sniff provides the following settings:
assumeAllConditionExpressionsAreAlreadyBoolean(default:false).
SlevomatCodingStandard.ControlStructures.UselessTernaryOperator ๐ง
Reports useless ternary operator where both branches return true or false.
Sniff provides the following settings:
assumeAllConditionExpressionsAreAlreadyBoolean(default:false).