1.2.333. Default Then Discard¶
Discard the value before assigning it.
In the code below, the variable is assigned a default value. Then, this value is immediately tested and discarded.
It is more readable to test the value, and discard it, or assign it later, rather than assign first then discard it later.
<?php
$a = $a ?? null;
if ($a === null) {
throw new Exception();
}
doSomething();
// Alternative code
if (!isset($a) || $a === null) {
throw new Exception();
}
// $a has a valid value for the purpose
doSomething();
?>
1.2.333.1. Connex PHP features¶
1.2.333.1.1. Suggestions¶
Test the value and bail out if it is not valid before assigning it
1.2.333.1.2. Specs¶
Short name |
Structures/DefaultThenDiscard |
Rulesets |
|
Exakat since |
2.5.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Medium |
Available in |