Skip to content

fix(diagnostics): array_map respects scalar callback return types#195

Open
calebdw wants to merge 2 commits into
PHPantom-dev:mainfrom
calebdw:calebdw/push-pmrspknwuvvr
Open

fix(diagnostics): array_map respects scalar callback return types#195
calebdw wants to merge 2 commits into
PHPantom-dev:mainfrom
calebdw:calebdw/push-pmrspknwuvvr

Conversation

@calebdw

@calebdw calebdw commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

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 #147

@codecov-commenter

codecov-commenter commented Jul 4, 2026

Copy link
Copy Markdown

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

Codecov Report

❌ Patch coverage is 74.54545% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/completion/variable/raw_type_inference.rs 74.54% 14 Missing ⚠️

📢 Thoughts on this report? Let us know!

@calebdw calebdw force-pushed the calebdw/push-pmrspknwuvvr branch from 3e39ca1 to 39d820e Compare July 4, 2026 04:05
calebdw added 2 commits July 3, 2026 23:07
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.
@calebdw calebdw force-pushed the calebdw/push-pmrspknwuvvr branch from 39d820e to 85085e8 Compare July 4, 2026 04:07
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.

array_map() callbacks type the result as the input type, not the callback's return type

2 participants