Report only the function's own flags for a parameter without an allowed constant list - #6660
Open
dylanpulver wants to merge 1 commit into
Open
dylanpulver wants to merge 1 commit into
dylanpulver wants to merge 1 commit into
Conversation
…ed constant list
The fallback branch rejected every builtin constant passed to a parameter whose
type matched the type of some mapped parameter of the same function. For
str_pad() that made $length, an unmapped int, reject any int constant, so
str_pad('bla', PHP_INT_SIZE, 'bla') was reported even though it is a valid
length. The branch now reports a constant only when some other parameter of the
function allows it, which is the misplaced-flag case it exists for.
Closes phpstan/phpstan#15308
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
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.
Closes phpstan/phpstan#15308
str_pad()maps allowed constants for$pad_typebut not for$length. Both areint, so the fallback branch inFunctionCallParametersCheckrejected every builtin constant passed as$length:The branch guards on
$allowedConstantsType->isSuperTypeOf($parameterType), which only asks whether the parameter could be confused with a mapped one by type. That is true of any unmappedintparameter, so a numeric constant used as a plain value is reported.The case the branch exists for (#14079) is a flag of the same function in the wrong position, so it now also requires that some other parameter actually allows the constant.
str_pad('bla', STR_PAD_LEFT, 'bla')is still reported;PHP_INT_SIZEis not.Membership is read through the existing
ExtendedParameterReflection::checkAllowedConstants(), so no getter is added toParameterAllowedConstants, which is@api.FunctionCallParametersCheck::check()is shared by the function, method, static method, instantiation, callable and attribute rules, so the fix covers all of them.Test Plan
tests/PHPStan/Rules/Functions/data/bug-15308.phpcarries the reporter's snippet, aPHP_INT_SIZEvariant that does not depend on the sodium extension, and theSTR_PAD_LEFTcase that must still be reported. Stashing the source change makestestBug15308fail.On the reproducer,
bin/phpstan analyse -l 8goes from 3 errors to the 1 correct one. The six rule test classes that use this check go from 620 to 621 tests with no other change,phpcsis clean, andbin/phpstanself-analysis reports no errors.🤖 Generated with Claude Code
https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo