Skip to content

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

Closed
stof opened this issue Dec 31, 2015 · 29 comments
Closed

Reconsider the CS rule for returning null in functions #17201

stof opened this issue Dec 31, 2015 · 29 comments

Comments

@stof
Copy link
Member

stof commented Dec 31, 2015

currently, our CS mandate to use return; to return null from a function.

But there is 2 different cases actually:

  • a function without any return value (returning void), for which return; makes sense
  • a function returning something or null, for which return null; would be more meaningful

The 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_type

I 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 ?

@stof
Copy link
Member Author

stof commented Dec 31, 2015

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).

@phansys
Copy link
Contributor

phansys commented Dec 31, 2015

IIUC, return; is only used when the function must return an early void, but if the function doesn't return anything, return must be omitted: https://symfony.com/doc/current/contributing/code/standards.html#structure

Use just return; instead of return null; when a function must return void early;

@phansys
Copy link
Contributor

phansys commented Dec 31, 2015

So, just as a recap for your 2 points, this should be the current CS:

  • a function without any return value (returning void)

return statements are fully absent.

  • a function returning something or null

return; statement only present if the return is required before the end of function's flow (early return inside a condition by instance).

As far as I can see, both cases are taken into account with the current spec.

@stof
Copy link
Member Author

stof commented Dec 31, 2015

IIUC, return; is only used when the function must return an early void, but if the function doesn't return anything, return must be omitted

anything or void is the same. It is just that PHP calls this void (because other languages call it void too)

As far as I can see, both cases are taken into account with the current spec.

you are confusing the case of returning null early and returning void early. Your comment assumes they are the same. They are not (and this is precisely my point).

@phansys
Copy link
Contributor

phansys commented Dec 31, 2015

You're right @stof, I misunderstood your point. Thank you for the clarification.

@derrabus
Copy link
Member

derrabus commented Jan 2, 2016

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 void. And it will take some time untill Symfony can set php 7.1 as a minimum requirement in order to make use of the void return type – we're probably talking about Symfony 5 here.

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.

@GrahamCampbell
Copy link
Contributor

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.

@dunglas
Copy link
Member

dunglas commented Jan 3, 2016

👍

@xabbuh
Copy link
Member

xabbuh commented Jan 4, 2016

👍 Sounds reasonable to me.

@Tobion
Copy link
Contributor

Tobion commented Jan 4, 2016

👍

@GrahamCampbell
Copy link
Contributor

For what it's worth, 👎 from me because of the fact that any assignment of the result of the function will still be null.

@thewilkybarkid
Copy link
Contributor

👍 for clarity.

@nicolas-grekas
Copy link
Member

👍

3 similar comments
@romainneutron
Copy link
Contributor

👍

@leofeyer
Copy link
Contributor

leofeyer commented Jan 6, 2016

+1

@huebs
Copy link

huebs commented Jan 6, 2016

+1

@keradus
Copy link
Member

keradus commented Jan 28, 2016

We should also reconsider phpdoc_no_empty_return, which will remove from phpdocs @return void and @return null annotations.

@GrahamCampbell
Copy link
Contributor

Indeed. I've always not liked that fixer.

@DavidBadura
Copy link
Contributor

👍 i like it

@javiereguiluz
Copy link
Member

I'm closing this as "fixed" because the community has accepted it and there is nothing else to do:

  1. The docs have been updated: Updated the CS rule about return null; and return; symfony-docs#6614
  2. We (probably) won't update the existing code to avoid merging conflicts.
  3. We'll use this new rule for the new code added from now on.

@stof
Copy link
Member Author

stof commented May 26, 2016

We (probably) won't update the existing code to avoid merging conflicts.

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

@keradus
Copy link
Member

keradus commented May 26, 2016

Please extend needed actions if needed:
PHP-CS-Fixer/PHP-CS-Fixer#1638 (comment)

xabbuh added a commit to symfony/symfony-docs that referenced this issue Jun 30, 2016
…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;
@stof
Copy link
Member Author

stof commented Jul 25, 2017

@symfony/deciders I'm reopening the issue for this discussion, now that return; and return null; are considered different in PHP 7.1+

@stof stof reopened this Jul 25, 2017
@robfrawley
Copy link
Contributor

Not a Symfony decider, but I have always hated how Symfony blurred null returns and void returns, even if separating such was only a "convention for clarity" prior to PHP 7.1. 👍 To the change. Wasn't this already accepted, though?

@nicolas-grekas
Copy link
Member

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...)

chalasr pushed a commit that referenced this issue Jun 25, 2019
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
fabpot added a commit that referenced this issue Jun 26, 2019
…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
@derrabus
Copy link
Member

derrabus commented Aug 7, 2019

Inconsistent return points in functions are going to bite us if we want to add return types to those functions in Symfony 5.0:

  • As soon as we add a return type, we cannot mix return; and return $someValue; within the same function anymore. https://3v4l.org/RBoMM
  • If we add a a return type other than void, php will not implicitly return null anymore when reaching the end of a function, but emit a TypeError instead. https://3v4l.org/5GrdN
  • If we add void as return type, returning with a value will result in a fatal error, even if we use the result of another void method. https://3v4l.org/B5jgp

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 for loops).

Bildschirmfoto 2019-08-07 um 12 14 06

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.

@nicolas-grekas
Copy link
Member

Or, we can decide it's not worth adding void. I think it provides nothing personally.

@derrabus
Copy link
Member

derrabus commented Aug 7, 2019

Or, we can decide it's not worth adding void. I think it provides nothing personally.

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.

return $this->logger->error('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]);

That wouldn't make fixing the other two cases easier, though.

nicolas-grekas added a commit that referenced this issue Aug 7, 2019
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.
@teohhanhui
Copy link
Contributor

teohhanhui commented Aug 8, 2019

@nicolas-grekas:

it's not worth adding void. I think it provides nothing personally.

It helps prevent mistakes, and prevent calling code rom depending on a return value.

return $this->logger->error('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]);

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;

nicolas-grekas added a commit that referenced this issue Aug 20, 2019
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.
nicolas-grekas added a commit that referenced this issue Aug 20, 2019
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.