@@ -5594,18 +5594,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
5594
5594
private function filterTypeWithMethod (Type $ typeWithMethod , string $ methodName ): ?Type
5595
5595
{
5596
5596
if ($ typeWithMethod instanceof UnionType) {
5597
- $ newTypes = [];
5598
- foreach ($ typeWithMethod ->getTypes () as $ innerType ) {
5599
- if (!$ innerType ->hasMethod ($ methodName )->yes ()) {
5600
- continue ;
5601
- }
5602
-
5603
- $ newTypes [] = $ innerType ;
5604
- }
5605
- if (count ($ newTypes ) === 0 ) {
5606
- return null ;
5607
- }
5608
- $ typeWithMethod = TypeCombinator::union (...$ newTypes );
5597
+ $ typeWithMethod = $ typeWithMethod ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasMethod ($ methodName )->yes ());
5609
5598
}
5610
5599
5611
5600
if (!$ typeWithMethod ->hasMethod ($ methodName )->yes ()) {
@@ -5709,18 +5698,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
5709
5698
public function getPropertyReflection (Type $ typeWithProperty , string $ propertyName ): ?ExtendedPropertyReflection
5710
5699
{
5711
5700
if ($ typeWithProperty instanceof UnionType) {
5712
- $ newTypes = [];
5713
- foreach ($ typeWithProperty ->getTypes () as $ innerType ) {
5714
- if (!$ innerType ->hasProperty ($ propertyName )->yes ()) {
5715
- continue ;
5716
- }
5717
-
5718
- $ newTypes [] = $ innerType ;
5719
- }
5720
- if (count ($ newTypes ) === 0 ) {
5721
- return null ;
5722
- }
5723
- $ typeWithProperty = TypeCombinator::union (...$ newTypes );
5701
+ $ typeWithProperty = $ typeWithProperty ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasProperty ($ propertyName )->yes ());
5724
5702
}
5725
5703
if (!$ typeWithProperty ->hasProperty ($ propertyName )->yes ()) {
5726
5704
return null ;
@@ -5749,18 +5727,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
5749
5727
public function getConstantReflection (Type $ typeWithConstant , string $ constantName ): ?ConstantReflection
5750
5728
{
5751
5729
if ($ typeWithConstant instanceof UnionType) {
5752
- $ newTypes = [];
5753
- foreach ($ typeWithConstant ->getTypes () as $ innerType ) {
5754
- if (!$ innerType ->hasConstant ($ constantName )->yes ()) {
5755
- continue ;
5756
- }
5757
-
5758
- $ newTypes [] = $ innerType ;
5759
- }
5760
- if (count ($ newTypes ) === 0 ) {
5761
- return null ;
5762
- }
5763
- $ typeWithConstant = TypeCombinator::union (...$ newTypes );
5730
+ $ typeWithConstant = $ typeWithConstant ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasConstant ($ constantName )->yes ());
5764
5731
}
5765
5732
if (!$ typeWithConstant ->hasConstant ($ constantName )->yes ()) {
5766
5733
return null ;
@@ -5804,18 +5771,10 @@ private function getNativeConstantTypes(): array
5804
5771
public function getIterableKeyType (Type $ iteratee ): Type
5805
5772
{
5806
5773
if ($ iteratee instanceof UnionType) {
5807
- $ newTypes = [];
5808
- foreach ($ iteratee ->getTypes () as $ innerType ) {
5809
- if (!$ innerType ->isIterable ()->yes ()) {
5810
- continue ;
5811
- }
5812
-
5813
- $ newTypes [] = $ innerType ;
5814
- }
5815
- if (count ($ newTypes ) === 0 ) {
5816
- return $ iteratee ->getIterableKeyType ();
5774
+ $ filtered = $ iteratee ->filterTypes (static fn (Type $ innerType ) => $ innerType ->isIterable ()->yes ());
5775
+ if (!$ filtered instanceof NeverType) {
5776
+ $ iteratee = $ filtered ;
5817
5777
}
5818
- $ iteratee = TypeCombinator::union (...$ newTypes );
5819
5778
}
5820
5779
5821
5780
return $ iteratee ->getIterableKeyType ();
@@ -5824,18 +5783,10 @@ public function getIterableKeyType(Type $iteratee): Type
5824
5783
public function getIterableValueType (Type $ iteratee ): Type
5825
5784
{
5826
5785
if ($ iteratee instanceof UnionType) {
5827
- $ newTypes = [];
5828
- foreach ($ iteratee ->getTypes () as $ innerType ) {
5829
- if (!$ innerType ->isIterable ()->yes ()) {
5830
- continue ;
5831
- }
5832
-
5833
- $ newTypes [] = $ innerType ;
5834
- }
5835
- if (count ($ newTypes ) === 0 ) {
5836
- return $ iteratee ->getIterableValueType ();
5786
+ $ filtered = $ iteratee ->filterTypes (static fn (Type $ innerType ) => $ innerType ->isIterable ()->yes ());
5787
+ if (!$ filtered instanceof NeverType) {
5788
+ $ iteratee = $ filtered ;
5837
5789
}
5838
- $ iteratee = TypeCombinator::union (...$ newTypes );
5839
5790
}
5840
5791
5841
5792
return $ iteratee ->getIterableValueType ();
0 commit comments