Skip to content

Commit ab74f12

Browse files
committed
test: reproduce #13358 (assert-if-true on $this lost via union)
1 parent fb0e78d commit ab74f12

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Methods;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class AssertIfTrueOnThisTest extends TypeInferenceTestCase
8+
{
9+
10+
public static function dataFileAsserts(): iterable
11+
{
12+
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13358.php');
13+
}
14+
15+
/**
16+
* @dataProvider dataFileAsserts
17+
* @param mixed ...$args
18+
*/
19+
public function testFileAsserts(string $assertType, string $file, ...$args): void
20+
{
21+
$this->assertFileAsserts($assertType, $file, ...$args);
22+
}
23+
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug13358;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @phpstan-sealed SystemActor|AnonymousVisitorActor
9+
*/
10+
abstract class Actor
11+
{
12+
/**
13+
* @phpstan-assert-if-true SystemActor $this
14+
*/
15+
public function isSystem(): bool
16+
{
17+
return $this instanceof SystemActor;
18+
}
19+
20+
/**
21+
* @phpstan-assert-if-true AnonymousVisitorActor $this
22+
*/
23+
public function isAnonymousVisitor(): bool
24+
{
25+
return $this instanceof AnonymousVisitorActor;
26+
}
27+
}
28+
29+
class SystemActor extends Actor {}
30+
class AnonymousVisitorActor extends Actor {}
31+
32+
function test(AnonymousVisitorActor|SystemActor $actor): void {
33+
assertType('Bug13358\AnonymousVisitorActor|Bug13358\SystemActor', $actor);
34+
35+
if ($actor->isSystem()) {
36+
assertType('Bug13358\SystemActor', $actor);
37+
} else {
38+
assertType('Bug13358\AnonymousVisitorActor', $actor);
39+
}
40+
}

0 commit comments

Comments
 (0)