Skip to content

fix: recognize arrow function beforeEach() hooks - #6

Open
maks-oleksyuk wants to merge 2 commits into
pestphp:5.xfrom
maks-oleksyuk:fix/arrow-function-beforeeach-hooks
Open

fix: recognize arrow function beforeEach() hooks#6
maks-oleksyuk wants to merge 2 commits into
pestphp:5.xfrom
maks-oleksyuk:fix/arrow-function-beforeeach-hooks

Conversation

@maks-oleksyuk

@maks-oleksyuk maks-oleksyuk commented Aug 6, 2026

Copy link
Copy Markdown

Problem

PestHookPropertyReader infers $this->prop types by parsing the beforeEach() hook body for $this->prop = ... assignments — but it only recognized a real Closure (function () {}) as the hook argument. Writing the hook as an ArrowFunction (fn () => ...), a common Pest idiom for one-line setup, was silently skipped: every $this->prop read anywhere in the file fell back to mixed, with no PHPStan diagnostic to flag it.

Fix

Both places that gate on the hook argument's type (extractUsesBeforeEachProperties for pest()->beforeEach()->in(), and parseTestFile for the bare beforeEach() function) now accept Closure|ArrowFunction.

Property extraction was generalized to read from getStmts() instead of ->stmts directly: PhpParser's ArrowFunction::getStmts() wraps its single expression body in a synthetic Return_, so the extractor now matches Assign out of either an Expression (closure body) or a Return_ (arrow function body).

Testing

  • 3 new assertType cases in tests/Type/data/test-hook-properties.php: object assignment, string literal assignment, and a non-assignment arrow body correctly staying mixed.
  • 2 new cases in PestHookPropertyScopeTest.php (+ ArrowScoped fixture) proving the pest()->extend(...)->beforeEach(fn () => ...)->in(...) directory-scoped path also resolves and doesn't leak into sibling directories.
  • pest --parallel: 468 passed. phpstan analyse: no errors. pint --test: clean.

maks-oleksyuk and others added 2 commits August 6, 2026 10:51
PestHookPropertyReader only handled Closure hook bodies, so an
ArrowFunction beforeEach() silently degraded every $this->prop read
to mixed. Covered with 5 new tests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Leftover from the earlier arrow-function narrowing fix — rector's
AddArrowFunctionReturnTypeRector flags this on every run otherwise.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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