-
Notifications
You must be signed in to change notification settings - Fork 523
RegexArrayShapeMatcher - trailling groups are not optional when PREG_UNMATCHED_AS_NULL #3219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
399b888
RegexArrayShapeMatcher - trailling groups are not optional when PREG_…
staabm fd3c3ec
fix test
staabm 4620350
move tests which got php version sensitive
staabm 4adedba
fixed namespace collision
staabm c28d43e
non-optional groups cannot be null when PREG_UNMATCHED_AS_NULL
staabm 5801853
fix comment
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php // lint < 7.4 | ||
|
||
namespace Bug11311Php72; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
// on PHP < 7.4, unmatched-as-null does not return null values; see https://3v4l.org/v3HE4 | ||
|
||
function doFoo(string $s) { | ||
if (1 === preg_match('/(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { | ||
assertType('array{0: string, major: string, 1: string, minor: string, 2: string, patch?: string, 3?: string}', $matches); | ||
} | ||
} | ||
|
||
function doUnmatchedAsNull(string $s): void { | ||
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { | ||
assertType('array{0: string, 1?: string, 2?: string, 3?: string}', $matches); | ||
} | ||
assertType('array{}|array{0: string, 1?: string, 2?: string, 3?: string}', $matches); | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace Bug11311; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function doFoo(string $s) { | ||
if (1 === preg_match('/(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { | ||
|
||
assertType('array{0: string, major: string, 1: string, minor: string, 2: string, patch: string|null, 3: string|null}', $matches); | ||
} | ||
} | ||
|
||
function doUnmatchedAsNull(string $s): void { | ||
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { | ||
assertType('array{string, string|null, string|null, string|null}', $matches); | ||
} | ||
assertType('array{}|array{string, string|null, string|null, string|null}', $matches); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,13 +116,6 @@ function doOffsetCapture(string $s): void { | |
assertType('array{}|array{array{string, int<0, max>}, array{string, int<0, max>}, array{string, int<0, max>}, array{string, int<0, max>}}', $matches); | ||
} | ||
|
||
function doUnmatchedAsNull(string $s): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this pre-existing test no longer has the same expectations across all php versions. |
||
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { | ||
assertType('array{0: string, 1?: string|null, 2?: string|null, 3?: string|null}', $matches); | ||
} | ||
assertType('array{}|array{0: string, 1?: string|null, 2?: string|null, 3?: string|null}', $matches); | ||
} | ||
|
||
function doUnknownFlags(string $s, int $flags): void { | ||
if (preg_match('/(foo)(bar)(baz)/xyz', $s, $matches, $flags)) { | ||
assertType('array<array{string|null, int<-1, max>}|string|null>', $matches); | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another fix this PR provides: a non-optional group cannot be
null
even whenPREG_UNMATCHED_AS_NULL
is given