Fix phpstan/phpstan#14325: Incorrect type inference in implode call with list<string> inside of closure#5248
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…eters - Added resolveClosurePhpDocParameterTypes() to extract @param types from PHPDoc comments on the statement containing a closure assignment - Applied resolved PHPDoc parameter types to the closure scope in processClosureNode() - Updated count-type test expectations to reflect correct generic types - New regression test in tests/PHPStan/Analyser/nsrt/bug-14325.php
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.
Summary
When a closure was assigned to a variable with a
@paramPHPDoc (e.g.@param list<string> $array), the PHPDoc types were not applied to the closure's parameters. This caused type degradation inside the closure body — for examplelist<string>became plainarray.Changes
resolveClosurePhpDocParameterTypes()method insrc/Analyser/NodeScopeResolver.phpthat extracts@paramtags from the PHPDoc comment on the parent statement and matches them to closure parametersprocessClosureNode(), applied the resolved PHPDoc parameter types to the closure scope after entering the anonymous function, overriding the expression types while preserving native typestests/PHPStan/Analyser/nsrt/count-type.phpto reflect that closures now correctly receive their PHPDoc generic types (e.g.ArrayObject<int, mixed>instead of bareArrayObject)Root cause
The PHPDoc
@paramtags on a statement like$func = function(array $array): void {}are attached to theStmt\Expressionnode, not theExpr\Closurenode itself. While regular functions (Stmt\Function_) have their PHPDoc resolved viagetPhpDocs()and applied throughenterFunction(), closures only used native type hints (viagetFunctionType()) when setting up their parameter scope inenterAnonymousFunctionWithoutReflection(). The fix reads the PHPDoc from the parent statement and applies@paramtypes to the closure scope.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14325.phpwith assertions verifying that:list<string>from@param(baseline)list<string>from@paramlist<string>from@param$array[] = 'bar') correctly narrows tonon-empty-list<string>in all casesFixes phpstan/phpstan#14325