Skip to content

Test/type error throw point regressions #1066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-6786.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 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'];
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/union-type-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // lint >= 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) {}
}
}