Skip to content

Commit 4cb02d1

Browse files
committed
Fix fetching class constants on object instances
1 parent a9f371e commit 4cb02d1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,7 @@ private function resolveType(Expr $node): Type
18771877
return new ErrorType();
18781878
} elseif ($node instanceof Node\Expr\ClassConstFetch && $node->name instanceof Node\Identifier) {
18791879
$constantName = $node->name->name;
1880+
$isObject = false;
18801881
if ($node->class instanceof Name) {
18811882
$constantClass = (string) $node->class;
18821883
$constantClassType = new ObjectType($constantClass);
@@ -1893,6 +1894,7 @@ private function resolveType(Expr $node): Type
18931894
}
18941895

18951896
$namesToResolve[] = 'static';
1897+
$isObject = true;
18961898
}
18971899
}
18981900
if (in_array(strtolower($constantClass), $namesToResolve, true)) {
@@ -1908,6 +1910,7 @@ private function resolveType(Expr $node): Type
19081910
}
19091911
} else {
19101912
$constantClassType = $this->getType($node->class);
1913+
$isObject = true;
19111914
}
19121915

19131916
$referencedClasses = TypeUtils::getDirectClassNames($constantClassType);
@@ -1936,8 +1939,7 @@ private function resolveType(Expr $node): Type
19361939
$constantReflection = $constantClassReflection->getConstant($constantName);
19371940
if (
19381941
$constantReflection instanceof ClassConstantReflection
1939-
&& $node->class instanceof Name
1940-
&& strtolower((string) $node->class) === 'static'
1942+
&& $isObject
19411943
&& !$constantClassReflection->isFinal()
19421944
&& !$constantReflection->hasPhpDocType()
19431945
) {

tests/PHPStan/Analyser/data/class-constant-types.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ public function doFoo()
1919
{
2020
assertType('1', self::NO_TYPE);
2121
assertType('mixed', static::NO_TYPE);
22+
assertType('mixed', $this::NO_TYPE);
2223

2324
assertType('string', self::TYPE);
2425
assertType('string', static::TYPE);
26+
assertType('string', $this::TYPE);
2527

2628
assertType('string', self::PRIVATE_TYPE);
2729
assertType('string', static::PRIVATE_TYPE);
30+
assertType('string', $this::PRIVATE_TYPE);
2831
}
2932

3033
}
@@ -40,9 +43,11 @@ public function doFoo()
4043
{
4144
assertType('string', self::TYPE);
4245
assertType('string', static::TYPE);
46+
assertType('string', $this::TYPE);
4347

4448
assertType('\'bar\'', self::PRIVATE_TYPE);
4549
assertType('mixed', static::PRIVATE_TYPE);
50+
assertType('mixed', $this::PRIVATE_TYPE);
4651
}
4752

4853
}
@@ -57,6 +62,7 @@ public function doFoo()
5762
{
5863
assertType('int', self::TYPE);
5964
assertType('int', static::TYPE);
65+
assertType('int', $this::TYPE);
6066
}
6167

6268
}
@@ -71,6 +77,7 @@ public function doFoo()
7177
{
7278
assertType('string', self::TYPE);
7379
assertType('string', static::TYPE);
80+
assertType('string', $this::TYPE);
7481
}
7582

7683
}

0 commit comments

Comments
 (0)