Skip to content

Commit

Permalink
Understand constant strings in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Aug 4, 2024
1 parent 30650e6 commit 32412c9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.11.9"
"phpstan/phpstan": "^1.11.10"
},
"conflict": {
"nette/application": "<2.3.0",
Expand Down
10 changes: 5 additions & 5 deletions tests/Type/Nette/data/strings-match.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ function (string $s): void {
assertType('array{string, string}|null', $result);

$result = Strings::match($s, '/(foo)(bar)(baz)/', PREG_OFFSET_CAPTURE);
assertType('array{array{string, int<0, max>}, array{non-empty-string, int<0, max>}, array{non-empty-string, int<0, max>}, array{non-empty-string, int<0, max>}}|null', $result);
assertType("array{array{string, int<0, max>}, array{'foo', int<0, max>}, array{'bar', int<0, max>}, array{'baz', int<0, max>}}|null", $result);

$result = Strings::match($s, '/(foo)(bar)(baz)/');
assertType('array{string, non-empty-string, non-empty-string, non-empty-string}|null', $result);
assertType("array{string, 'foo', 'bar', 'baz'}|null", $result);

$result = Strings::match($s, '/(foo)(bar)'. preg_quote($s) .'(baz)/');
assertType('array{string, non-empty-string, non-empty-string, non-empty-string}|null', $result);
assertType("array{string, 'foo', 'bar', 'baz'}|null", $result);
};

function (string $s): void {
$result = Strings::matchAll($s, '/ab(?P<num>\d+)(?P<suffix>ab)?/', PREG_SET_ORDER);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: non-empty-string, 2?: non-empty-string}>", $result);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: 'ab', 2?: 'ab'}>", $result);
};

function (string $s): void {
$result = Strings::matchAll($s, '/ab(?P<num>\d+)(?P<suffix>ab)?/', PREG_PATTERN_ORDER);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<string>, 2: list<string>}", $result);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<''|'ab'>, 2: list<''|'ab'>}", $result);
};

0 comments on commit 32412c9

Please sign in to comment.