Skip to content

Commit 5ab5450

Browse files
committed
Fixed resolving static on static
1 parent 3ec2400 commit 5ab5450

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

src/Analyser/MutatingScope.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3617,7 +3617,7 @@ private function methodCallReturnType(Type $calledOnType, Type $typeWithMethod,
36173617
)->getReturnType();
36183618

36193619
if ($methodCall instanceof MethodCall) {
3620-
$calledOnThis = $calledOnType instanceof ThisType && $this->isInClass();
3620+
$calledOnThis = $calledOnType instanceof StaticType && $this->isInClass();
36213621
} else {
36223622
if (!$methodCall->class instanceof Name) {
36233623
$calledOnThis = false;
@@ -3673,7 +3673,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
36733673
}
36743674

36753675
if ($propertyFetch instanceof PropertyFetch) {
3676-
$fetchedOnThis = $fetchedOnType instanceof ThisType && $this->isInClass();
3676+
$fetchedOnThis = $fetchedOnType instanceof StaticType && $this->isInClass();
36773677
} else {
36783678
if (!$propertyFetch->class instanceof Name) {
36793679
$fetchedOnThis = false;

tests/PHPStan/Analyser/data/static-methods.php

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function staticMethod()
2222
public function doFoo()
2323
{
2424
assertType('array<static(StaticMethods\Foo)>', $this->method());
25+
assertType('array<static(StaticMethods\Foo)>', $this->method()[0]->method());
2526
assertType('array<static(StaticMethods\Foo)>', self::staticMethod());
2627
assertType('array<static(StaticMethods\Foo)>', static::staticMethod());
2728
}
@@ -34,6 +35,7 @@ class Bar extends Foo
3435
public function doFoo()
3536
{
3637
assertType('array<static(StaticMethods\Bar)>', $this->method());
38+
assertType('array<static(StaticMethods\Bar)>', $this->method()[0]->method());
3739
assertType('array<static(StaticMethods\Bar)>', self::staticMethod());
3840
assertType('array<static(StaticMethods\Bar)>', static::staticMethod());
3941
}
@@ -43,6 +45,7 @@ public function doFoo()
4345
function (Foo $foo, Bar $bar) {
4446
assertType('array<StaticMethods\Foo>', $foo->method());
4547
assertType('array<StaticMethods\Bar>', $bar->method());
48+
assertType('array<StaticMethods\Bar>', $bar->method()[0]->method());
4649

4750
assertType('array<StaticMethods\Foo>', Foo::staticMethod());
4851
assertType('array<StaticMethods\Bar>', Bar::staticMethod());

tests/PHPStan/Analyser/data/static-properties.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Foo
1616
public function doFoo()
1717
{
1818
assertType('array<static(StaticProperties\Foo)>', $this->prop);
19+
assertType('array<static(StaticProperties\Foo)>', $this->prop[0]->prop);
1920
assertType('array<static(StaticProperties\Foo)>', self::$staticProp);
2021
assertType('array<static(StaticProperties\Foo)>', static::$staticProp);
2122
}
@@ -28,6 +29,7 @@ class Bar extends Foo
2829
public function doFoo()
2930
{
3031
assertType('array<static(StaticProperties\Bar)>', $this->prop);
32+
assertType('array<static(StaticProperties\Bar)>', $this->prop[0]->prop);
3133
assertType('array<static(StaticProperties\Bar)>', self::$staticProp);
3234
assertType('array<static(StaticProperties\Bar)>', static::$staticProp);
3335
}

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,9 @@ public function testBug2676(): void
350350
$this->analyse([__DIR__ . '/data/bug-2676.php'], []);
351351
}
352352

353+
public function testBug2885(): void
354+
{
355+
$this->analyse([__DIR__ . '/data/bug-2885.php'], []);
356+
}
357+
353358
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug2885;
4+
5+
class Test
6+
{
7+
/**
8+
* @return static
9+
*/
10+
function do()
11+
{
12+
return $this->do()->do();
13+
}
14+
}

0 commit comments

Comments
 (0)