Allow null values in native preg_replace_callback callback array type for PREG_UNMATCHED_AS_NULL#5987
Merged
staabm merged 1 commit intoJul 4, 2026
Conversation
…pe for `PREG_UNMATCHED_AS_NULL` - Change the `callback` parameter signature in `functionMap.php` and `functionMap_php74delta.php` from `callable(array<int|string, string>):string` to `callable(array<int|string, string|null>):string`, so the native match-array value type reflects that groups can be null when `PREG_UNMATCHED_AS_NULL` is passed. - Fixes a spurious `notIdentical.alwaysTrue` / `deadCode.unreachable` report with `treatPhpDocTypesAsCertain: false`: the strict-comparison rule then falls back to the native type, which previously claimed match values were always non-null `string`. - Adjacent regex functions were probed and found already correct: `preg_match`/`preg_match_all` set the native `$matches` out type via `PregMatchParameterOutTypeExtension` (already null-aware via the shape matcher), and `preg_replace_callback_array` performs no shape inference on its callbacks.
staabm
approved these changes
Jul 4, 2026
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
With
treatPhpDocTypesAsCertain: false, PHPStan reported a spuriousStrict comparison using !== between string and null will always evaluate to true.(and a follow-upUnreachable statement) inside apreg_replace_callbackcallback that checks a named capture group againstnull, even thoughPREG_UNMATCHED_AS_NULLwas passed and the group can legitimately benull.The regex shape extension already infers the precise PHPDoc type (
'b'|null) for the match array, so the default configuration behaves correctly. The problem only appeared withtreatPhpDocTypesAsCertain: false, where the always-true check falls back to the native type of$match. That native type came from the function signature map entry forpreg_replace_callback, which declared the callback ascallable(array<int|string, string>):string— asserting the values are always non-nullstring.Changes
resources/functionMap.php:preg_replace_callbackcallback signature changed fromcallable(array<int|string, string>):stringtocallable(array<int|string, string|null>):string.resources/functionMap_php74delta.php: same change (this delta overrides the base map for PHP ≥ 7.4).tests/PHPStan/Rules/Comparison/data/bug-14904.php+StrictComparisonOfDifferentTypesRuleTest::testBug14904: regression test withtreatPhpDocTypesAsCertain = false.tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes.php: addedassertNativeType('array<int|string, string|null>', $match)(alongside the existing precise PHPDocassertType) to lock in the corrected native type.Analogous cases probed:
preg_match/preg_match_all: their$matchesout type is produced byPregMatchParameterOutTypeExtension, which sets both the PHPDoc and native type from the regex shape matcher (already null-aware). Not affected.preg_replace_callback_array: no per-callback shape inference exists, so callback match arrays stayarray(value typemixed) and never triggered a false always-true. Not affected.Root cause
When
treatPhpDocTypesAsCertainisfalse, always-true/false comparison rules re-evaluate using native types. The native type of the callback parameter is taken from the function signature map, not from the regex shape extension (which only overrides the PHPDoc parameter type). The signature map over-claimed the match values as non-nullstring, so$match['b'] !== nullwas deemed always true. SincePREG_UNMATCHED_AS_NULLgenuinely makes unmatched groupsnull, the native signature is simply widened tostring|null.Test
StrictComparisonOfDifferentTypesRuleTest::testBug14904reproduces the reported issue (fails before the fix with10: Strict comparison ... will always evaluate to true., passes after).preg_replace_callback_shapes.phpgains aassertNativeTypeassertion locking the corrected native match-array type while keeping the precise PHPDoc shape.Fixes phpstan/phpstan#14904