Skip to content

Refactor ReplaceFunctionsDynamicReturnTypeExtension #3339

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

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
46 changes: 34 additions & 12 deletions src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ public function getTypeFromFunctionCall(
{
$type = $this->getPreliminarilyResolvedTypeFromFunctionCall($functionReflection, $functionCall, $scope);

$possibleTypes = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();
// resolve conditional return types
$possibleTypes = TypeUtils::resolveLateResolvableTypes($possibleTypes);

if (TypeCombinator::containsNull($possibleTypes)) {
if ($this->canReturnNull($functionReflection, $functionCall, $scope)) {
$type = TypeCombinator::addNull($type);
}

Expand All @@ -73,18 +65,17 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
Scope $scope,
): Type
{
$argumentPosition = self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()];
$subjectArgumentType = $this->getSubjectType($functionReflection, $functionCall, $scope);
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

if (count($functionCall->getArgs()) <= $argumentPosition) {
if ($subjectArgumentType === null) {
return $defaultReturnType;
}

$subjectArgumentType = $scope->getType($functionCall->getArgs()[$argumentPosition]->value);
if ($subjectArgumentType instanceof MixedType) {
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
Expand Down Expand Up @@ -124,4 +115,35 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
return $defaultReturnType;
}

private function getSubjectType(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): ?Type
{
$argumentPosition = self::FUNCTIONS_SUBJECT_POSITION[$functionReflection->getName()];
if (count($functionCall->getArgs()) <= $argumentPosition) {
return null;
}
return $scope->getType($functionCall->getArgs()[$argumentPosition]->value);
}

private function canReturnNull(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): bool
{
$possibleTypes = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

// resolve conditional return types
$possibleTypes = TypeUtils::resolveLateResolvableTypes($possibleTypes);

return TypeCombinator::containsNull($possibleTypes);
}

}
Loading