Skip to content

Commit f3313df

Browse files
Resolve static in assert phpdoc
1 parent 98d927a commit f3313df

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

src/Reflection/Dummy/ChangedTypeMethodReflection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct(
2727
private ?array $namedArgumentsVariants,
2828
private ?Type $selfOutType,
2929
private ?Type $throwType,
30+
private Assertions $assertions,
3031
)
3132
{
3233
}
@@ -133,7 +134,7 @@ public function hasSideEffects(): TrinaryLogic
133134

134135
public function getAsserts(): Assertions
135136
{
136-
return $this->reflection->getAsserts();
137+
return $this->assertions;
137138
}
138139

139140
public function acceptsNamedArguments(): TrinaryLogic

src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
137137
$namedArgumentVariants,
138138
$selfOutType,
139139
$throwType,
140+
$method->getAsserts()->mapTypes($this->transformStaticTypeCallback),
140141
);
141142
}
142143

src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
132132
$namedArgumentsVariants,
133133
$selfOutType,
134134
$throwType,
135+
$method->getAsserts()->mapTypes(fn (Type $type): Type => $this->transformStaticType($type)),
135136
);
136137
}
137138

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,6 +3608,16 @@ public function testBug9141(): void
36083608
$this->analyse([__DIR__ . '/data/bug-9141.php'], []);
36093609
}
36103610

3611+
public function testBug12548(): void
3612+
{
3613+
$this->checkThisOnly = false;
3614+
$this->checkNullables = true;
3615+
$this->checkUnionTypes = true;
3616+
$this->checkExplicitMixed = true;
3617+
3618+
$this->analyse([__DIR__ . '/data/bug-12548.php'], []);
3619+
}
3620+
36113621
public function testBug3589(): void
36123622
{
36133623
$this->checkThisOnly = false;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12548;
4+
5+
class BaseSat extends \stdClass
6+
{
7+
/**
8+
* @template T of object
9+
*
10+
* @param T $object
11+
*
12+
* @phpstan-assert-if-true =static $object
13+
*/
14+
public static function assertInstanceOf(object $object): bool
15+
{
16+
return $object instanceof static;
17+
}
18+
19+
/**
20+
* @template T of object
21+
*
22+
* @param T $object
23+
*
24+
* @return (T is static ? T : static)
25+
*
26+
* @phpstan-assert static $object
27+
*/
28+
public static function assertInstanceOf2(object $object)
29+
{
30+
if (!$object instanceof static) {
31+
throw new \Error('Object is not an instance of static class');
32+
}
33+
34+
return $object;
35+
}
36+
}
37+
38+
class StdSat extends BaseSat
39+
{
40+
public function foo(): void {}
41+
42+
/**
43+
* @template T of object
44+
*
45+
* @param T $object
46+
*
47+
* @return (T is static ? T : static)
48+
*
49+
* @phpstan-assert static $object
50+
*/
51+
public static function assertInstanceOf3(object $object)
52+
{
53+
if (!$object instanceof static) {
54+
throw new \Error('Object is not an instance of static class');
55+
}
56+
57+
return $object;
58+
}
59+
}
60+
61+
class TestCase
62+
{
63+
private function createStdSat(): \stdClass
64+
{
65+
return new StdSat();
66+
}
67+
68+
public function testAssertInstanceOf(): void
69+
{
70+
$o = $this->createStdSat();
71+
$o->foo(); // @phpstan-ignore method.nonObject (EXPECTED)
72+
73+
$o = $this->createStdSat();
74+
if (StdSat::assertInstanceOf($o)) {
75+
$o->foo();
76+
}
77+
78+
$o = $this->createStdSat();
79+
StdSat::assertInstanceOf2($o);
80+
$o->foo();
81+
82+
$o = $this->createStdSat();
83+
StdSat::assertInstanceOf3($o);
84+
$o->foo();
85+
}
86+
}

0 commit comments

Comments
 (0)