Skip to content

Add many missing return types #49342

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
wants to merge 6 commits into from

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Feb 12, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? no
Deprecations? yes
Tickets Fix part of #47551
License MIT
Doc PR -

We've purposely skipped adding some return types in the 6.0 branch, but it seems like our tooling has also missed return types. This PR is build using a slightly modified version of Psalter. The added return types are based on Psalm's analysis, so we should verify whether they are all correct.

I've split the contribution in 3 commits:

  • ea7bafc adds PHP return types if the method is private, it's a function or it's final.
  • 904ce68 adds PHPdoc for all other discovered return types
  • da68669 adds void return types, these are a lot and I'm personally not sure if we should add void return types to the code base

I propose adding as much PHPdoc return types as possible in 6.3, as this means we have 6 months to receive feedback from the community (whether they are correct) before committing to adding all the types in 7.0.

@wouterj
Copy link
Member Author

wouterj commented Feb 12, 2023

Btw, this is a massive PR. Please let me know if, and how, I should split up this one to ease reviews :)

@nicolas-grekas
Copy link
Member

Thanks for the PR, that was on my list also :)

About void, we specifically decided to NOT add them because it would cause needless BC breaks. I think this is still a wise approach and we should stick to it. Of course, adding @return void works to make the indent clear.

@wouterj wouterj force-pushed the issue-47551/return-types branch from da68669 to cfee93a Compare February 12, 2023 14:30
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Feb 12, 2023

I'm thinking about ways to make this PR reviewable. What about keeping all @return foo in one PR, and all real return types in another? Those are more BC critical.

Another idea: split void from other types?

Of course only if this doesn't double the amount of work for you.

@wouterj
Copy link
Member Author

wouterj commented Feb 12, 2023

As I already split them in commits, I can easily create a PR for each change. Let me close here and open 3 new PRs instead :)

@wouterj
Copy link
Member Author

wouterj commented Feb 12, 2023

Here we go: #49347, #49348, #49349

nicolas-grekas added a commit that referenced this pull request Feb 13, 2023
This PR was merged into the 6.3 branch.

Discussion
----------

Add many missing PHPdoc return types

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | Ref #47551, continues #49342
| License       | MIT
| Doc PR        | -

This PR adds lots of ``@return`` PHPdoc that we missed in the 5.4 branch. These will produce new deprecation warnings, if applications override one of these methods. It will not break backwards compatibility.
Adding these PHPdoc in 6.3 already allow the community to test all these annotations for 6 months in stable version, before committing to adding them as PHP types in 7.0. This way, I hope we patch out any wrong type.

This PR is done using a slightly modified Psalter script, based on Psalm's type inference. See #49348 for the counter PR adding real PHP types to private, final or internal methods.

Commits
-------

e23d8be Add missing PHPdoc return types
nicolas-grekas added a commit that referenced this pull request Feb 13, 2023
This PR was merged into the 6.3 branch.

Discussion
----------

Add PHP types to private methods and functions

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | Ref #47551, continues #49342
| License       | MIT
| Doc PR        | -

This PR adds real PHP types to private, internal or final (excluding ``@final``) methods based on Psalm's type inference. Many more return types are missing still, but let's do them in other contributions.

This change can result in BC breaks in multiple ways, so we should carefully review the changes:

* A method may wrongly be considered "safe" by Psalter. If the method is unsafe (e.g. ``@final``, but not yet real `final`), we should instead add a PHPdoc return type (see #49349)
* Psalm may not infer the correct set of return types. If an unexpected type is returned, this will result in a PHP error.

Commits
-------

384c9a6 Add PHP types to private methods and functions
nicolas-grekas added a commit that referenced this pull request Feb 13, 2023
…(wouterj)

This PR was merged into the 5.4 branch.

Discussion
----------

[ErrorHandler] Do not patch return statements in closures

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

The type patcher script also fixes some return statements automatically to deal with void/null differences. However, the script is a bit too eager and also patches return statements of closures inside methods:

```php
public function something(): void
{
    $someHandler = function (): ?string {
        if (...) {
            return null;
        }
    };
}
```

Statements like this were patched to `return;`, causing a PHP fatal error.

Note: I didn't find any test for the return statement patching, so I also didn't write one for this bug. However, this bug is discovered by our own CI in #49342, so I would say we've covered the fix :)

Commits
-------

1779ee1 [ErrorHandler] Do not patch return statements in closures
nicolas-grekas added a commit that referenced this pull request Feb 13, 2023
This PR was merged into the 6.3 branch.

Discussion
----------

Add void (PHPdoc) return types

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Ref #47551, continues #49342
| License       | MIT
| Doc PR        | -

This introduces lots of void return types to the codebase, following our policy:

* Adding ``@return` void` to any methods that return void is OK
* Adding `void` PHP return type is only OK if the method is internal, private or final (excluding ``@final``).

The changes in this PR are automated through a modified Psalter script. We should particularly review if ``@final`` classes/methods didn't get real PHP void return types, as this is a BC break.

Commits
-------

508ed81 Add void return types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants