|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug14575; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +function doFoo(string $string): void { |
| 8 | + // Anchors in alternation should not make the match non-empty |
| 9 | + if (preg_match('(foo|$)', $string, $match)) { |
| 10 | + assertType('array{string}', $match); |
| 11 | + } |
| 12 | + |
| 13 | + if (preg_match('(^|foo)', $string, $match)) { |
| 14 | + assertType('array{string}', $match); |
| 15 | + } |
| 16 | + |
| 17 | + if (preg_match('(\b|foo)', $string, $match)) { |
| 18 | + assertType('array{string}', $match); |
| 19 | + } |
| 20 | + |
| 21 | + if (preg_match('/foo|$/', $string, $match)) { |
| 22 | + assertType('array{string}', $match); |
| 23 | + } |
| 24 | + |
| 25 | + if (preg_match('/^|bar/', $string, $match)) { |
| 26 | + assertType('array{string}', $match); |
| 27 | + } |
| 28 | + |
| 29 | + // Anchor in alternation within capturing group |
| 30 | + if (preg_match('/(foo|$)/', $string, $match)) { |
| 31 | + assertType('array{string, string}', $match); |
| 32 | + } |
| 33 | + |
| 34 | + if (preg_match('/(^|bar)/', $string, $match)) { |
| 35 | + assertType('array{string, string}', $match); |
| 36 | + } |
| 37 | + |
| 38 | + // Anchor in alternation does not affect parent concatenation |
| 39 | + if (preg_match('/^abc(def|$)/', $string, $match)) { |
| 40 | + assertType("array{non-falsy-string, string}", $match); |
| 41 | + } |
| 42 | + |
| 43 | + // All non-empty alternatives should still produce non-empty/non-falsy |
| 44 | + if (preg_match('/foo|bar/', $string, $match)) { |
| 45 | + assertType('array{non-falsy-string}', $match); |
| 46 | + } |
| 47 | + |
| 48 | + if (preg_match('/foo/', $string, $match)) { |
| 49 | + assertType('array{non-falsy-string}', $match); |
| 50 | + } |
| 51 | + |
| 52 | + // Anchor alone |
| 53 | + if (preg_match('/^$/', $string, $match)) { |
| 54 | + assertType('array{string}', $match); |
| 55 | + } |
| 56 | + |
| 57 | + // Three-way alternation with anchor |
| 58 | + if (preg_match('/foo|bar|$/', $string, $match)) { |
| 59 | + assertType('array{string}', $match); |
| 60 | + } |
| 61 | + |
| 62 | + // All alternatives are single chars (non-falsy not determinable from single tokens) |
| 63 | + if (preg_match('/a|b/', $string, $match)) { |
| 64 | + assertType('array{non-empty-string}', $match); |
| 65 | + } |
| 66 | + |
| 67 | + // Empty alternation branches |
| 68 | + if (preg_match('/(|)/', $string, $match)) { |
| 69 | + assertType("array{string, ''}", $match); |
| 70 | + } |
| 71 | + |
| 72 | + if (preg_match('/(foo|)/', $string, $match)) { |
| 73 | + assertType("array{string, ''|'foo'}", $match); |
| 74 | + } |
| 75 | + |
| 76 | + if (preg_match('/(|bar)/', $string, $match)) { |
| 77 | + assertType("array{string, ''|'bar'}", $match); |
| 78 | + } |
| 79 | + |
| 80 | + if (preg_match('/|/', $string, $match)) { |
| 81 | + assertType('array{string}', $match); |
| 82 | + } |
| 83 | + |
| 84 | + if (preg_match('/foo|/', $string, $match)) { |
| 85 | + assertType('array{string}', $match); |
| 86 | + } |
| 87 | +} |
0 commit comments