Infer non-empty-list for preg_match_all() matches when the match succeeded#5812
Merged
staabm merged 1 commit intoJun 6, 2026
Merged
Conversation
…succeeded - In `RegexArrayShapeMatcher`, intersect the per-pattern `list` types with `NonEmptyArrayType` when `$wasMatched->yes()`, since a successful `preg_match_all()` guarantees at least one match. - Covers all three list-producing sites: the `PREG_PATTERN_ORDER` subject list (`createSubjectValueType`), the `PREG_PATTERN_ORDER` per-group lists (`createGroupValueType`), and the `PREG_SET_ORDER` outer list (`buildArrayType`). - Thread `$wasMatched` into `createSubjectValueType`.
staabm
approved these changes
Jun 6, 2026
VincentLanglet
approved these changes
Jun 6, 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
After a successful
preg_match_all()(e.g. insideif (preg_match_all(...))), the resulting$matchesarrays were inferred with plainlist<...>value types. Because alistmay be empty, accessing$matches[0][0]produced a false positiveOffset 0 might not exist on list<string>.. A successfulpreg_match_all()returns the number of full matches, which is>= 1, so every produced list is guaranteed non-empty.This change infers
non-empty-list<...>for those lists when the match is known to have succeeded.Changes
src/Type/Php/RegexArrayShapeMatcher.php:createSubjectValueType()now takes$wasMatchedand intersects thePREG_PATTERN_ORDERsubject list withNonEmptyArrayTypewhen$wasMatched->yes().createGroupValueType()intersects thePREG_PATTERN_ORDERper-group list withNonEmptyArrayTypewhen$wasMatched->yes().buildArrayType()intersects thePREG_SET_ORDERouter list withNonEmptyArrayTypewhen$wasMatched->yes().matchesAllcall sites ofcreateSubjectValueType()to pass$wasMatched.tests/PHPStan/Analyser/nsrt/preg_match_all_shapes.php: updated existing matched-branch expectations tonon-empty-listand added abug14781()regression covering bothPREG_PATTERN_ORDERandPREG_SET_ORDER, including safe$matches[0][0]access.Root cause
RegexArrayShapeMatcherbuilt thepreg_match_all()result lists asArrayType & AccessoryArrayListTypewithout recording non-emptiness, even when the call was specified as a successful match ($wasMatched->yes()). The fix addsNonEmptyArrayTypeto the intersection at each of the three places that build a list forpreg_match_all():PREG_PATTERN_ORDERsubject listPREG_PATTERN_ORDERcapturing-group listsPREG_SET_ORDERouter listpreg_match()(single match) and the non-matchesAlltagged-union branches were probed and are unaffected — they do not produce lists, so there is no non-emptiness to preserve. Non-constant patterns fall back to the genericparam-outtypes and are likewise unaffected.Test
bug14781()intests/PHPStan/Analyser/nsrt/preg_match_all_shapes.phpassertsnon-empty-listfor bothPREG_PATTERN_ORDERandPREG_SET_ORDERinsideif (preg_match_all(...)), and that$matches[0][0]/$setMatches[0]no longer trigger possibly-missing-offset errors.listtonon-empty-list. Assertions outside a success check (plain calls,=== 0guards) correctly remainlist.Fixes phpstan/phpstan#14781