Skip to content

feat(code-action): convert arrow function to closure - #191

Merged
AJenbo merged 1 commit into
PHPantom-dev:mainfrom
calebdw:calebdw/push-tlprmqnqspkt
Jul 4, 2026
Merged

feat(code-action): convert arrow function to closure#191
AJenbo merged 1 commit into
PHPantom-dev:mainfrom
calebdw:calebdw/push-tlprmqnqspkt

Conversation

@calebdw

@calebdw calebdw commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Add a refactor.rewrite code action that converts arrow functions to anonymous closures: fn($x) => $x * 2 becomes
function($x) { return $x * 2; }.

Variables from the outer scope used in the expression are automatically detected and captured via a use() clause. $this is excluded since closures bind it automatically (unless static). Nested arrow functions are handled by extending the parameter exclusion set.

Preserves:

  • static keyword
  • Return type hints
  • Parameter type hints

Includes 10 unit tests covering: simple expressions, type hints, static arrows, single/multiple variable capture, $this exclusion, parameter exclusion, method calls with captures, and deduplication.

Closes #148

@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 54.46072% with 342 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/code_actions/convert_to_closure.rs 54.40% 342 Missing ⚠️

📢 Thoughts on this report? Let us know!

Add a refactor.rewrite code action that converts arrow functions to
anonymous closures: `fn($x) => $x * 2` becomes
`function($x) { return $x * 2; }`.

Variables from the outer scope used in the expression are automatically
detected and captured via a `use()` clause. `$this` is excluded since
closures bind it automatically (unless static). Nested arrow functions
are handled by extending the parameter exclusion set.

Preserves:
- static keyword
- Return type hints
- Parameter type hints

Includes 10 unit tests covering: simple expressions, type hints,
static arrows, single/multiple variable capture, $this exclusion,
parameter exclusion, method calls with captures, and deduplication.

Closes PHPantom-dev#148

Signed-off-by: Anders Jenbo <anders@jenbo.dk>
@AJenbo
AJenbo force-pushed the calebdw/push-tlprmqnqspkt branch from 5368dff to 762be3a Compare July 4, 2026 02:38
@AJenbo
AJenbo merged commit 4951466 into PHPantom-dev:main Jul 4, 2026
7 checks passed
@calebdw
calebdw deleted the calebdw/push-tlprmqnqspkt branch July 4, 2026 02:46
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.

Refactor short closures into multiline ones and the other way around

3 participants