Respect @phpstan-all-methods-impure, @phpstan-all-methods-pure, and method-level purity annotations on built-in classes#5539
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…nd method-level purity annotations on built-in classes - In PhpClassReflectionExtension::createMethod(), the built-in class path (line 614 condition) determined hasSideEffects solely from signature map metadata, ignoring PHPDoc purity annotations from stubs - Added checks for method-level @phpstan-pure/@phpstan-impure from $currentResolvedPhpDoc->isPure(), then class-level @phpstan-all-methods-pure/@phpstan-all-methods-impure from $declaringClass->getResolvedPhpDoc() - This mirrors the existing logic in createUserlandMethodReflection() (lines 908-932) which correctly handles these annotations - Initialized $currentResolvedPhpDoc before the method signature loop to avoid undefined variable when the loop doesn't execute
Member
|
Personally I think the tests should put the annotations above some non-sensical class we'll never do it for everyone, like SplFileObject or something. Once we register a stub file for Memcached for everyone, all the tests lose their meaning. |
Contributor
|
I took the fix and the tests (with SplFileStorage) in #5536 |
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 creating method reflections for built-in PHP classes (like
Memcached),PhpClassReflectionExtension::createMethod()takes a special path for classes that are built-in or enums and have method signatures in the signature map. This path determinedhasSideEffectssolely from the signature map metadata, completely ignoring PHPDoc purity annotations (@phpstan-all-methods-impure,@phpstan-all-methods-pure,@phpstan-pure,@phpstan-impure) from stubs.This meant that adding a stub like:
had no effect on built-in classes whose methods exist in the signature map, causing false positives when PHPStan cached/narrowed method return values across calls.
Changes
PhpClassReflectionExtension::createMethod()(src/Reflection/Php/PhpClassReflectionExtension.php):@phpstan-pure/@phpstan-impurefrom$currentResolvedPhpDoc->isPure()@phpstan-all-methods-pure/@phpstan-all-methods-impurefrom$declaringClass->getResolvedPhpDoc()TrinaryLogic::createMaybe()as before$currentResolvedPhpDocbefore the method signature loop to fix an undefined variable warningRoot cause
The built-in class path in
createMethod()(guarded by$declaringClass->isBuiltin() || $declaringClass->isEnum()) only consulted$this->signatureMapProvider->hasMethodMetadata()for side-effect information. The userland path (createUserlandMethodReflection()) already had the correct logic at lines 908-932 checking both method-level and class-level purity annotations. The fix replicates this pattern in the built-in path.Test
tests/PHPStan/Rules/Comparison/Bug14534Test.php: Regression test for the reported bug —@phpstan-all-methods-impureonMemcachedvia stub, verifying no falsenotIdentical.alwaysFalseerror ongetResultCode()in a do-while looptests/PHPStan/Rules/Pure/Bug14534AllPureBuiltinTest.php: Analogous test for@phpstan-all-methods-pureon a built-in class — verifying a pure function can call methods without "Possibly impure call" errorstests/PHPStan/Rules/Comparison/Bug14534MethodImpureTest.php: Test for method-level@phpstan-impureon a specific built-in method via stubAll three tests fail without the fix and pass with it.
Fixes phpstan/phpstan#14534