fix(diagnostics): array_map respects scalar callback return types#195
Open
calebdw wants to merge 2 commits into
Open
fix(diagnostics): array_map respects scalar callback return types#195calebdw wants to merge 2 commits into
calebdw wants to merge 2 commits into
Conversation
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
3e39ca1 to
39d820e
Compare
The `base_name().is_some()` guard in `extract_array_map_element_type` rejected scalar return types (`string`, `int`, `bool`, etc.) because `base_name()` only returns `Some` for class-like names. This caused `array_map(fn(Item $item): string => $item->id, $items)` to infer `list<Item>` (the input element type) instead of `list<string>` (the callback's return type). Fix: replace `base_name().is_some()` with `!is_untyped()`, which accepts any resolved type including scalars. Closes PHPantom-dev#147
…ssion When the callback passed to `array_map` has no explicit return type hint, the LSP now infers the return type by resolving the body expression against the input array's element type. For example, `array_map(fn($item) => $item->id, $items)` where `$items` is `list<Item>` now correctly produces `list<string>` (from `Item::$id`'s type) instead of falling back to `list<Item>`. Implementation: - Extracts the first callback parameter name and maps it to the input array's element type via a synthetic `scope_var_resolver` - Loads the `ClassInfo` for the element type so property access resolution can find class members - For arrow functions: resolves `arrow.expression` directly - For closures: finds the first `return` statement's expression Includes 1 new integration test for inferred return types.
39d820e to
85085e8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
base_name().is_some()guard inextract_array_map_element_typerejected scalar return types (string,int,bool, etc.) becausebase_name()only returnsSomefor class-like names. This causedarray_map(fn(Item $item): string => $item->id, $items)to inferlist<Item>(the input element type) instead oflist<string>(the callback's return type).Fix: replace
base_name().is_some()with!is_untyped(), which accepts any resolved type including scalars.Closes #147