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

Call all involved class on TypeSpecifierAwareExtension #3882

Draft
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Draft
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
42 changes: 30 additions & 12 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,19 +519,28 @@ public function specifyTypesInCondition(
$methodReflection = $scope->getMethodReflection($methodCalledOnType, $expr->name->name);
if ($methodReflection !== null) {
$referencedClasses = $methodCalledOnType->getObjectClassNames();
Copy link
Contributor

@staabm staabm Mar 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified using getObjectClassReflections instead of getObjectClassNames.
that way you no longer need the reflectionProvider as you already get the ClassReflections returned.

(same for static call)

if (
count($referencedClasses) === 1
&& $this->reflectionProvider->hasClass($referencedClasses[0])
) {
$methodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]);
$specifiedTypes = null;
foreach ($referencedClasses as $referencedClass) {
if (!$this->reflectionProvider->hasClass($referencedClass)) {
continue;
}

$methodClassReflection = $this->reflectionProvider->getClass($referencedClass);
foreach ($this->getMethodTypeSpecifyingExtensionsForClass($methodClassReflection->getName()) as $extension) {
if (!$extension->isMethodSupported($methodReflection, $expr, $context)) {
continue;
}

return $extension->specifyTypes($methodReflection, $expr, $scope, $context);
if ($specifiedTypes !== null) {
$specifiedTypes = $specifiedTypes->unionWith($extension->specifyTypes($methodReflection, $expr, $scope, $context));
} else {
$specifiedTypes = $extension->specifyTypes($methodReflection, $expr, $scope, $context);
}
}
}
if ($specifiedTypes !== null) {
return $specifiedTypes;
}

// lazy create parametersAcceptor, as creation can be expensive
$parametersAcceptor = null;
Expand Down Expand Up @@ -572,19 +581,28 @@ public function specifyTypesInCondition(
$staticMethodReflection = $scope->getMethodReflection($calleeType, $expr->name->name);
if ($staticMethodReflection !== null) {
$referencedClasses = $calleeType->getObjectClassNames();
if (
count($referencedClasses) === 1
&& $this->reflectionProvider->hasClass($referencedClasses[0])
) {
$staticMethodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]);
$specifiedTypes = null;
foreach ($referencedClasses as $referencedClass) {
if (!$this->reflectionProvider->hasClass($referencedClass)) {
continue;
}

$staticMethodClassReflection = $this->reflectionProvider->getClass($referencedClass);
foreach ($this->getStaticMethodTypeSpecifyingExtensionsForClass($staticMethodClassReflection->getName()) as $extension) {
if (!$extension->isStaticMethodSupported($staticMethodReflection, $expr, $context)) {
continue;
}

return $extension->specifyTypes($staticMethodReflection, $expr, $scope, $context);
if ($specifiedTypes !== null) {
$specifiedTypes = $specifiedTypes->unionWith($extension->specifyTypes($staticMethodReflection, $expr, $scope, $context));
} else {
$specifiedTypes = $extension->specifyTypes($staticMethodReflection, $expr, $scope, $context);
}
}
}
if ($specifiedTypes !== null) {
return $specifiedTypes;
}

// lazy create parametersAcceptor, as creation can be expensive
$parametersAcceptor = null;
Expand Down
Loading