-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RegexArrayShapeMatcher - trailling groups are not optional when PREG_…
…UNMATCHED_AS_NULL
- Loading branch information
Showing
5 changed files
with
73 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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