@@ -5527,18 +5527,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
55275527 private function filterTypeWithMethod (Type $ typeWithMethod , string $ methodName ): ?Type
55285528 {
55295529 if ($ typeWithMethod instanceof UnionType) {
5530- $ newTypes = [];
5531- foreach ($ typeWithMethod ->getTypes () as $ innerType ) {
5532- if (!$ innerType ->hasMethod ($ methodName )->yes ()) {
5533- continue ;
5534- }
5535-
5536- $ newTypes [] = $ innerType ;
5537- }
5538- if (count ($ newTypes ) === 0 ) {
5539- return null ;
5540- }
5541- $ typeWithMethod = TypeCombinator::union (...$ newTypes );
5530+ $ typeWithMethod = $ typeWithMethod ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasMethod ($ methodName )->yes ());
55425531 }
55435532
55445533 if (!$ typeWithMethod ->hasMethod ($ methodName )->yes ()) {
@@ -5641,18 +5630,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
56415630 public function getPropertyReflection (Type $ typeWithProperty , string $ propertyName ): ?ExtendedPropertyReflection
56425631 {
56435632 if ($ typeWithProperty instanceof UnionType) {
5644- $ newTypes = [];
5645- foreach ($ typeWithProperty ->getTypes () as $ innerType ) {
5646- if (!$ innerType ->hasProperty ($ propertyName )->yes ()) {
5647- continue ;
5648- }
5649-
5650- $ newTypes [] = $ innerType ;
5651- }
5652- if (count ($ newTypes ) === 0 ) {
5653- return null ;
5654- }
5655- $ typeWithProperty = TypeCombinator::union (...$ newTypes );
5633+ $ typeWithProperty = $ typeWithProperty ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasProperty ($ propertyName )->yes ());
56565634 }
56575635 if (!$ typeWithProperty ->hasProperty ($ propertyName )->yes ()) {
56585636 return null ;
@@ -5681,18 +5659,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
56815659 public function getConstantReflection (Type $ typeWithConstant , string $ constantName ): ?ClassConstantReflection
56825660 {
56835661 if ($ typeWithConstant instanceof UnionType) {
5684- $ newTypes = [];
5685- foreach ($ typeWithConstant ->getTypes () as $ innerType ) {
5686- if (!$ innerType ->hasConstant ($ constantName )->yes ()) {
5687- continue ;
5688- }
5689-
5690- $ newTypes [] = $ innerType ;
5691- }
5692- if (count ($ newTypes ) === 0 ) {
5693- return null ;
5694- }
5695- $ typeWithConstant = TypeCombinator::union (...$ newTypes );
5662+ $ typeWithConstant = $ typeWithConstant ->filterTypes (static fn (Type $ innerType ) => $ innerType ->hasConstant ($ constantName )->yes ());
56965663 }
56975664 if (!$ typeWithConstant ->hasConstant ($ constantName )->yes ()) {
56985665 return null ;
@@ -5736,18 +5703,10 @@ private function getNativeConstantTypes(): array
57365703 public function getIterableKeyType (Type $ iteratee ): Type
57375704 {
57385705 if ($ iteratee instanceof UnionType) {
5739- $ newTypes = [];
5740- foreach ($ iteratee ->getTypes () as $ innerType ) {
5741- if (!$ innerType ->isIterable ()->yes ()) {
5742- continue ;
5743- }
5744-
5745- $ newTypes [] = $ innerType ;
5746- }
5747- if (count ($ newTypes ) === 0 ) {
5748- return $ iteratee ->getIterableKeyType ();
5706+ $ filtered = $ iteratee ->filterTypes (static fn (Type $ innerType ) => $ innerType ->isIterable ()->yes ());
5707+ if (!$ filtered instanceof NeverType) {
5708+ $ iteratee = $ filtered ;
57495709 }
5750- $ iteratee = TypeCombinator::union (...$ newTypes );
57515710 }
57525711
57535712 return $ iteratee ->getIterableKeyType ();
@@ -5756,18 +5715,10 @@ public function getIterableKeyType(Type $iteratee): Type
57565715 public function getIterableValueType (Type $ iteratee ): Type
57575716 {
57585717 if ($ iteratee instanceof UnionType) {
5759- $ newTypes = [];
5760- foreach ($ iteratee ->getTypes () as $ innerType ) {
5761- if (!$ innerType ->isIterable ()->yes ()) {
5762- continue ;
5763- }
5764-
5765- $ newTypes [] = $ innerType ;
5766- }
5767- if (count ($ newTypes ) === 0 ) {
5768- return $ iteratee ->getIterableValueType ();
5718+ $ filtered = $ iteratee ->filterTypes (static fn (Type $ innerType ) => $ innerType ->isIterable ()->yes ());
5719+ if (!$ filtered instanceof NeverType) {
5720+ $ iteratee = $ filtered ;
57695721 }
5770- $ iteratee = TypeCombinator::union (...$ newTypes );
57715722 }
57725723
57735724 return $ iteratee ->getIterableValueType ();
0 commit comments