Fix #10396: preg_replace_callback doesn't account for flags in callback signature#4971
Open
phpstan-bot wants to merge 1 commit into2.1.xfrom
Open
Fix #10396: preg_replace_callback doesn't account for flags in callback signature#4971phpstan-bot wants to merge 1 commit into2.1.xfrom
phpstan-bot wants to merge 1 commit into2.1.xfrom
Conversation
…reg_replace_callback and preg_replace_callback_array callback signatures Fixes phpstan/phpstan#10396
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
Fixes phpstan/phpstan#10396
When
preg_replace_callbackorpreg_replace_callback_arrayis called withPREG_OFFSET_CAPTUREand/orPREG_UNMATCHED_AS_NULLflags, PHPStan now accounts for these flags when determining the expected callback parameter type.Previously, PHPStan always expected
callable(array<string>): stringregardless of flags, causing false positives when users correctly typed their callbacks for flag-aware match arrays (e.g.,array<int|string, array{string|null, int}>forPREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL).Additionally,
preg_replace_callback_arraycallbacks now get proper type inference for the$matchesparameter when flags are used (previously always inferred asmixed).Changes
New
PregReplaceCallbackArgVisitor: AST visitor that sets apregReplaceCallbackFlagsattribute on callback arguments (forpreg_replace_callback) and on closure/arrow function nodes inside the array argument (forpreg_replace_callback_array), following the same pattern asArrayMapArgVisitor.ParametersAcceptorSelector: When the flags attribute is present, overrides the callback parameter type to account forPREG_OFFSET_CAPTURE(match values becomearray{string, int<-1, max>}) andPREG_UNMATCHED_AS_NULL(match values includenull). This fixes call-site validation.NodeScopeResolver::createCallableParameters(): When a closure/arrow function has the flags attribute, computes flag-aware callable parameters for scope inference inside the closure body. This fixes type inference for$matchesinsidepreg_replace_callback_arraycallbacks.MutatingScope::getClosureType(): Reads the flags attribute to construct the correct external closure type.Flag behavior
stringPREG_UNMATCHED_AS_NULLstring|nullPREG_OFFSET_CAPTUREarray{string, int<-1, max>}array{string|null, int<-1, max>}