|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug13566; |
| 6 | + |
| 7 | +use LogicException; |
| 8 | + |
| 9 | +class ReturnViaBool |
| 10 | +{ |
| 11 | + /** @return ($exit is true ? never : void) */ |
| 12 | + public static function notFound(bool $exit = true): void |
| 13 | + { |
| 14 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 15 | + |
| 16 | + if ($exit) { |
| 17 | + echo '404 Not Found'; |
| 18 | + exit; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public function test(): void |
| 23 | + { |
| 24 | + // send 404 header without exiting |
| 25 | + self::notFound(false); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class ReturnViaInt |
| 30 | +{ |
| 31 | + /** @return ($exit is 1 ? never : void) */ |
| 32 | + public static function notFound(int $exit = 1): void |
| 33 | + { |
| 34 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 35 | + |
| 36 | + if ($exit == 1) { |
| 37 | + echo '404 Not Found'; |
| 38 | + exit; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public function test(): void |
| 43 | + { |
| 44 | + // send 404 header |
| 45 | + self::notFound(0); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class ReturnViaString |
| 50 | +{ |
| 51 | + /** @return ($exit is '1' ? never : void) */ |
| 52 | + public static function notFound(string $exit = '1'): void |
| 53 | + { |
| 54 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 55 | + |
| 56 | + if ($exit == '1') { |
| 57 | + echo '404 Not Found'; |
| 58 | + exit; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public function test(): void |
| 63 | + { |
| 64 | + // send 404 header |
| 65 | + self::notFound('0'); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class ReturnViaIntNonNative |
| 70 | +{ |
| 71 | + /** @return ($exit is 1 ? never : void) */ |
| 72 | + public static function notFound(int $exit = 1) |
| 73 | + { |
| 74 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 75 | + |
| 76 | + if ($exit == 1) { |
| 77 | + echo '404 Not Found'; |
| 78 | + exit; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function test(): void |
| 83 | + { |
| 84 | + // send 404 header |
| 85 | + self::notFound(0); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class ReturnsMaybeNever |
| 90 | +{ |
| 91 | + /** @return ($exit is true ? never : 1) */ |
| 92 | + public static function notFound(bool $exit = true) |
| 93 | + { |
| 94 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 95 | + |
| 96 | + if ($exit) { |
| 97 | + echo '404 Not Found'; |
| 98 | + exit; |
| 99 | + } |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + public function test(): void |
| 104 | + { |
| 105 | + // send 404 header |
| 106 | + self::notFound(false); |
| 107 | + |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +class ReturnsMaybeVoid |
| 112 | +{ |
| 113 | + /** @return ($exit is true ? void : 1) */ |
| 114 | + public static function notFound(bool $exit = true) |
| 115 | + { |
| 116 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 117 | + |
| 118 | + if ($exit) { |
| 119 | + echo '404 Not Found'; |
| 120 | + return; |
| 121 | + } |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + public function test(): void |
| 126 | + { |
| 127 | + // send 404 header |
| 128 | + self::notFound(false); |
| 129 | + |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +class ReturnsWithInstanceMethod |
| 136 | +{ |
| 137 | + /** @return ($exit is true ? never-return : void) */ |
| 138 | + public function notFound(bool $exit = true): void |
| 139 | + { |
| 140 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 141 | + |
| 142 | + if ($exit) { |
| 143 | + echo '404 Not Found'; |
| 144 | + exit; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public function test(): void |
| 150 | + { |
| 151 | + // send 404 header |
| 152 | + $this->notFound(false); |
| 153 | + |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** @return ($exit is true ? never-return : void) */ |
| 158 | +function notFound(bool $exit = true): void |
| 159 | +{ |
| 160 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 161 | + |
| 162 | + if ($exit) { |
| 163 | + echo '404 Not Found'; |
| 164 | + exit; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +function test(): void |
| 169 | +{ |
| 170 | + // send 404 header |
| 171 | + notFound(false); |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +class ReturnViaUnion |
| 176 | +{ |
| 177 | + /** |
| 178 | + * @return ($exit is int ? void : never) |
| 179 | + */ |
| 180 | + public static function notFound(int|string $exit = 'hello world'): void |
| 181 | + { |
| 182 | + header('HTTP/1.1 404 Not Found', true, 404); |
| 183 | + |
| 184 | + if (!is_int($exit)) { |
| 185 | + echo '404 Not Found'; |
| 186 | + exit; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public function test(): void |
| 191 | + { |
| 192 | + // send 404 header |
| 193 | + self::notFound(0); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +$x = 123; |
| 199 | +if (is_numeric($x)) { |
| 200 | +} |
| 201 | + |
| 202 | +function doFoo($mixed) { |
| 203 | + assertIsInt($mixed); |
| 204 | + assertIsInt($mixed); |
| 205 | +} |
| 206 | + |
| 207 | +/** @phpstan-assert int $i */ |
| 208 | +function assertIsInt($i):void { |
| 209 | + if (!is_int($i)) { |
| 210 | + throw new \LogicException(); |
| 211 | + } |
| 212 | +} |
0 commit comments