@@ -2533,7 +2533,7 @@ static function (): void {
2533
2533
} elseif ($ expr instanceof Expr \Closure) {
2534
2534
return $ this ->processClosureNode ($ stmt , $ expr , $ scope , $ nodeCallback , $ context , null );
2535
2535
} elseif ($ expr instanceof Expr \ArrowFunction) {
2536
- return $ this ->processArrowFunctionNode ($ stmt , $ expr , $ scope , $ nodeCallback , $ context , null );
2536
+ return $ this ->processArrowFunctionNode ($ stmt , $ expr , $ scope , $ nodeCallback , null );
2537
2537
} elseif ($ expr instanceof ErrorSuppress) {
2538
2538
$ result = $ this ->processExprNode ($ stmt , $ expr ->expr , $ scope , $ nodeCallback , $ context );
2539
2539
$ hasYield = $ result ->hasYield ();
@@ -3425,74 +3425,13 @@ private function processClosureNode(
3425
3425
3426
3426
$ byRefUses = [];
3427
3427
3428
- $ callableParameters = null ;
3429
3428
$ 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
+ );
3496
3435
3497
3436
$ useScope = $ scope ;
3498
3437
foreach ($ expr ->uses as $ use ) {
@@ -3627,7 +3566,6 @@ private function processArrowFunctionNode(
3627
3566
Expr \ArrowFunction $ expr ,
3628
3567
MutatingScope $ scope ,
3629
3568
callable $ nodeCallback ,
3630
- ExpressionContext $ context ,
3631
3569
?Type $ passedToType ,
3632
3570
): ExpressionResult
3633
3571
{
@@ -3638,20 +3576,41 @@ private function processArrowFunctionNode(
3638
3576
$ nodeCallback ($ expr ->returnType , $ scope );
3639
3577
}
3640
3578
3641
- $ callableParameters = null ;
3642
3579
$ 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
+ }
3643
3595
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 );
3646
3605
if (count ($ acceptors ) === 1 ) {
3647
3606
$ callableParameters = $ acceptors [0 ]->getParameters ();
3648
3607
3649
3608
foreach ($ callableParameters as $ index => $ callableParameter ) {
3650
- if (!isset ($ arrowFunctionCallArgs [$ index ])) {
3609
+ if (!isset ($ args [$ index ])) {
3651
3610
continue ;
3652
3611
}
3653
3612
3654
- $ type = $ scope ->getType ($ arrowFunctionCallArgs [$ index ]->value );
3613
+ $ type = $ scope ->getType ($ args [$ index ]->value );
3655
3614
$ callableParameters [$ index ] = new NativeParameterReflection (
3656
3615
$ callableParameter ->getName (),
3657
3616
$ callableParameter ->isOptional (),
@@ -3707,15 +3666,7 @@ private function processArrowFunctionNode(
3707
3666
}
3708
3667
}
3709
3668
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 ;
3719
3670
}
3720
3671
3721
3672
/**
@@ -3844,7 +3795,7 @@ private function processArgs(
3844
3795
$ result = $ this ->processClosureNode ($ stmt , $ arg ->value , $ scopeToPass , $ nodeCallback , $ context , $ parameterType ?? null );
3845
3796
} elseif ($ arg ->value instanceof Expr \ArrowFunction) {
3846
3797
$ 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 );
3848
3799
} else {
3849
3800
$ result = $ this ->processExprNode ($ stmt , $ arg ->value , $ scopeToPass , $ nodeCallback , $ context ->enterDeep ());
3850
3801
}
0 commit comments