Fix phpstan/phpstan#11100: Possibly impure call to function array_map() in pure method - false positive#5197
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…_map() with pure callback - Added array_map to functionMetadata_original.php with hasSideEffects: false - Regenerated resources/functionMetadata.php - New regression test in tests/PHPStan/Rules/Pure/data/bug-11100.php - The root cause was that array_map had no metadata entry, so hasSideEffects() returned "maybe", causing a "Possibly impure" error even when the callback was provably pure. Callback impure points are already propagated separately via processArgs(), so marking array_map itself as side-effect-free is correct.
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
array_map()is called with a pure static closure inside a@phpstan-puremethod, PHPStan incorrectly reports "Possibly impure call to function array_map()". This is a false positive becausearray_map()itself has no side effects beyond invoking its callback, and the callback's purity is already tracked separately.Changes
'array_map' => ['hasSideEffects' => false]tobin/functionMetadata_original.phpresources/functionMetadata.phpviabin/generate-function-metadata.phptests/PHPStan/Rules/Pure/data/bug-11100.phpandtests/PHPStan/Rules/Pure/PureMethodRuleTest.phpRoot cause
array_maphad no entry in the function metadata, soNativeFunctionReflectionProviderassigned itTrinaryLogic::createMaybe()forhasSideEffects. This causedSimpleImpurePoint::createFromVariant()to always create a "possibly impure" point for the function call itself, even when the callback argument was provably pure.Since PHPStan already propagates the callback's impure points separately through
NodeScopeResolver::processArgs()(when the callback is immediately invoked), markingarray_mapashasSideEffects: falseis correct — any side effects from the callback are still reported, but the function call itself no longer generates a spurious impure point.This is consistent with how other callback-taking array functions like
array_diff_uassoc,array_diff_ukey,array_udiff, etc. are already marked ashasSideEffects: false.Test
Added
tests/PHPStan/Rules/Pure/data/bug-11100.phpreproducing the original issue: a@phpstan-puremethod callingarray_map()with a pure static closure. The test expects no errors.Fixes phpstan/phpstan#11100