forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-9519.php
More file actions
87 lines (69 loc) · 1.6 KB
/
bug-9519.php
File metadata and controls
87 lines (69 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php declare(strict_types = 1);
namespace Bug9519;
use function PHPStan\Testing\assertType;
class ClassA {
public function sayHello(): void
{
echo 'Hello';
}
}
class ClassB {
public function sayHello(): void
{
echo 'Hello';
}
}
function test1(mixed $obj): void {
$isA = $obj instanceof ClassA;
$isB = $obj instanceof ClassB;
if ($isA || $isB) {
assertType('Bug9519\ClassA|Bug9519\ClassB', $obj);
}
}
function test2(mixed $obj): void {
// Direct instanceof in condition should work (and already does)
if (($obj instanceof ClassA) || ($obj instanceof ClassB)) {
assertType('Bug9519\ClassA|Bug9519\ClassB', $obj);
}
}
function test3(mixed $obj): void {
$isA = $obj instanceof ClassA;
$isB = $obj instanceof ClassB;
if ($isA) {
assertType('Bug9519\ClassA', $obj);
}
if ($isB) {
assertType('Bug9519\ClassB', $obj);
}
}
interface SomeInterface {
public function test(): void;
}
class ObjectClass {
}
class OtherClass extends ObjectClass {
}
/**
* @template T of object
* @param class-string<T> $class_name
* @return T
*/
function getObject(string $class_name): object {
return new $class_name;
}
function test4(): void {
$obj = getObject(ObjectClass::class);
$is_other = $obj instanceof OtherClass;
$is_interface = $obj instanceof SomeInterface;
if ($is_interface) {
assertType('Bug9519\ObjectClass&Bug9519\SomeInterface', $obj);
}
}
function test5(): void {
$obj = getObject(ObjectClass::class);
$is_interface = $obj instanceof SomeInterface;
$is_other = $obj instanceof OtherClass;
if ($is_interface) {
assertType('Bug9519\ObjectClass&Bug9519\SomeInterface', $obj);
}
}