Skip to content

Commit

Permalink
Prevent unnecessary class existence checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Apr 2, 2022
1 parent 90c3f80 commit 4eff9fa
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/PhpDoc/TypeNodeResolver.php
Expand Up @@ -362,7 +362,7 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
return new ErrorType();
}

if ($this->getReflectionProvider()->hasClass($stringName)) {
if (!$this->mightBeConstant($typeNode->name) || $this->getReflectionProvider()->hasClass($stringName)) {
return new ObjectType($stringName);
}

Expand All @@ -374,13 +374,14 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
return new ObjectType($stringName);
}

private function tryResolveConstant(string $name, NameScope $nameScope): ?Type
private function mightBeConstant(string $name): bool
{
if (!$this->mightBeConstant($name)) {
return null;
}
return preg_match('((?:^|\\\\)[A-Z_][A-Z0-9_]*$)', $name) > 0;
}

$names = $this->getPossibleConstNames($name, $nameScope);
private function tryResolveConstant(string $name, NameScope $nameScope): ?Type
{
$names = $this->getPossibleConstantNames($name, $nameScope);

foreach ($names as $name) {
$nameNode = new Name\FullyQualified(explode('\\', $name));
Expand All @@ -396,7 +397,7 @@ private function tryResolveConstant(string $name, NameScope $nameScope): ?Type
/**
* @return non-empty-list<string>
*/
private function getPossibleConstNames(string $name, NameScope $nameScope): array
private function getPossibleConstantNames(string $name, NameScope $nameScope): array
{
if (strpos($name, '\\') === 0) {
return [ltrim($name, '\\')];
Expand All @@ -409,11 +410,6 @@ private function getPossibleConstNames(string $name, NameScope $nameScope): arra
return [$name];
}

private function mightBeConstant(string $name): bool
{
return preg_match('((?:^|\\\\)[A-Z_][A-Z0-9_]*$)', $name) > 0;
}

private function tryResolvePseudoTypeClassType(IdentifierTypeNode $typeNode, NameScope $nameScope): ?Type
{
if ($nameScope->hasUseAlias($typeNode->name)) {
Expand Down

0 comments on commit 4eff9fa

Please sign in to comment.