-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Reconsider the CS rule for returning null in functions #17201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that if this is accepted, the existing return fixer in PHP-CS-Fixer will need to be disabled (see PHP-CS-Fixer/PHP-CS-Fixer#1638 for a discussion about it), and a new fixer might be created to automate the new rule (allowing to update our whole codebase in an automated way). |
IIUC,
|
So, just as a recap for your 2 points, this should be the current CS:
As far as I can see, both cases are taken into account with the current spec. |
anything or void is the same. It is just that PHP calls this void (because other languages call it void too)
you are confusing the case of returning |
You're right @stof, I misunderstood your point. Thank you for the clarification. |
I never liked this specialty of Symfony's coding standard, so if you want to change it: 👍 There's no direct need to change it, though. Code with the current CS will run fine under php 7.1 as long as a function is not declared as But you're probably right. If php introduces the concept of void type functions, Symfony should probably embrace this concept even before making use of the actual feature. |
PHP 7.1 has already done this. |
👍 |
👍 Sounds reasonable to me. |
👍 |
For what it's worth, 👎 from me because of the fact that any assignment of the result of the function will still be |
👍 for clarity. |
👍 |
3 similar comments
👍 |
+1 |
+1 |
We should also reconsider |
Indeed. I've always not liked that fixer. |
👍 i like it |
I'm closing this as "fixed" because the community has accepted it and there is nothing else to do:
|
Technically, we could totally update the existing code in an automated way once we update PHP-CS-Fixer to cover the new standard. And I would even say we should do it, otherwise people will receive warnings from fabbot.io all the time once the automated tool is aware of the new rule |
Please extend needed actions if needed: |
…reguiluz) This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #6614). Discussion ---------- Updated the CS rule about return null; and return; This fixes symfony/symfony#17201 Commits ------- 1cc723a Updated the CS rule about return null; and return;
@symfony/deciders I'm reopening the issue for this discussion, now that |
Not a Symfony decider, but I have always hated how Symfony blurred |
since php 7.1 will force us to do so in master, this should help merges into master. So 👍, even if it's a boring task (it's not only having a command to help, but also doing to job from 2.7 up to master, potentially handle cross version test failures and dealing with conflicts, and forcing everyone to rebase their PRs meanwhile...) |
This PR was merged into the 5.0-dev branch. Discussion ---------- add missing return type in User class | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Fixed a missing type in #31996 chasing #17201 Every method in this class had a return type but this method. Commits ------- 7857f2f add missing return type in User class
…lver (Tobion) This PR was merged into the 4.4 branch. Discussion ---------- use proper return types in ErrorHandler and ArgumentResolver | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | tiny | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | | License | MIT | Doc PR | Found those things while reviewing #31996 which missed some return types due to using `return` instead of `return null`. It's part of fixing #17201 (due to #10717). See also #30869 that somebody was stumbling over. Commits ------- 2f9121b use proper return types in ErrorHandler and ArgumentResolver
Inconsistent return points in functions are going to bite us if we want to add return types to those functions in Symfony 5.0:
I have seen all three cases in Symfony's codebase. I don't know if we can fix all inconsistent return points automatically. The third case needs a different strategy than the other two and an automated tool would have a hard time detecting that case. But at least PhpStorm is pretty good at finding them. On the current 3.4 branch, PhpStorm detects 353 issues. I've browsed them and I've only found a handful of false positives (mostly fixtures and, let's say, interesting usages of I'd say, it's worth fixing those issues in 3.4 and 4.3 now to make adding return types to Symfony 5 easier. |
Or, we can decide it's not worth adding |
Fair enough, in that case we could ignore item 3 on my list and would treat PhpStorm warnings on lines like this one like a false positive.
That wouldn't make fixing the other two cases easier, though. |
This PR was merged into the 3.4 branch. Discussion ---------- Fix inconsistent return points | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17201 (partly) | License | MIT | Doc PR | N/A This PR fixes some inconsistent return points I've discovered while working on #32993. Adding return types made fixing these inconsistencies necessary, see also [this comment](#17201 (comment)). Commits ------- 1a83f9b Fix inconsistent return points.
It helps prevent mistakes, and prevent calling code rom depending on a return value.
is really bad style anyway... It should be rewritten as: $this->logger->error('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]);
return; |
This PR was merged into the 3.4 branch. Discussion ---------- Fix inconsistent return points | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17201 in preparation for #33228 | License | MIT | Doc PR | N/A Inconsistent return points in methods prevent adding return types. I thought, I'll give it a try and fix them. After this PR, PhpStorm's inspection still finds 39 issues, but as far as I can tell, they're either false positives or fixture code. Commits ------- f5b6ee9 Fix inconsistent return points.
This PR was merged into the 4.3 branch. Discussion ---------- Fix inconsistent return points | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17201 in preparation for #33228 | License | MIT | Doc PR | N/A That's #33252 on the 4.3 branch. Commits ------- c26c535 Fix inconsistent return points.
currently, our CS mandate to use
return;
to returnnull
from a function.But there is 2 different cases actually:
return;
makes sensenull
, for whichreturn null;
would be more meaningfulThe original discussion said that PHP had no concept of
void
and so that our CS should not distinguish these 2 cases (and so should have a single rule). But PHP 7.1 now has such concept: https://wiki.php.net/rfc/void_return_typeI think we should revisit this rule in our coding standards, to distinguish functions returning void and functions returning something or null.
what do you think ?
The text was updated successfully, but these errors were encountered: