Skip to content

Substitute generic return types when the native hint is nullable#152

Merged
AJenbo merged 1 commit into
PHPantom-dev:mainfrom
MrSrsen:fix/nullable-native-generic-return
Jun 20, 2026
Merged

Substitute generic return types when the native hint is nullable#152
AJenbo merged 1 commit into
PHPantom-dev:mainfrom
MrSrsen:fix/nullable-native-generic-return

Conversation

@MrSrsen

@MrSrsen MrSrsen commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Fixes: #151

should_override_type_typed used unwrap_nullable(), which only strips the ?Foo (Nullable) form and not the Foo|null (Union-with-null) form. A nullable-union native such as object|null therefore reached the union branch with its null member attached; since object and null are both scalar names, the branch judged the type unrefinable and discarded a generic docblock return like @psalm-return ?T, leaving the bare native.

Use non_null_type() (strips null from both Nullable and Union forms) so the non-null part is analysed, matching the function's documented Foo|null → Foo intent. Fixes inherited generic returns such as Doctrine's ServiceEntityRepository<T>::find(): ?T resolving to object|null.

Adds an assert_type regression fixture.

AJenbo
AJenbo previously approved these changes Jun 8, 2026
@MrSrsen

MrSrsen commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@AJenbo I think CI won't pass unless you run the CI because my fork has no access to project secrets. Or, can I do something about the CI myself?

`should_override_type_typed` used `unwrap_nullable()`, which only strips
the
`?Foo` (Nullable) form and not the `Foo|null` (Union-with-null) form. A
nullable-union native such as `object|null` therefore reached the union
branch with its `null` member attached; since `object` and `null` are
both
scalar names, the branch judged the type unrefinable and discarded a
generic
docblock return like `@psalm-return ?T`, leaving the bare native.

Use `non_null_type()` (strips null from both Nullable and Union forms)
so the
non-null part is analysed, matching the function's documented `Foo|null
→ Foo`
intent. Fixes inherited generic returns such as Doctrine's
`ServiceEntityRepository<T>::find(): ?T` resolving to `object|null`.

Adds an assert_type regression fixture.

Signed-off-by: Anders Jenbo <anders@jenbo.dk>
@AJenbo AJenbo force-pushed the fix/nullable-native-generic-return branch from ff8d13e to d485d6d Compare June 20, 2026 13:21
@AJenbo AJenbo merged commit 655f99b into PHPantom-dev:main Jun 20, 2026
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AJenbo

AJenbo commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Nice work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants