Skip to content
Draft
Changes from 1 commit
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