|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PregMatchShapes; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +function doMatch(string $s): void { |
| 8 | + if (preg_match('/Price: (£|€)\d+/', $s, $matches)) { |
| 9 | + assertType('array{0: string, 1?: string}', $matches); |
| 10 | + } |
| 11 | + assertType('array<string>', $matches); |
| 12 | + |
| 13 | + if (preg_match('/Price: (£|€)(\d+)/i', $s, $matches)) { |
| 14 | + assertType('array{0: string, 1?: string, 2?: string}', $matches); |
| 15 | + } |
| 16 | + assertType('array<string>', $matches); |
| 17 | + |
| 18 | + if (preg_match('/(a)(b)*(c)(d)*/', $s, $matches)) { |
| 19 | + assertType('array{0: string, 1?: string, 2?: string, 3?: string, 4?: string}', $matches); |
| 20 | + } |
| 21 | + assertType('array<string>', $matches); |
| 22 | +} |
| 23 | + |
| 24 | +function doNonCapturingGroup(string $s): void { |
| 25 | + if (preg_match('/Price: (?:£|€)(\d+)/', $s, $matches)) { |
| 26 | + assertType('array{0: string, 1?: string}', $matches); |
| 27 | + } |
| 28 | + assertType('array<string>', $matches); |
| 29 | +} |
| 30 | + |
| 31 | +function doNamedSubpattern(string $s): void { |
| 32 | + if (preg_match('/\w-(?P<num>\d+)-(\w)/', $s, $matches)) { |
| 33 | + assertType('array{0: string, num?: string, 1?: string, 2?: string}', $matches); |
| 34 | + } |
| 35 | + assertType('array<string>', $matches); |
| 36 | +} |
| 37 | + |
| 38 | +function doOffsetCapture(string $s): void { |
| 39 | + if (preg_match('/(foo)(bar)(baz)/', $s, $matches, PREG_OFFSET_CAPTURE)) { |
| 40 | + assertType('array{0: array{string, int<0, max>}, 1?: array{string, int<0, max>}, 2?: array{string, int<0, max>}, 3?: array{string, int<0, max>}}', $matches); |
| 41 | + } |
| 42 | + assertType('array<array{string, int<-1, max>}>', $matches); |
| 43 | +} |
| 44 | + |
| 45 | +function doUnmatchedAsNull(string $s): void { |
| 46 | + if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) { |
| 47 | + assertType('array{0: string|null, 1?: string|null, 2?: string|null, 3?: string|null}', $matches); |
| 48 | + } |
| 49 | + assertType('array<string|null>', $matches); |
| 50 | +} |
| 51 | + |
| 52 | +function doOffsetCaptureWithUnmatchedNull(string $s): void { |
| 53 | + // see https://3v4l.org/07rBO#v8.2.9 |
| 54 | + if (preg_match('/(foo)(bar)(baz)/', $s, $matches, PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL)) { |
| 55 | + assertType('array{0: array{null, -1}|array{string, int<0, max>}, 1?: array{null, -1}|array{string, int<0, max>}, 2?: array{null, -1}|array{string, int<0, max>}, 3?: array{null, -1}|array{string, int<0, max>}}', $matches); |
| 56 | + } |
| 57 | + assertType('array<array{null, -1}|array{string, int<0, max>}>', $matches); |
| 58 | +} |
| 59 | + |
| 60 | +function doUnknownFlags(string $s, int $flags): void { |
| 61 | + if (preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, $flags)) { |
| 62 | + assertType('array<array{string|null, int<-1, max>}|string|null>', $matches); |
| 63 | + } |
| 64 | + assertType('array<array{string|null, int<-1, max>}|string|null>', $matches); |
| 65 | +} |
0 commit comments