Skip to content

Fix missing and too-wide return types with --fix, add projectType - #7283

Merged
ondrejmirtes merged 24 commits into
2.3.xfrom
fix-return-types
Dec 2, 2026
Merged

ondrejmirtes merged 24 commits into
2.3.xfrom
fix-return-types

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Adds a projectType: open|closed parameter and --fix support for missing and too-wide return types. Both fixes are under one feature toggle, fixMissingReturnTypes, which is enabled in bleeding edge.

Missing return types

  • missingType.return and missingType.iterableValue of functions and methods are now reported on FunctionReturnStatementsNode / MethodReturnStatementsNode. The rules therefore see the precise inferred return types. Yields record their own scope, so generators get key, value, send and return types.
  • The fix adds a native type. It also adds a @return tag when the native type cannot express the type: iterables, or types like static on PHP 7.4.
  • The fixer never writes mixed. It generalizes constant values and large array shapes, and uses an anonymous class's parent or interfaces.
  • It skips functions that return a value as never or void.
  • Methods that can be overridden are coordinated in a CollectedDataNode rule, so parents stay as wide as their children need. Types stay invariant before PHP 7.4.

Too-wide return types

  • Fixes return.unusedType and return.tooWideBool by removing union members, ?T included. The native type is narrowed first, then the @return tags, and the PHPDoc stays a subtype of the native type.
  • Applies to functions and methods that can't be overridden. In closed projects, and in open projects with checkTooWideReturnTypesInProtectedAndPublicMethods, the fix is coordinated across the class hierarchy.
  • Skipped when the returned values are accepted only by coercion. Also skipped when the parent's return type does not overlap with what the method returns.

projectType

  • With closed, the checkTooWide*InProtectedAndPublicMethods options have no effect. Protected and public members are reported through a CollectedDataNode with regard to the methods overriding them, and trait methods are considered from every class that uses them.

Breaking change for ignore error extensions

Missing return types (missingType.return, missingType.iterableValue and similar) of methods with a body are now reported on MethodReturnStatementsNode by the new MissingImplementedMethodReturnTypehintRule, not on InClassMethodNode by MissingMethodReturnTypehintRule.

Stress tests

Sylius, WordPress (open and closed) and two private projects were tested:

  • With 50% of the return typehints removed, --fix restored them in a few rounds.
  • On the unmodified projects, the too-wide fixes introduced no wrong types.

Amp thread: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301

ondrejmirtes added a commit to phpstan/phpstan-phpunit that referenced this pull request Dec 2, 2026
…ReturnTypehintRule too

PHPStan reports missing return types of methods with a body in MissingImplementedMethodReturnTypehintRule
since phpstan/phpstan-src#7283.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
@ondrejmirtes
ondrejmirtes force-pushed the fix-return-types branch 2 times, most recently from 232f827 to d343e47 Compare December 2, 2026 14:37
…heir return statements node

The missing return type and the missing iterable value type of the return type are reported
on FunctionReturnStatementsNode and MethodReturnStatementsNode, so that the rules have access
to the types the function returns. Methods without a body, which have no return statements node,
are still reported on InClassMethodNode.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
An open project, the default, is a library or a framework other code extends, so its classes
can be extended and its methods overridden by code PHPStan does not see. A closed project is an
application whose code is all analysed.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
… regard to the overriding methods

With projectType: closed, the too-wide return, @param-out and @throws types of public and protected
methods in non-final classes are reported regardless of the checkTooWide*InProtectedAndPublicMethods
options. Such a method and the methods overriding it emit which members of the type they use, and
TooWideTypeInOverridableMemberRule reports the members none of them uses, so that removing them keeps
the overriding methods covariant. The type of a method that nothing overrides is reported like the type
of a private method.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…odes

MethodReturnStatementsNode and FunctionReturnStatementsNode now keep the
scope each yield was evaluated in, next to the yield node itself, so rules
can read the key and value types of yields the same way they read the types
of returned expressions.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…eturnTypes feature toggle

Bleeding edge enables the toggle. The missingType.return and missingType.iterableValue
errors are fixed with a native return type of the type the function returns: void when it returns nothing,
never when it always throws, and the Generator its yields describe for a generator, read from the scopes
the return statements node records for its yields. Its TSend is what the generators it delegates to with
yield from accept, the intersection of their TSend types, and mixed when it delegates to none. A function returning a value and also nothing is not
fixed, PHP and PHPStan would report it. Without native union types, objects are declared natively as their
closest common parent class.

A type with mixed in it, even explicit mixed or an array of mixed values, is not written (the mixed key type of
array<V> excepted): callers would
lose precision, while a later round may infer a more specific type once the callees are typed. Only the TSend
of a Generator is mixed when nothing restricts what can be sent to it.

A @return PHPDoc tag is added only when the native type does not say everything: for iterables, callables
and generic classes it carries the full returned type, and when the PHP version cannot declare the type
natively, like static on PHP 7.4 or never on PHP 8.0, it carries the type the newest PHP version would declare.

The methods overriding a method that can be overridden in a closed project are known only after the analysis,
so MissingReturnTypeFixRule reports and fixes these errors. The return type of such a method covers what
the overriding methods return, and its native type is declared only when every overriding method has one
or gets one with its own fix. Methods that can be overridden in an open project are not fixed.

