Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jul 16, 2024
1 parent 479e5fa commit b874b8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions tests/PHPStan/Analyser/nsrt/bug-11311.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
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: numeric-string, 1: numeric-string, minor: numeric-string, 2: numeric-string, patch: numeric-string|null, 3: numeric-string|null}', $matches);
assertType('array{0: string, major: non-empty-string&numeric-string, 1: non-empty-string&numeric-string, minor: non-empty-string&numeric-string, 2: non-empty-string&numeric-string, patch: (non-empty-string&numeric-string)|null, 3: (non-empty-string&numeric-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{string, non-empty-string|null, non-empty-string|null, non-empty-string|null}', $matches);
}
assertType('array{}|array{string, string|null, string|null, string|null}', $matches);
assertType('array{}|array{string, non-empty-string|null, non-empty-string|null, non-empty-string|null}', $matches);
}

// see https://3v4l.org/VeDob
function unmatchedAsNullWithOptionalGroup(string $s): void {
if (preg_match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
// with PREG_UNMATCHED_AS_NULL the offset 1 will always exist. It is correct that it's nullable because it's optional though
assertType('array{string, string|null}', $matches);
assertType('array{string, non-empty-string|null}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{string, string|null}', $matches);
assertType('array{}|array{string, non-empty-string|null}', $matches);
}

function bug11331a(string $url):void {
Expand All @@ -36,7 +36,7 @@ function bug11331a(string $url):void {
(?<a>.+)
)?
(?<b>.+)}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, a: string|null, 1: string|null, b: string, 2: string}', $matches);
assertType('array{0: string, a: non-empty-string|null, 1: non-empty-string|null, b: non-empty-string, 2: non-empty-string}', $matches);
}
}

Expand All @@ -46,7 +46,7 @@ function bug11331b(string $url):void {
(?<a>.+)
)?
(?<b>.+)?}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, a: string|null, 1: string|null, b: string|null, 2: string|null}', $matches);
assertType('array{0: string, a: non-empty-string|null, 1: non-empty-string|null, b: non-empty-string|null, 2: non-empty-string|null}', $matches);
}
}

Expand All @@ -62,20 +62,20 @@ function bug11331c(string $url):void {
([^/]+?)
(?:\.git|/)?
$}x', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, string|null, string|null, string, string}', $matches);
assertType('array{string, non-empty-string|null, non-empty-string|null, non-empty-string, non-empty-string}', $matches);
}
}

class UnmatchedAsNullWithTopLevelAlternation {
function doFoo(string $s): void {
if (preg_match('/Price: (?:(£)|(€))\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, string|null, string|null}', $matches); // could be array{0: string, 1: null, 2: string}|array{0: string, 1: string, 2: null}
assertType('array{string, non-empty-string|null, non-empty-string|null}', $matches); // could be array{0: string, 1: null, 2: string}|array{0: string, 1: string, 2: null}
}
}

function doBar(string $s): void {
if (preg_match('/Price: (?:(£)|(€))?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, string|null, string|null}', $matches);
assertType('array{string, non-empty-string|null, non-empty-string|null}', $matches);
}
}
}
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function doFoo2(string $row): void
return;
}

assertType('array{0: string, 1: string, branchCode: numeric-string, 2: numeric-string, accountNumber: numeric-string, 3: numeric-string, bankCode: numeric-string, 4: numeric-string}', $matches);
assertType('array{0: string, 1: non-empty-string&numeric-string, branchCode: non-empty-string&numeric-string, 2: non-empty-string&numeric-string, accountNumber: non-empty-string&numeric-string, 3: non-empty-string&numeric-string, bankCode: non-empty-string&numeric-string, 4: non-empty-string&numeric-string}', $matches);
}

function doFoo3(string $row): void
Expand Down Expand Up @@ -289,7 +289,7 @@ function (string $size): void {
if (preg_match('~^a\.b(c(\d+)?)d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, 1: string, 2?: numeric-string}', $matches);
assertType('array{0: string, 1: non-empty-string, 2?: non-empty-string&numeric-string}', $matches);
};

function (string $size): void {
Expand All @@ -303,7 +303,7 @@ function (string $size): void {
if (preg_match('~^a\.b(c(\d+))d~', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, string, numeric-string}', $matches);
assertType('array{string, non-empty-string, non-empty-string&numeric-string}', $matches);
};

function (string $size): void {
Expand Down Expand Up @@ -376,7 +376,7 @@ function bug11323a(string $s): void
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, currency: string, 1: string}', $matches);
assertType('array{}|array{0: string, currency: non-empty-string, 1: non-empty-string}', $matches);
}

function bug11323b(string $s): void
Expand Down Expand Up @@ -447,21 +447,21 @@ function (string $size): void {
if (preg_match('/ab(\d){2,4}xx([0-9])?e?/', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, 1: non-empty-string&numeric-string, 2?: non-empty-string}', $matches);
assertType('array{0: string, 1: non-empty-string&numeric-string, 2?: non-empty-string&numeric-string}', $matches);
};

function (string $size): void {
if (preg_match('/a(\dAB){2}b(\d){2,4}([1-5])([1-5a-z])e?/', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{string, string, numeric-string, numeric-string, string}', $matches);
assertType('array{string, non-empty-string, non-empty-string&numeric-string, non-empty-string&numeric-string, non-empty-string}', $matches);
};

function (string $size): void {
if (preg_match('/ab(ab(\d)){2,4}xx([0-9][a-c])?e?/', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, 1: string, 2: numeric-string, 3?: string}', $matches);
assertType('array{0: string, 1: non-empty-string, 2: non-empty-string&numeric-string, 3?: non-empty-string}', $matches);
};

function (string $size): void {
Expand All @@ -475,7 +475,7 @@ function (string $size): void {
if (preg_match('/ab(?P<num>\d+)e?/', $size, $matches) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: string, num: numeric-string, 1: numeric-string}', $matches);
assertType('array{0: string, num: non-empty-string&numeric-string, 1: non-empty-string&numeric-string}', $matches);
};

function (string $size): void {
Expand Down

0 comments on commit b874b8d

Please sign in to comment.