()
| 352 | class GuardClauseDemo |
| 353 | { |
| 354 | public function demo(): void |
| 355 | { |
| 356 | $subject = pickRockOrBanana(); // Rock|Banana |
| 357 | if (!$subject instanceof Banana) { |
| 358 | return; // early return — guard clause |
| 359 | } |
| 360 | $subject->peel(); // narrowed to Banana after guard |
| 361 | |
| 362 | $candidate = pickRockOrBanana(); // Rock|Banana |
| 363 | if ($candidate instanceof Rock) { |
| 364 | throw new Exception('no rocks'); // early throw — guard clause |
| 365 | } |
| 366 | $candidate->peel(); // narrowed to Banana (Rock excluded) |
| 367 | |
| 368 | $unknown = getUnknownValue(); |
| 369 | if (!$unknown instanceof Rock) return; // single-statement guard (no braces) |
| 370 | $unknown->crush(); // narrowed to Rock |
| 371 | } |
| 372 | |
| 373 | /** Positive instanceof + early return on a mixed parameter. */ |
| 374 | public function mixedGuard(mixed $value): void |
nothing calls this directly
no test coverage detected