|
| 1 | +<?php declare(strict_types = 0); |
| 2 | + |
| 3 | +namespace Bug9146NonStrict; |
| 4 | + |
| 5 | +final class HelloWorld |
| 6 | +{ |
| 7 | + public int $number; |
| 8 | + public function __construct(mixed $number) |
| 9 | + { |
| 10 | + try { |
| 11 | + $this->number = $number; |
| 12 | + } catch (\TypeError $e) { |
| 13 | + throw new \UnexpectedValueException(); |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +final class HelloWorld2 |
| 19 | +{ |
| 20 | + public string $name; |
| 21 | + public function setName(mixed $value): void |
| 22 | + { |
| 23 | + try { |
| 24 | + $this->name = $value; |
| 25 | + } catch (\TypeError $e) { |
| 26 | + throw new \InvalidArgumentException('Expected string'); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +final class HelloWorld3 |
| 32 | +{ |
| 33 | + public float $amount; |
| 34 | + public function setAmount(mixed $value): void |
| 35 | + { |
| 36 | + try { |
| 37 | + $this->amount = $value; |
| 38 | + } catch (\TypeError $e) { |
| 39 | + echo "caught"; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Dead catch: int assigned to ?float, PHP coerces int to float without TypeError |
| 45 | +final class FloatNullCoercion |
| 46 | +{ |
| 47 | + public ?float $amount; |
| 48 | + public function setAmount(int $value): void |
| 49 | + { |
| 50 | + try { |
| 51 | + $this->amount = $value; |
| 52 | + } catch (\TypeError $e) { // error: Dead catch - TypeError is never thrown in the try block. |
| 53 | + echo "caught"; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Not dead: int|string assigned to int, string part could throw TypeError |
| 59 | +final class PartialTypeMatch |
| 60 | +{ |
| 61 | + public int $number; |
| 62 | + /** @param int|string $value */ |
| 63 | + public function setNumber($value): void |
| 64 | + { |
| 65 | + try { |
| 66 | + $this->number = $value; |
| 67 | + } catch (\TypeError $e) { |
| 68 | + echo "caught"; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +final class MixedWillNotThrow |
| 74 | +{ |
| 75 | + public mixed $name; |
| 76 | + public function setName(mixed $value): void |
| 77 | + { |
| 78 | + try { |
| 79 | + $this->name = $value; |
| 80 | + } catch (\TypeError $e) { |
| 81 | + throw new \InvalidArgumentException('Expected string'); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +final class IntJustWarnsOnFloat |
| 87 | +{ |
| 88 | + public int $amount; |
| 89 | + public function setAmount(float $value): void |
| 90 | + { |
| 91 | + try { |
| 92 | + $this->amount = $value; |
| 93 | + } catch (\TypeError $e) { |
| 94 | + echo "caught"; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments