Skip to content

Commit

Permalink
Fix fetching class constants on object instances
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 17, 2021
1 parent a9f371e commit 4cb02d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -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);
Expand All @@ -1893,6 +1894,7 @@ private function resolveType(Expr $node): Type
}

$namesToResolve[] = 'static';
$isObject = true;
}
}
if (in_array(strtolower($constantClass), $namesToResolve, true)) {
Expand All @@ -1908,6 +1910,7 @@ private function resolveType(Expr $node): Type
}
} else {
$constantClassType = $this->getType($node->class);
$isObject = true;
}

$referencedClasses = TypeUtils::getDirectClassNames($constantClassType);
Expand Down Expand Up @@ -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()
) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/class-constant-types.php
Expand Up @@ -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);
}

}
Expand All @@ -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);
}

}
Expand All @@ -57,6 +62,7 @@ public function doFoo()
{
assertType('int', self::TYPE);
assertType('int', static::TYPE);
assertType('int', $this::TYPE);
}

}
Expand All @@ -71,6 +77,7 @@ public function doFoo()
{
assertType('string', self::TYPE);
assertType('string', static::TYPE);
assertType('string', $this::TYPE);
}

}

0 comments on commit 4cb02d1

Please sign in to comment.