Because it is dynamically typed, PHP does not enforce a return type on a function. This means that different paths through a function can return
different types of values, which can be very confusing to the user and significantly harder to maintain.
In particular, it is consequently also possible to mix empty return
statements (implicitly returning null
) with some
returning an expression. This rule verifies that all the return
statements from a function are consistent.
Noncompliant code example
function foo($a) { // Noncompliant, function will return "true" or null
if ($a == 1) {
return true;
}
return;
}
Compliant solution
function foo($a) {
if ($a == 1) {
return true;
}
return false;
}
or
function foo($a) {
if ($a == 1) {
return true;
}
return null;
}