symfony: Command::execute() should always return int - deprecate returning null

Right now, Command::execute() returns int|void.

I’d suggest we should deprecate returning void (or null) by throwing a deprecation when the method returns a non-int, in Application.

We would need to update all core commands to return 0; where they return; right now.

And in 5.0, we would make returning a non-int throw an exception, in preparation for adding a real return type in 6.0.

Help wanted.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I realize its too late but the PR doesn’t give any reasoning as to why this change is helpful. It does break a large number of projects (or anyway it broke all of mine) and it makes the code fairly ugly with the lone return 0 at the end of any execute() method. Personally, I find this totally counterproductive.

I think a : int should be added here so the IDE will warn you if you override it with :void? I stayed with 4.4 for about a year and never noticed the deprecation, only now that I upgraded to 5.1 I saw the error.

Thanks @xabbuh i had wrongly assumed because it forced throwing exceptions which were catch later on it would be fine, reading your post I see it removes the silence, so this is my mistake, really sorry

Your error handler defined on line 75 of that exact class does not respect silenced errors.

@ihmels see #33236 for background on the topic

@jschaedl your list looks good

Can we include Commands annotated with @final as well?

yes, unless the annotation says @final since ..., which means “not before 5.0”

You may want to update it like this:

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
    if (0 === error_reporting()) {
         return;
    }
    # print_r($errcontext);
    throw new \Exception("{$errfile} {$errline} - {$errstr}", 0);
});

I don’t get this restriction. Even in a strictly typed language like C I can have an int function that returns nothing, it will be automatically converted to 0. Also you can define main as being void. So why C is more flexible than Symfony here?