@@ -207,6 +207,7 @@ public function __construct(
207
207
private ConstantResolver $ constantResolver ,
208
208
private ScopeContext $ context ,
209
209
private PhpVersion $ phpVersion ,
210
+ private MethodCallReturnTypeHelper $ methodCallReturnTypeHelper ,
210
211
private bool $ declareStrictTypes = false ,
211
212
private PhpFunctionFromParserNodeReflection |null $ function = null ,
212
213
?string $ namespace = null ,
@@ -5485,7 +5486,6 @@ private function exactInstantiation(New_ $node, string $className): ?Type
5485
5486
$ constructorMethod = new DummyConstructorReflection ($ classReflection );
5486
5487
}
5487
5488
5488
- $ resolvedTypes = [];
5489
5489
$ methodCall = new Expr \StaticCall (
5490
5490
new Name ($ resolvedClassName ),
5491
5491
new Node \Identifier ($ constructorMethod ->getName ()),
@@ -5498,29 +5498,15 @@ private function exactInstantiation(New_ $node, string $className): ?Type
5498
5498
$ constructorMethod ->getVariants (),
5499
5499
$ constructorMethod ->getNamedArgumentsVariants (),
5500
5500
);
5501
- $ normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments ($ parametersAcceptor , $ methodCall );
5502
-
5503
- if ($ normalizedMethodCall !== null ) {
5504
- foreach ($ this ->dynamicReturnTypeExtensionRegistry ->getDynamicStaticMethodReturnTypeExtensionsForClass ($ classReflection ->getName ()) as $ dynamicStaticMethodReturnTypeExtension ) {
5505
- if (!$ dynamicStaticMethodReturnTypeExtension ->isStaticMethodSupported ($ constructorMethod )) {
5506
- continue ;
5507
- }
5508
-
5509
- $ resolvedType = $ dynamicStaticMethodReturnTypeExtension ->getTypeFromStaticMethodCall (
5510
- $ constructorMethod ,
5511
- $ normalizedMethodCall ,
5512
- $ this ,
5513
- );
5514
- if ($ resolvedType === null ) {
5515
- continue ;
5516
- }
5517
-
5518
- $ resolvedTypes [] = $ resolvedType ;
5519
- }
5520
- }
5521
-
5522
- if (count ($ resolvedTypes ) > 0 ) {
5523
- return TypeCombinator::union (...$ resolvedTypes );
5501
+ $ returnType = $ this ->methodCallReturnTypeHelper ->constructorReturnType (
5502
+ $ parametersAcceptor ,
5503
+ $ this ,
5504
+ $ methodCall ,
5505
+ $ constructorMethod ,
5506
+ $ classReflection ->getName (),
5507
+ );
5508
+ if ($ returnType !== null ) {
5509
+ return $ returnType ;
5524
5510
}
5525
5511
5526
5512
$ methodResult = $ this ->getType ($ methodCall );
@@ -5619,34 +5605,10 @@ private function exactInstantiation(New_ $node, string $className): ?Type
5619
5605
);
5620
5606
}
5621
5607
5622
- private function filterTypeWithMethod (Type $ typeWithMethod , string $ methodName ): ?Type
5623
- {
5624
- if ($ typeWithMethod instanceof UnionType) {
5625
- $ newTypes = [];
5626
- foreach ($ typeWithMethod ->getTypes () as $ innerType ) {
5627
- if (!$ innerType ->hasMethod ($ methodName )->yes ()) {
5628
- continue ;
5629
- }
5630
-
5631
- $ newTypes [] = $ innerType ;
5632
- }
5633
- if (count ($ newTypes ) === 0 ) {
5634
- return null ;
5635
- }
5636
- $ typeWithMethod = TypeCombinator::union (...$ newTypes );
5637
- }
5638
-
5639
- if (!$ typeWithMethod ->hasMethod ($ methodName )->yes ()) {
5640
- return null ;
5641
- }
5642
-
5643
- return $ typeWithMethod ;
5644
- }
5645
-
5646
5608
/** @api */
5647
5609
public function getMethodReflection (Type $ typeWithMethod , string $ methodName ): ?ExtendedMethodReflection
5648
5610
{
5649
- $ type = $ this ->filterTypeWithMethod ($ typeWithMethod , $ methodName );
5611
+ $ type = $ this ->methodCallReturnTypeHelper -> filterTypeWithMethod ($ typeWithMethod , $ methodName );
5650
5612
if ($ type === null ) {
5651
5613
return null ;
5652
5614
}
@@ -5657,7 +5619,7 @@ public function getMethodReflection(Type $typeWithMethod, string $methodName): ?
5657
5619
/** @api */
5658
5620
public function getNakedMethod (Type $ typeWithMethod , string $ methodName ): ?ExtendedMethodReflection
5659
5621
{
5660
- $ type = $ this ->filterTypeWithMethod ($ typeWithMethod , $ methodName );
5622
+ $ type = $ this ->methodCallReturnTypeHelper -> filterTypeWithMethod ($ typeWithMethod , $ methodName );
5661
5623
if ($ type === null ) {
5662
5624
return null ;
5663
5625
}
@@ -5670,7 +5632,7 @@ public function getNakedMethod(Type $typeWithMethod, string $methodName): ?Exten
5670
5632
*/
5671
5633
private function methodCallReturnType (Type $ typeWithMethod , string $ methodName , Expr $ methodCall ): ?Type
5672
5634
{
5673
- $ typeWithMethod = $ this ->filterTypeWithMethod ($ typeWithMethod , $ methodName );
5635
+ $ typeWithMethod = $ this ->methodCallReturnTypeHelper -> filterTypeWithMethod ($ typeWithMethod , $ methodName );
5674
5636
if ($ typeWithMethod === null ) {
5675
5637
return null ;
5676
5638
}
@@ -5682,55 +5644,20 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
5682
5644
$ methodReflection ->getVariants (),
5683
5645
$ methodReflection ->getNamedArgumentsVariants (),
5684
5646
);
5685
- if ($ methodCall instanceof MethodCall) {
5686
- $ normalizedMethodCall = ArgumentsNormalizer::reorderMethodArguments ($ parametersAcceptor , $ methodCall );
5687
- } else {
5688
- $ normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments ($ parametersAcceptor , $ methodCall );
5689
- }
5690
- if ($ normalizedMethodCall === null ) {
5691
- return $ this ->transformVoidToNull ($ parametersAcceptor ->getReturnType (), $ methodCall );
5692
- }
5693
-
5694
- $ resolvedTypes = [];
5695
- foreach ($ typeWithMethod ->getObjectClassNames () as $ className ) {
5696
- if ($ normalizedMethodCall instanceof MethodCall) {
5697
- foreach ($ this ->dynamicReturnTypeExtensionRegistry ->getDynamicMethodReturnTypeExtensionsForClass ($ className ) as $ dynamicMethodReturnTypeExtension ) {
5698
- if (!$ dynamicMethodReturnTypeExtension ->isMethodSupported ($ methodReflection )) {
5699
- continue ;
5700
- }
5701
-
5702
- $ resolvedType = $ dynamicMethodReturnTypeExtension ->getTypeFromMethodCall ($ methodReflection , $ normalizedMethodCall , $ this );
5703
- if ($ resolvedType === null ) {
5704
- continue ;
5705
- }
5706
-
5707
- $ resolvedTypes [] = $ resolvedType ;
5708
- }
5709
- } else {
5710
- foreach ($ this ->dynamicReturnTypeExtensionRegistry ->getDynamicStaticMethodReturnTypeExtensionsForClass ($ className ) as $ dynamicStaticMethodReturnTypeExtension ) {
5711
- if (!$ dynamicStaticMethodReturnTypeExtension ->isStaticMethodSupported ($ methodReflection )) {
5712
- continue ;
5713
- }
5714
-
5715
- $ resolvedType = $ dynamicStaticMethodReturnTypeExtension ->getTypeFromStaticMethodCall (
5716
- $ methodReflection ,
5717
- $ normalizedMethodCall ,
5718
- $ this ,
5719
- );
5720
- if ($ resolvedType === null ) {
5721
- continue ;
5722
- }
5723
5647
5724
- $ resolvedTypes [] = $ resolvedType ;
5725
- }
5726
- }
5727
- }
5648
+ $ returnType = $ this ->methodCallReturnTypeHelper ->methodCallReturnType (
5649
+ $ parametersAcceptor ,
5650
+ $ this ,
5651
+ $ typeWithMethod ,
5652
+ $ methodName ,
5653
+ $ methodCall ,
5654
+ );
5728
5655
5729
- if (count ( $ resolvedTypes ) > 0 ) {
5730
- return $ this ->transformVoidToNull (TypeCombinator:: union (... $ resolvedTypes ) , $ methodCall );
5656
+ if ($ returnType !== null ) {
5657
+ return $ this ->transformVoidToNull ($ returnType , $ methodCall );
5731
5658
}
5732
5659
5733
- return $ this -> transformVoidToNull ( $ parametersAcceptor -> getReturnType (), $ methodCall ) ;
5660
+ return null ;
5734
5661
}
5735
5662
5736
5663
/** @api */
0 commit comments