Výrazy
Většina rozhodnutí o operátorech je otázka zápisu: && místo and,
?? místo ternárního operátoru s isset(), Yoda podmínky, závorky tam, kde se priorita operátorů
snadno splete. Přísné porovnání === místo == ale může změnit, co kód dělá, a sekce
expressions s ním zachází opatrně.
Oprava comparison.loose: forbidden je proto riziková (risky fix): z == udělá === a
kód, který na volném porovnání stál, se pak chová jinak. S typy
z PHPStanu ji DressCode udělá sám tam, kde kód porovnává hodnoty, u kterých volné i přísné porovnání dá
totéž; jinde čeká na váš souhlas v klíči fixRisky.
expressions:
comparison:
loose: forbidden
notEquals: '!='
wordLogicalOperator: forbidden
assignment:
targetInValue: forbidden
coalesce: required
ternary:
short: required
ofTrueFalse: forbidden
doubleNegation: forbidden
if ($status == 'paid' and $total <> 0) { // The `==` comparison must be written `===`. // The `and` operator must be written `&&`. // The `<>` comparison must be written `!==`.
$count = $count + 1; // The assignment must be written `+=` instead of repeating its target.
$name = isset($input) ? $input : 'anonymous'; // The ternary testing for null must be written with `??`.
$title = $this->title ? $this->title : $default; // The ternary repeating its condition must be written `?:`.
$valid = $total > 0 ? true : false; // Useless ternary operator, because the condition itself is the result.
$flag = !!$value; // The double negation `!!` must be written as a `(bool)` cast.
}
if ($status === 'paid' && $total !== 0) {
$count += 1;
$name = $input ?? 'anonymous';
$title = $this->title ?: $default;
$valid = $total > 0;
$flag = (bool) $value;
}
Na <> se tu sahá dvakrát: notEquals z něj udělá != a loose pak
!==.
expressions.cast
The type name of a cast.
canonical:(int),(bool),(float),(string)in lowercase, never(integer),(Boolean),(double),(binary)keep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs canonical, psr12 canonical,
nette canonical, symfony canonical, laravel canonical · pokrývá
lowercase_cast, short_scalar_cast, Generic.PHP.LowerCaseType,
PSR12.Keywords.ShortFormTypeKeywords,
SlevomatCodingStandard.PHP.TypeCast, Squiz.WhiteSpace.CastSpacing
expressions.comparison.loose
A loose comparison, ==, != or <>, is written === or
!==.
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á
strict_comparison, SlevomatCodingStandard.Operators.DisallowEqualOperators
expressions.comparison.notEquals
The operator of inequality.
Not about whether the comparison is loose, which expressions.comparison.loose decides.
'!=':!=, never<>keep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
'!=', symfony '!=', laravel '!=' · pokrývá standardize_not_equals
expressions.comparison.yoda
A Yoda comparison, the constant on its left side, a comparison of two variables or calls staying as it is.
forbidden: never there, the variable on the left and the constant on the right,$a === 1required: always there, the constant on the left,1 === $a, so that an accidental assignment does not compilekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
forbidden, symfony required, laravel forbidden · pokrývá yoda_style,
SlevomatCodingStandard.ControlStructures.DisallowYodaComparison, SlevomatCodingStandard.ControlStructures.RequireYodaComparison
expressions.assignment.targetInValue
$a = $a + $b is $a += $b.
Not about the $a += 1 it may write, which expressions.assignment.addingOne writes as an
increment.
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á
assign_null_coalescing_to_coalesce_equal, long_to_shorthand_operator,
SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator, SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator
expressions.assignment.addingOne
$i += 1, which is $i++, or ++$i by expressions.assignment.increment.
forbidden: never therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
forbidden, symfony forbidden, laravel keep ·
pokrývá standardize_increment
expressions.assignment.increment
An increment or a decrement whose value is not read, a statement or a step of for; inside an expression it stays
as it is.
prefix:++$i,--$ipostfix:$i++,$i--keep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
postfix, symfony prefix, laravel postfix · pokrývá increment_style
expressions.ternary.short
$a ?: $b, written for $a ? $a : $b or the other way round where the condition can be read twice.
required: always thereforbidden: never therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
required, symfony keep, laravel keep · pokrývá ternary_to_elvis_operator,
SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator, SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator
Zkrácení mění počet vyhodnocení podmínky a to nemusí být neškodné:
array_shift($queue) ? array_shift($queue) : null odebere z fronty dva prvky, kdežto zkrácená podoba jen jeden.
Zkracují se proto jen výrazy, jejichž opakované čtení nemá vedlejší účinek: proměnné, prvky polí, vlastnosti,
konstanty a skalární hodnoty. Právě tady se ukazuje, co přináší syntaktický strom: PHP CS Fixer označuje svůj
protějšek ternary_to_elvis_operator za rizikový, protože nad polem tokenů nerozezná volání od proměnné;
DressCode to rozhodne podle stromu.
expressions:
ternary:
short: required
$name = $input ? $input : 'anonymous'; // The ternary repeating its condition must be written `?:`.
$size = $options['size'] ? $options['size'] : 10; // The ternary repeating its condition must be written `?:`.
$next = array_shift($queue) ? array_shift($queue) : null;
$name = $input ?: 'anonymous';
$size = $options['size'] ?: 10;
$next = array_shift($queue) ? array_shift($queue) : null;
expressions.ternary.ofTrueFalse
$a > 1 ? true : false is the condition itself, where it is a boolean.
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á SlevomatCodingStandard.ControlStructures.UselessTernaryOperator
expressions.coalesce
$a ?? $b written for isset($a) ? $a : $b and $a !== null ? $a : $b.
required: always therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
required, symfony keep, laravel keep · pokrývá
ternary_to_null_coalescing, SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator
expressions.wordLogicalOperator
and, or bind weaker than = and are written &&, ||.
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á
logical_operators, Squiz.Operators.ValidLogicalOperators
expressions.doubleNegation
!!$a is (bool) $a.
forbidden: never therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
forbidden, symfony forbidden, laravel forbidden ·
pokrývá no_short_bool_cast
expressions.explicitPrecedence
An operand where logical or bitwise operators are easy to misread is parenthesized.
required: always therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
required, symfony keep, laravel keep ·
pokrývá Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence
expressions.parenthesesAfterConstruct
return ($x); is return $x;.
forbidden: never therekeep: nic se nevynucuje, kód zůstane, jak je
Výchozí keep · standardy: perCs keep, psr12 keep, nette
forbidden, symfony forbidden, laravel forbidden · pokrývá include,
no_unneeded_control_parentheses,
SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses, Squiz.Strings.EchoedStrings