diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 05a5cd0338..68de2a8a41 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1877,6 +1877,7 @@ private function resolveType(Expr $node): Type return new ErrorType(); } elseif ($node instanceof Node\Expr\ClassConstFetch && $node->name instanceof Node\Identifier) { $constantName = $node->name->name; + $isObject = false; if ($node->class instanceof Name) { $constantClass = (string) $node->class; $constantClassType = new ObjectType($constantClass); @@ -1893,6 +1894,7 @@ private function resolveType(Expr $node): Type } $namesToResolve[] = 'static'; + $isObject = true; } } if (in_array(strtolower($constantClass), $namesToResolve, true)) { @@ -1908,6 +1910,7 @@ private function resolveType(Expr $node): Type } } else { $constantClassType = $this->getType($node->class); + $isObject = true; } $referencedClasses = TypeUtils::getDirectClassNames($constantClassType); @@ -1936,8 +1939,7 @@ private function resolveType(Expr $node): Type $constantReflection = $constantClassReflection->getConstant($constantName); if ( $constantReflection instanceof ClassConstantReflection - && $node->class instanceof Name - && strtolower((string) $node->class) === 'static' + && $isObject && !$constantClassReflection->isFinal() && !$constantReflection->hasPhpDocType() ) { diff --git a/tests/PHPStan/Analyser/data/class-constant-types.php b/tests/PHPStan/Analyser/data/class-constant-types.php index 1109a1441e..c5fd3499b2 100644 --- a/tests/PHPStan/Analyser/data/class-constant-types.php +++ b/tests/PHPStan/Analyser/data/class-constant-types.php @@ -19,12 +19,15 @@ public function doFoo() { assertType('1', self::NO_TYPE); assertType('mixed', static::NO_TYPE); + assertType('mixed', $this::NO_TYPE); assertType('string', self::TYPE); assertType('string', static::TYPE); + assertType('string', $this::TYPE); assertType('string', self::PRIVATE_TYPE); assertType('string', static::PRIVATE_TYPE); + assertType('string', $this::PRIVATE_TYPE); } } @@ -40,9 +43,11 @@ public function doFoo() { assertType('string', self::TYPE); assertType('string', static::TYPE); + assertType('string', $this::TYPE); assertType('\'bar\'', self::PRIVATE_TYPE); assertType('mixed', static::PRIVATE_TYPE); + assertType('mixed', $this::PRIVATE_TYPE); } } @@ -57,6 +62,7 @@ public function doFoo() { assertType('int', self::TYPE); assertType('int', static::TYPE); + assertType('int', $this::TYPE); } } @@ -71,6 +77,7 @@ public function doFoo() { assertType('string', self::TYPE); assertType('string', static::TYPE); + assertType('string', $this::TYPE); } }