Skip to content

Commit e82ccf4

Browse files
phpstan-botclaude
andcommitted
Add regression tests for phpstan/phpstan#13591, #10422, #10055
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b0ee6d3 commit e82ccf4

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10055;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param 'value1'|'value2'|'value3' $param1
9+
* @param ($param1 is 'value3' ? bool : int) $param2
10+
*/
11+
function test(string $param1, $param2): void
12+
{
13+
if ($param1 === 'value1') {
14+
assertType('int', $param2);
15+
}
16+
if ($param1 === 'value2') {
17+
assertType('int', $param2);
18+
}
19+
if ($param1 === 'value3') {
20+
assertType('bool', $param2);
21+
}
22+
23+
match ($param1) {
24+
'value1' => assertType('int', $param2),
25+
'value2' => assertType('int', $param2),
26+
'value3' => assertType('bool', $param2),
27+
};
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10422;
4+
5+
use stdClass;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Foo
9+
{
10+
11+
public function other(): bool
12+
{
13+
return true;
14+
}
15+
16+
public function test(): void
17+
{
18+
}
19+
20+
}
21+
22+
function createOrNotObject(): ?Foo
23+
{
24+
return new Foo();
25+
}
26+
27+
function testSimple(): void
28+
{
29+
$test = createOrNotObject();
30+
31+
$error = '';
32+
if (!$test) {
33+
$error = 'yes';
34+
}
35+
if ($error) {
36+
return;
37+
}
38+
assertType(Foo::class, $test);
39+
}
40+
41+
function testWithElseIf(): void
42+
{
43+
$test = createOrNotObject();
44+
45+
$error = '';
46+
if (!$test) {
47+
$error = 'yes';
48+
} else if ($test->other()) {
49+
$error = 'yes';
50+
}
51+
if ($error) {
52+
return;
53+
}
54+
assertType(Foo::class . '|null', $test); // Should be Foo, see https://github.com/phpstan/phpstan/issues/10422
55+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13591;
4+
5+
use InvalidArgumentException;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
11+
public function test(string $action, ?int $hotelId): void
12+
{
13+
if ($action === 'get_rooms' && $hotelId === null) {
14+
throw new InvalidArgumentException('Hotel ID is required');
15+
}
16+
17+
if ($action === 'get_rooms') {
18+
assertType('int', $hotelId);
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)