1.2.1410. Useless Type Check¶
With the type system, checking the type of arguments is handled by PHP.
In particular, a typed argument can’t be null, unless it is explicitly nullable, or has a null
value as default.
<?php
// The test on null is useless, it will never happen
function foo(A $a) {
if (is_null($a)) {
// do something
}
}
// Either nullable ? is too much, either the default null is
function barbar(?A $a = null) {
}
// The test on null is useful, the default value null allows it
function bar(A $a = null) {
if ($a === null) {
// do something
}
}
?>
See also Type Declarations.
1.2.1410.1. Connex PHP features¶
1.2.1410.1.1. Suggestions¶
Remove the nullable typehint
Remove the null default value
Remove tests on null
1.2.1410.1.2. Specs¶
Short name |
Functions/UselessTypeCheck |
Rulesets |
|
Exakat since |
1.8.9 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |