Repository navigation
Keep optional groups optional within a matched regex alternation branch - #6596
Merged
Merged
Conversation
When building the tagged union for a single top-level alternation, the groups of the matched branch were forced non-optional, which also dropped their own optional quantifiers, e.g. `(?:-(?P<bar>\d+))?`. Such a group is now only forced out of the alternation's optionality. With PREG_UNMATCHED_AS_NULL on PHP 7.2 and 7.3, an unmatched group followed by a matched one is reported as null, not as an empty string. Only the trailing unmatched groups are omitted there. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KjXHtRyzsYF9xktijyaRBr
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#15297
The reported errors show up when the analysed PHP version is below 7.4 (e.g. PHP 7.2/7.3 on the playground). Two inaccuracies in
RegexArrayShapeMatchercaused them:forceNonOptional()-ed, which also dropped its own optional quantifier, e.g.(?:-(?P<bar1>[0-9]+))?. PHP omits such a trailing unmatched group (withoutPREG_UNMATCHED_AS_NULL, or on 7.2/7.3), but PHPStan inferredbar1: numeric-stringas always present. The matched branch's groups are now only forced out of the alternation's optionality: they remain optional when they, or a parent within the same branch, are optionally quantified.PREG_UNMATCHED_AS_NULLon PHP 7.2 and 7.3 was modelled as no flag at all, i.e.''for unmatched groups. Those versions already report an unmatched group followed by a matched one asnull; only trailing unmatched groups are omitted. Verified against php:7.2-cli and php:7.3-cli.On PHP 7.3 the reproducer now reports one remaining error which is a true positive: when
foo1isnullthe second branch matched, so$matches["foo2"]always exists and?? ""is dead. PHP 7.4+ was and remains error-free.Tests:
nsrt/bug-15297.php– flag-less shapes (also(?:(a))?(b)|(c), previously inferred as always'a')Bug15297Php73Test– PHP 7.3 shapes withPREG_UNMATCHED_AS_NULLNullCoalesceRulePhp73Test– the playground reproducer verbatim on PHP 7.3🤖 Generated with Claude Code
https://claude.ai/code/session_01KjXHtRyzsYF9xktijyaRBr