Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Improve performance on AstResolver and ClassLikeAstResolver #3234

Merged
merged 5 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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