A method of a trait, private ones too, is fixed in MissingReturnTypeFixRule as well: the rules emit what it
returns in each class using the trait, with the types resolved in the trait so that $this is static, and its
return type covers all of them, one fix shared by all the classes. The errors are still reported in the context of
each class. When the trait method can be overridden, it is fixed only in a closed project, covering the overriding methods.

The fixes are tested for PHP 7.4, 8.0, 8.1 and 8.2 with a phpVersion configuration file each.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…r void

A function whose return statements return only the result of a call to a function that never returns,
or to one returning void, returns never or void, but PHP forbids a return statement with a value
in a function declaring never or void. Its missing return type is not fixed.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…ts parent class or interfaces

An anonymous class cannot be named in a return type. It is replaced by its parent class, the intersection
of the interfaces it implements, or object, in the native type and in the @return PHPDoc tag.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
The @return PHPDoc tag written by the fix of a missing return type describes what the function returns
in general, not the literal values of one implementation: constant strings, integers and floats are
generalized, true and false only inside arrays, and an array shape stays a shape only with at most
ten keys that are integers or ASCII identifiers. Otherwise it is a general array of the generalized
value types. Literals copied from a data provider made the tags huge, and a string containing */
ended the PHPDoc comment.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
Every next round of --fix got each file the previous rounds changed as a --fixed-ignored-in option,
even without --fix-ignored, the only option that reads them. After thousands of changed files,
the command of the round exceeded the length the system allows and could not be launched.
The list is now written to a JSON file in tmpDir, passed with --fixed-files, and removed after the round.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
The type of new Foo() says the object is exactly an instance of Foo, so the union of the types of
new RedirectResponse() and new Response() kept both. A written type cannot say that, the fix of a missing
return type declared RedirectResponse|Response, a union with a member that is a subtype of another.
The returned types lose it before they are combined, the fix declares Response.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…Self

A method returning an instance of its own class gets self as its native type and in its @return tag.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…hods

A method overriding or implementing another one inherits its contract: its return type has to be within the
return type the parent method declares, natively or in PHPDoc, and PHP requires its native type to be within
the parent's native type. The fix of a missing return type cannot widen a declared parent return type,
so a method returning something else, like an array where the parent declares WP_REST_Response|WP_Error,
is not fixed. A generalized type that leaves the parent's return type falls back to the returned type.

The return types of the parent methods are resolved in each class, the methods of a trait and the methods
fixed after the analysis in a closed project are kept within them in every class.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
When the fix of a missing return type declares a native type that says everything, it added no @return tag,
but it kept the one the method had, like @return array|Dog of a method now declaring Dog. The tag contradicted
the new type. It gets the new type, or it is removed when it has no description and only repeats the native type.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…values

The check for mixed in the written return type did not check the item type of array<V> and iterable<V>
itself, only the types in it, so array<mixed> was written as the native array type. Mixed is never written,
not even as the value type of an array.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…again

The collected data carries where the method node is and its declared native return type,
so the rule creates the fix without finding the node in a parse of its file.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…HP 7.4

Without return type covariance, a method overriding one with a native return type
declares the same native type and the narrower one in @return, and an overridden method
is fixed only when the methods overriding it already declare the same native type.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…rridden

Under the fixMissingReturnTypes feature toggle, return.unusedType and return.tooWideBool
of functions, private methods and methods that cannot be overridden remove the members
the function never returns, first from the native type, then from the @return tags,
which stay subtypes of the native type. bool becomes true or false only where PHP allows it,
and before PHP 7.4 the native type of an overriding method is not narrowed.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
In a closed project, and in an open project with checkTooWideReturnTypesInProtectedAndPublicMethods,
the return type of a method that can be overridden is fixed in TooWideTypeInOverridableMemberRule.
The overriding methods emit the members of the overridden type they return and declare, the fix keeps them,
so the overridden type stays as wide as the overriding ones need. Usages computed against another version
of the overridden type do not take part in a fix.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…alues

A PHPDoc type is not checked at runtime: a getter of a property documented as string
that a setter assigns null to, or a value narrowed by an inline @var tag, can still be null.
The @return tags are narrowed by the PHPDoc types, the native type keeps the members
the native types of the returned values need.
…e of

When a value is accepted only by coercion, like int by float, or the declared
type does not accept it at all, removing the members the rule reports as unused
can break the function, for example ?float became null in a function returning 1.
…arents

When the return type of the overridden method does not accept the return type of the
overriding one, narrowing the overriding method can make the incompatibility reported.
Like the check without collected data, only nested types of a method overriding another one
keep null. A method implementing Iterator::current() with @return Foo|null that never returns null
was reported in an open project checking protected and public methods, but not in a closed project.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
…eturn

The parent of a method implementing Iterator<int, Foo>::current() with @return Foo|null
returns Foo, which does not accept the declared type but accepts the narrowed one.
Only a parent return type that does not overlap with the returned type blocks the fix.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22c96-9e21-771f-9a29-4627539ba301
@ondrejmirtes
ondrejmirtes merged commit 89cdfaa into 2.3.x Dec 2, 2026
89 of 111 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix-return-types branch December 2, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant