Skip to content

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
phpstan-bot:create-pull-request/patch-c3ipet6
Open

Fix phpstan/phpstan#11100: Possibly impure call to function array_map() in pure method - false positive#5197
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-c3ipet6

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When array_map() is called with a pure static closure inside a @phpstan-pure method, PHPStan incorrectly reports "Possibly impure call to function array_map()". This is a false positive because array_map() itself has no side effects beyond invoking its callback, and the callback's purity is already tracked separately.

Changes

  • Added 'array_map' => ['hasSideEffects' => false] to bin/functionMetadata_original.php
  • Regenerated resources/functionMetadata.php via bin/generate-function-metadata.php
  • Added regression test in tests/PHPStan/Rules/Pure/data/bug-11100.php and tests/PHPStan/Rules/Pure/PureMethodRuleTest.php

Root cause

array_map had no entry in the function metadata, so NativeFunctionReflectionProvider assigned it TrinaryLogic::createMaybe() for hasSideEffects. This caused SimpleImpurePoint::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), marking array_map as hasSideEffects: false is 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 as hasSideEffects: false.

Test

Added tests/PHPStan/Rules/Pure/data/bug-11100.php reproducing the original issue: a @phpstan-pure method calling array_map() with a pure static closure. The test expects no errors.

Fixes phpstan/phpstan#11100

…_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.
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