Skip to content

Commit d0b1cf6

Browse files
committed
Refactoring
1 parent c10476d commit d0b1cf6

File tree

1 file changed

+35
-84
lines changed

1 file changed

+35
-84
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 35 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,7 @@ static function (): void {
25332533
} elseif ($expr instanceof Expr\Closure) {
25342534
return $this->processClosureNode($stmt, $expr, $scope, $nodeCallback, $context, null);
25352535
} elseif ($expr instanceof Expr\ArrowFunction) {
2536-
return $this->processArrowFunctionNode($stmt, $expr, $scope, $nodeCallback, $context, null);
2536+
return $this->processArrowFunctionNode($stmt, $expr, $scope, $nodeCallback, null);
25372537
} elseif ($expr instanceof ErrorSuppress) {
25382538
$result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context);
25392539
$hasYield = $result->hasYield();
@@ -3425,74 +3425,13 @@ private function processClosureNode(
34253425

34263426
$byRefUses = [];
34273427

3428-
$callableParameters = null;
34293428
$closureCallArgs = $expr->getAttribute(ClosureArgVisitor::ATTRIBUTE_NAME);
3430-
3431-
if ($closureCallArgs !== null) {
3432-
$acceptors = $scope->getType($expr)->getCallableParametersAcceptors($scope);
3433-
if (count($acceptors) === 1) {
3434-
$callableParameters = $acceptors[0]->getParameters();
3435-
3436-
foreach ($callableParameters as $index => $callableParameter) {
3437-
if (!isset($closureCallArgs[$index])) {
3438-
continue;
3439-
}
3440-
3441-
$type = $scope->getType($closureCallArgs[$index]->value);
3442-
$callableParameters[$index] = new NativeParameterReflection(
3443-
$callableParameter->getName(),
3444-
$callableParameter->isOptional(),
3445-
$type,
3446-
$callableParameter->passedByReference(),
3447-
$callableParameter->isVariadic(),
3448-
$callableParameter->getDefaultValue(),
3449-
);
3450-
}
3451-
}
3452-
} elseif ($passedToType !== null && !$passedToType->isCallable()->no()) {
3453-
if ($passedToType instanceof UnionType) {
3454-
$passedToType = TypeCombinator::union(...array_filter(
3455-
$passedToType->getTypes(),
3456-
static fn (Type $type) => $type->isCallable()->yes(),
3457-
));
3458-
}
3459-
3460-
$acceptors = $passedToType->getCallableParametersAcceptors($scope);
3461-
if (count($acceptors) > 0) {
3462-
foreach ($acceptors as $acceptor) {
3463-
if ($callableParameters === null) {
3464-
$callableParameters = array_map(static fn (ParameterReflection $callableParameter) => new NativeParameterReflection(
3465-
$callableParameter->getName(),
3466-
$callableParameter->isOptional(),
3467-
$callableParameter->getType(),
3468-
$callableParameter->passedByReference(),
3469-
$callableParameter->isVariadic(),
3470-
$callableParameter->getDefaultValue(),
3471-
), $acceptor->getParameters());
3472-
continue;
3473-
}
3474-
3475-
$newParameters = [];
3476-
foreach ($acceptor->getParameters() as $i => $callableParameter) {
3477-
if (!array_key_exists($i, $callableParameters)) {
3478-
$newParameters[] = $callableParameter;
3479-
continue;
3480-
}
3481-
3482-
$newParameters[] = $callableParameters[$i]->union(new NativeParameterReflection(
3483-
$callableParameter->getName(),
3484-
$callableParameter->isOptional(),
3485-
$callableParameter->getType(),
3486-
$callableParameter->passedByReference(),
3487-
$callableParameter->isVariadic(),
3488-
$callableParameter->getDefaultValue(),
3489-
));
3490-
}
3491-
3492-
$callableParameters = $newParameters;
3493-
}
3494-
}
3495-
}
3429+
$callableParameters = $this->createCallableParameters(
3430+
$scope,
3431+
$expr,
3432+
$closureCallArgs,
3433+
$passedToType,
3434+
);
34963435

34973436
$useScope = $scope;
34983437
foreach ($expr->uses as $use) {
@@ -3627,7 +3566,6 @@ private function processArrowFunctionNode(
36273566
Expr\ArrowFunction $expr,
36283567
MutatingScope $scope,
36293568
callable $nodeCallback,
3630-
ExpressionContext $context,
36313569
?Type $passedToType,
36323570
): ExpressionResult
36333571
{
@@ -3638,20 +3576,41 @@ private function processArrowFunctionNode(
36383576
$nodeCallback($expr->returnType, $scope);
36393577
}
36403578

3641-
$callableParameters = null;
36423579
$arrowFunctionCallArgs = $expr->getAttribute(ArrowFunctionArgVisitor::ATTRIBUTE_NAME);
3580+
$arrowFunctionScope = $scope->enterArrowFunction($expr, $this->createCallableParameters(
3581+
$scope,
3582+
$expr,
3583+
$arrowFunctionCallArgs,
3584+
$passedToType,
3585+
));
3586+
$arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection();
3587+
if (!$arrowFunctionType instanceof ClosureType) {
3588+
throw new ShouldNotHappenException();
3589+
}
3590+
$nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope);
3591+
$this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel());
3592+
3593+
return new ExpressionResult($scope, false, []);
3594+
}
36433595

3644-
if ($arrowFunctionCallArgs !== null) {
3645-
$acceptors = $scope->getType($expr)->getCallableParametersAcceptors($scope);
3596+
/**
3597+
* @param Node\Arg[] $args
3598+
* @return ParameterReflection[]|null
3599+
*/
3600+
private function createCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $passedToType): ?array
3601+
{
3602+
$callableParameters = null;
3603+
if ($args !== null) {
3604+
$acceptors = $scope->getType($closureExpr)->getCallableParametersAcceptors($scope);
36463605
if (count($acceptors) === 1) {
36473606
$callableParameters = $acceptors[0]->getParameters();
36483607

36493608
foreach ($callableParameters as $index => $callableParameter) {
3650-
if (!isset($arrowFunctionCallArgs[$index])) {
3609+
if (!isset($args[$index])) {
36513610
continue;
36523611
}
36533612

3654-
$type = $scope->getType($arrowFunctionCallArgs[$index]->value);
3613+
$type = $scope->getType($args[$index]->value);
36553614
$callableParameters[$index] = new NativeParameterReflection(
36563615
$callableParameter->getName(),
36573616
$callableParameter->isOptional(),
@@ -3707,15 +3666,7 @@ private function processArrowFunctionNode(
37073666
}
37083667
}
37093668

3710-
$arrowFunctionScope = $scope->enterArrowFunction($expr, $callableParameters);
3711-
$arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection();
3712-
if (!$arrowFunctionType instanceof ClosureType) {
3713-
throw new ShouldNotHappenException();
3714-
}
3715-
$nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope);
3716-
$this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel());
3717-
3718-
return new ExpressionResult($scope, false, []);
3669+
return $callableParameters;
37193670
}
37203671

37213672
/**
@@ -3844,7 +3795,7 @@ private function processArgs(
38443795
$result = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context, $parameterType ?? null);
38453796
} elseif ($arg->value instanceof Expr\ArrowFunction) {
38463797
$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $context);
3847-
$result = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context, $parameterType ?? null);
3798+
$result = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $parameterType ?? null);
38483799
} else {
38493800
$result = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep());
38503801
}

0 commit comments

Comments
 (0)