Skip to content

Commit 231a007

Browse files
clxmstaabondrejmirtes
authored andcommitted
added regression test
1 parent 4b01b52 commit 231a007

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ public function dataFileAsserts(): iterable
10671067
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Properties/data/bug-7839.php');
10681068
yield from $this->gatherAssertTypes(__DIR__ . '/data/self-out.php');
10691069
yield from $this->gatherAssertTypes(__DIR__ . '/data/native-expressions.php');
1070+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Classes/data/bug-5333.php');
10701071
}
10711072

10721073
/**

tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,16 @@ public function testBug6213(): void
346346
$this->analyse([__DIR__ . '/data/bug-6213.php'], []);
347347
}
348348

349+
public function testBug5333(): void
350+
{
351+
$this->checkAlwaysTrueInstanceOf = true;
352+
$this->treatPhpDocTypesAsCertain = false;
353+
$this->analyse([__DIR__ . '/data/bug-5333.php'], [
354+
[
355+
'Instanceof between Bug5333\FinalRoute and Bug5333\FinalRoute will always evaluate to true.',
356+
113,
357+
],
358+
]);
359+
}
360+
349361
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug5333;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Route {}
9+
final class FinalRoute {}
10+
11+
class HelloWorld
12+
{
13+
/**
14+
* @param Route|Route[] $foo
15+
*
16+
* @return Route
17+
**/
18+
public function sayHello($foo): Route
19+
{
20+
if (\is_array($foo)) {
21+
$res = $foo[0];
22+
assertType('Bug5333\Route', $res);
23+
assertNativeType('mixed', $res);
24+
25+
if (!$res instanceof Route) {
26+
throw new \Exception();
27+
}
28+
29+
assertType('array<Bug5333\Route>', $foo);
30+
assertNativeType('array', $foo);
31+
32+
assertType('Bug5333\Route', $res);
33+
assertNativeType('Bug5333\Route', $res);
34+
35+
return $res;
36+
}
37+
38+
return $foo;
39+
}
40+
}
41+
42+
class HelloWorld2
43+
{
44+
/**
45+
* @var Route|callable():Route
46+
**/
47+
private $foo;
48+
49+
/**
50+
* @param Route|callable():Route $foo
51+
**/
52+
public function setFoo($foo): void
53+
{
54+
assertType('Bug5333\Route|(callable(): Bug5333\Route)', $foo);
55+
assertNativeType('mixed', $foo);
56+
57+
$this->foo = $foo;
58+
}
59+
60+
public function getFoo(): Route
61+
{
62+
assertType('Bug5333\Route|(callable(): Bug5333\Route)', $this->foo);
63+
assertNativeType('Bug5333\Route|(callable(): Bug5333\Route)', $this->foo);
64+
65+
if (\is_callable($this->foo)) {
66+
assertType('(Bug5333\Route&callable(): mixed)|(callable(): Bug5333\Route)', $this->foo);
67+
assertNativeType('(Bug5333\Route&callable(): mixed)|(callable(): Bug5333\Route)', $this->foo);
68+
69+
$res = ($this->foo)();
70+
assertType('mixed', $res);
71+
assertNativeType('mixed', $res);
72+
if (!$res instanceof Route) {
73+
throw new \Exception();
74+
}
75+
76+
return $res;
77+
}
78+
79+
return $this->foo;
80+
}
81+
}
82+
83+
class HelloFinalWorld
84+
{
85+
/**
86+
* @var FinalRoute|callable():FinalRoute
87+
**/
88+
private $foo;
89+
90+
/**
91+
* @param FinalRoute|callable():FinalRoute $foo
92+
**/
93+
public function setFoo($foo): void
94+
{
95+
assertType('Bug5333\FinalRoute|(callable(): Bug5333\FinalRoute)', $foo);
96+
assertNativeType('mixed', $foo);
97+
98+
$this->foo = $foo;
99+
}
100+
101+
public function getFoo(): FinalRoute
102+
{
103+
assertType('Bug5333\FinalRoute|(callable(): Bug5333\FinalRoute)', $this->foo);
104+
assertNativeType('Bug5333\FinalRoute|(callable(): Bug5333\FinalRoute)', $this->foo);
105+
106+
if (\is_callable($this->foo)) {
107+
assertType('callable(): Bug5333\FinalRoute', $this->foo);
108+
assertNativeType('callable(): Bug5333\FinalRoute', $this->foo);
109+
110+
$res = ($this->foo)();
111+
assertType('Bug5333\FinalRoute', $res);
112+
assertNativeType('Bug5333\FinalRoute', $res);
113+
if (!$res instanceof FinalRoute) {
114+
throw new \Exception();
115+
}
116+
117+
return $res;
118+
}
119+
120+
return $this->foo;
121+
}
122+
}

0 commit comments

Comments
 (0)