Three Return Errors

../_images/three_return_errors.png

There are three errors in this code.

The first is the first return: PHP linter complains that A function with return type must return a value. That is true, but with the initial throw, the returns won’t be ever reached. And removing empty returns is sufficient to appease the linter.

Indeed, the second return pass linting phase, and it is bound to fail at execution time. It is obvious that returning 1 instead of A will fail, but the code could have been returning a variable, whose type could not have been checked. So, PHP left all the checks aside.

Finally, the last wrong part of this code is the return type: the exception makes it irrelevant, and, even in the case of a method, it could have been replaced by the never type, which is covariant with every other type. At that point, PHP complains that the method cannot return.

Definitely, this code doesn’t want to work.

See Also

PHP Error Messages

PHP Features