Skip to content

Commit dd527ab

Browse files
Add tests
1 parent 2e1a040 commit dd527ab

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12376;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* workaround https://github.com/phpstan/phpstan-src/pull/3853
9+
*
10+
* @template T of object
11+
* @param class-string<T> $class
12+
* @return T
13+
*/
14+
function newNonFinalInstance(string $class): object
15+
{
16+
return new $class();
17+
}
18+
19+
class Base {}
20+
21+
class A extends Base
22+
{
23+
/**
24+
* @template T of object
25+
* @param T $object
26+
* @return (T is static ? T : static)
27+
*
28+
* @phpstan-assert static $object
29+
*/
30+
public static function assertInstanceOf(object $object)
31+
{
32+
if (!$object instanceof static) {
33+
throw new \Exception();
34+
}
35+
36+
return $object;
37+
}
38+
}
39+
40+
class B extends A {}
41+
class C extends Base {}
42+
43+
$o = newNonFinalInstance(\DateTime::class);
44+
$r = A::assertInstanceOf($o);
45+
assertType('*NEVER*', $o);
46+
assertType('Bug12376\A', $r);
47+
48+
$o = newNonFinalInstance(A::class);
49+
$r = A::assertInstanceOf($o);
50+
assertType('Bug12376\A', $o);
51+
assertType('Bug12376\A', $r);
52+
53+
$o = newNonFinalInstance(B::class);
54+
$r = A::assertInstanceOf($o);
55+
assertType('Bug12376\B', $o);
56+
assertType('Bug12376\B', $r);
57+
58+
$o = newNonFinalInstance(C::class);
59+
$r = A::assertInstanceOf($o);
60+
assertType('*NEVER*', $o);
61+
assertType('Bug12376\A', $r);
62+
63+
$o = newNonFinalInstance(A::class);
64+
$r = B::assertInstanceOf($o);
65+
assertType('Bug12376\B', $o);
66+
assertType('Bug12376\B', $r);

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,14 @@ public function testBug12645(): void
11651165
]);
11661166
}
11671167

1168+
public function testBug11289(): void
1169+
{
1170+
$this->checkThisOnly = false;
1171+
$this->checkUnionTypes = true;
1172+
$this->checkDynamicProperties = false;
1173+
$this->analyse([__DIR__ . '/data/bug-11289.php'], []);
1174+
}
1175+
11681176
public function testBug8668(): void
11691177
{
11701178
$this->checkThisOnly = false;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug11289;
6+
7+
abstract class SomeAbstractClass
8+
{
9+
private bool $someValue = true;
10+
11+
/**
12+
* @phpstan-assert-if-true =static $other
13+
*/
14+
public function equals(?self $other): bool
15+
{
16+
return $other instanceof static
17+
&& $this->someValue === $other->someValue;
18+
}
19+
}
20+
21+
class SomeConcreteClass extends SomeAbstractClass
22+
{
23+
public function __construct(
24+
private bool $someOtherValue,
25+
) {}
26+
27+
public function equals(?SomeAbstractClass $other): bool
28+
{
29+
return parent::equals($other)
30+
&& $this->someOtherValue === $other->someOtherValue;
31+
}
32+
}
33+
34+
$a = new SomeConcreteClass(true);
35+
$b = new SomeConcreteClass(false);
36+
37+
var_dump($a->equals($b), $b->equals($b));

0 commit comments

Comments
 (0)