htmlpurifier icon indicating copy to clipboard operation
htmlpurifier copied to clipboard

fix: Fixed missing return statement

Open PHPGangsta opened this issue 3 years ago • 6 comments

Fixes: 127 Method HTMLPurifier_VarParser::parse() should return string but return statement is missing

PHPGangsta avatar Nov 18 '22 21:11 PHPGangsta

Is there a way to suppress the warning with a noreturn annotation on the method call that would be preferred

ezyang avatar Nov 19 '22 05:11 ezyang

If there is no return statement, the function returns "void", correct? If you store the return value to a variable, it's stored as "null".

"void" as a return type is possible since PHP 7.1.0.

So you would prefer something like @return string|void in the PHPDoc, if that's possible and fixes the phpstan warning, right?

PHPGangsta avatar Nov 19 '22 09:11 PHPGangsta

Not necessarily! If you raise an exception you don't need to return

ezyang avatar Nov 19 '22 21:11 ezyang

Oh, you are right! $this->errorGeneric() always throws an Exception. Maybe the function errorGeneric() should have a PHPDoc of @throws HTMLPurifier_VarParserException then phpstan might not report the missing return statement anymore.

I was also thinking about changing the PHPDoc of function parse() to @return string|null because it can return null in line 72.

PHPGangsta avatar Nov 19 '22 21:11 PHPGangsta

If I set @return string|null the error in phpstan is gone, but I still get the error in PHPStorm "Missing 'return' statement": image

If I set it to @return string|null|void both errors are gone.

How do we proceed?

PHPGangsta avatar Nov 22 '22 17:11 PHPGangsta

If there is no way to indicate to PHPStorm that errorGeneric always throws an exception, refactor the code so that the helper function returns the exception object, and throw it at the call site.

ezyang avatar Nov 22 '22 19:11 ezyang