diff --git a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php index 38efef3f77..80fe132f33 100644 --- a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php @@ -289,4 +289,31 @@ public function testBug6791(): void ]); } + public function testBug6786(): void + { + if (PHP_VERSION_ID < 70400) { + self::markTestSkipped('Test requires PHP 7.4.'); + } + + $this->analyse([__DIR__ . '/data/bug-6786.php'], []); + } + + public function testUnionTypeError(): void + { + if (PHP_VERSION_ID < 80000) { + self::markTestSkipped('Test requires PHP 8.0.'); + } + + $this->analyse([__DIR__ . '/data/union-type-error.php'], [ + [ + 'Dead catch - TypeError is never thrown in the try block.', + 14, + ], + [ + 'Dead catch - TypeError is never thrown in the try block.', + 22, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-6786.php b/tests/PHPStan/Rules/Exceptions/data/bug-6786.php new file mode 100644 index 0000000000..9cd084c93d --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-6786.php @@ -0,0 +1,24 @@ += 7.4 + +declare(strict_types = 1); + +namespace Bug6786; + +class HelloWorld +{ + protected int $id; + protected string $code; + protected bool $suggest; + + /** + * @param array|mixed[] $row + * @return void + */ + protected function mapping(array $row): void + { + $this->id = (int) $row['id']; + $this->code = $row['code']; + $this->suggest = (bool) $row['suggest']; + } + +} diff --git a/tests/PHPStan/Rules/Exceptions/data/union-type-error.php b/tests/PHPStan/Rules/Exceptions/data/union-type-error.php new file mode 100644 index 0000000000..1c16fec53d --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/union-type-error.php @@ -0,0 +1,29 @@ += 8.0 + +declare(strict_types = 1); + +namespace UnionTypeError; + +class Foo { + public string|int $stringOrInt; + public string|array $stringOrArray; + + public function bar() { + try { + $this->stringOrInt = ""; + } catch (\TypeError $e) {} + + try { + $this->stringOrInt = true; + } catch (\TypeError $e) {} + + try { + $this->stringOrArray = []; + } catch (\TypeError $e) {} + + try { + $this->stringOrInt = $this->stringOrArray; + } catch (\TypeError $e) {} + } +} +