Skip to content

Commit

Permalink
[Core] Improve performance on AstResolver and ClassLikeAstResolver (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 22, 2022
1 parent 84faf29 commit 7f8f9f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR
{
$classReflection = $methodReflection->getDeclaringClass();

if (isset($this->classMethodsByClassAndMethod[$classReflection->getName()][$methodReflection->getName()])) {
return $this->classMethodsByClassAndMethod[$classReflection->getName()][$methodReflection->getName()];
$classLikeName = $classReflection->getName();
$methodName = $methodReflection->getName();

if (isset($this->classMethodsByClassAndMethod[$classLikeName][$methodName])) {
return $this->classMethodsByClassAndMethod[$classLikeName][$methodName];
}

// saved as null data
if (array_key_exists($classLikeName, $this->classMethodsByClassAndMethod)
&& array_key_exists($methodName, $this->classMethodsByClassAndMethod[$classLikeName])) {
return null;
}

$fileName = $classReflection->getFileName();
Expand All @@ -108,25 +117,23 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR

/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($nodes, ClassLike::class);
$classLikeName = $classReflection->getName();
$methodReflectionName = $methodReflection->getName();

foreach ($classLikes as $classLike) {
if (! $this->nodeNameResolver->isName($classLike, $classLikeName)) {
continue;
}

$classMethod = $classLike->getMethod($methodReflectionName);
$classMethod = $classLike->getMethod($methodName);
if (! $classMethod instanceof ClassMethod) {
continue;
}

$this->classMethodsByClassAndMethod[$classLikeName][$methodReflectionName] = $classMethod;
$this->classMethodsByClassAndMethod[$classLikeName][$methodName] = $classMethod;
return $classMethod;
}

// avoids looking for a class in a file where is not present
$this->classMethodsByClassAndMethod[$classLikeName][$methodReflectionName] = null;
$this->classMethodsByClassAndMethod[$classLikeName][$methodName] = null;
return null;
}

Expand All @@ -143,8 +150,15 @@ public function resolveClassMethodOrFunctionFromCall(

public function resolveFunctionFromFunctionReflection(FunctionReflection $functionReflection): ?Function_
{
if (isset($this->functionsByName[$functionReflection->getName()])) {
return $this->functionsByName[$functionReflection->getName()];
$functionName = $functionReflection->getName();

if (isset($this->functionsByName[$functionName])) {
return $this->functionsByName[$functionName];
}

// saved as null data
if (array_key_exists($functionName, $this->functionsByName)) {
return null;
}

$fileName = $functionReflection->getFileName();
Expand All @@ -160,18 +174,18 @@ public function resolveFunctionFromFunctionReflection(FunctionReflection $functi
/** @var Function_[] $functions */
$functions = $this->betterNodeFinder->findInstanceOf($nodes, Function_::class);
foreach ($functions as $function) {
if (! $this->nodeNameResolver->isName($function, $functionReflection->getName())) {
if (! $this->nodeNameResolver->isName($function, $functionName)) {
continue;
}

// to avoid parsing missing function again
$this->functionsByName[$functionReflection->getName()] = $function;
$this->functionsByName[$functionName] = $function;

return $function;
}

// to avoid parsing missing function again
$this->functionsByName[$functionReflection->getName()] = null;
$this->functionsByName[$functionName] = null;

return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/PhpParser/ClassLikeAstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function resolveClassFromClassReflection(
return $this->classLikesByName[$className];
}

// saved as null data
if (array_key_exists($className, $this->classLikesByName)) {
return null;
}

$fileName = $classReflection->getFileName();

// probably internal class
Expand Down

0 comments on commit 7f8f9f8

Please sign in to comment.