fix: recognize arrow function beforeEach() hooks - #6
Open
maks-oleksyuk wants to merge 2 commits into
Open
Conversation
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>
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.
Problem
PestHookPropertyReaderinfers$this->proptypes by parsing thebeforeEach()hook body for$this->prop = ...assignments — but it only recognized a realClosure(function () {}) as the hook argument. Writing the hook as anArrowFunction(fn () => ...), a common Pest idiom for one-line setup, was silently skipped: every$this->propread anywhere in the file fell back tomixed, with no PHPStan diagnostic to flag it.Fix
Both places that gate on the hook argument's type (
extractUsesBeforeEachPropertiesforpest()->beforeEach()->in(), andparseTestFilefor the barebeforeEach()function) now acceptClosure|ArrowFunction.Property extraction was generalized to read from
getStmts()instead of->stmtsdirectly: PhpParser'sArrowFunction::getStmts()wraps its single expression body in a syntheticReturn_, so the extractor now matchesAssignout of either anExpression(closure body) or aReturn_(arrow function body).Testing
assertTypecases intests/Type/data/test-hook-properties.php: object assignment, string literal assignment, and a non-assignment arrow body correctly stayingmixed.PestHookPropertyScopeTest.php(+ArrowScopedfixture) proving thepest()->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.