Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nolros's avatar
Level 23

Laracasts Commander return question / PHP programming best practice

All,

Inside of a Laracasts Commander Handler any sort of return statement is frowned upon by PHP Storm. The warning is function declared void must not return a value.

Although it does work is it bad practice and therefore PHP Storm has an issue or is it just that PHP Storm cannot resolve back to request?

Example, this will work but PHP Storm will give me the above warning.

public function handle($command)
{
return $this->UserRepository->findUserbyUserId($command->userId);
}

Option 2 - Return based on condition:

public function handle($command)
{
$value = $this->UserRepository->findUserbyUserId($command->userId);

if (is_null($value))
{
return 'This failed' // and or a error, a constant but ensure something is returned
} 
else
{
return $value;
}

Option 3 - only one return

public function handle($command)
{
$value = $this->UserRepository->findUserbyUserId($command->userId);

if (is_null($value))
{
$statusCode = 'This failed';
}
else
{
$statusCode = '$value;
}

return $statusCode;

or some sort of other option?

Thanks Nolan

0 likes
2 replies
Devon's avatar
Devon
Best Answer
Level 18

@nolros, do you have @ return void in the docblock above the handle() method?

If so, try changing this to an appropriate value such as string, bool, or something... You could also simply throw an exception if it fails and then catch it in the controller.

nolros's avatar
Level 23

@devon you rock! thank you so much. When I change it to @return mixed, it is happy. Thanks a million.

Please or to participate in this conversation.