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)
Links to this issue
Commits related to this issue
- bug #33805 [FrameworkBundle] Fix wrong returned status code in ConfigDebugCommand (jschaedl) This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] Fix wrong returned statu... — committed to symfony/symfony by xabbuh 5 years ago
- feature #33775 [Console] Add deprecation message for non-int statusCode (jschaedl) This PR was merged into the 4.4 branch. Discussion ---------- [Console] Add deprecation message for non-int status... — committed to symfony/symfony by nicolas-grekas 5 years ago
- minor #33808 [Console] Throw a TypeError for non-int return value calling Command::execute() (jschaedl) This PR was merged into the 5.0-dev branch. Discussion ---------- [Console] Throw a TypeError... — committed to symfony/symfony by nicolas-grekas 5 years ago
- minor #4570 Command::execute() should always return an integer (derrabus) This PR was merged into the 2.15 branch. Discussion ---------- Command::execute() should always return an integer In Symfo... — committed to PHP-CS-Fixer/PHP-CS-Fixer by SpacePossum 5 years ago
- Return 0 in console commands Per https://github.com/symfony/symfony/issues/33747, required for Symfony 5.0 upgrade. — committed to mikeyclarke/Hipper by mikeyclarke 5 years ago
- Fix Command::execute() to return int See https://github.com/symfony/symfony/issues/33747 — committed to tarantool-php/jobqueue by rybakit 3 years ago
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 anyexecute()
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
yes, unless the annotation says
@final since ...
, which means “not before 5.0”You may want to update it like this:
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?