Skip to content

Commit 6b65db3

Browse files
Improve
1 parent 5b10da3 commit 6b65db3

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

src/Analyser/MutatingScope.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,10 +5594,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
55945594
private function filterTypeWithMethod(Type $typeWithMethod, string $methodName): ?Type
55955595
{
55965596
if ($typeWithMethod instanceof UnionType) {
5597-
$typeWithMethod = $typeWithMethod->filterTypes(fn (Type $innerType) => $innerType->hasMethod($methodName)->yes());
5598-
if ($typeWithMethod === null) {
5599-
return null;
5600-
}
5597+
$typeWithMethod = $typeWithMethod->filterTypes(static fn (Type $innerType) => $innerType->hasMethod($methodName)->yes());
56015598
}
56025599

56035600
if (!$typeWithMethod->hasMethod($methodName)->yes()) {
@@ -5701,10 +5698,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
57015698
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection
57025699
{
57035700
if ($typeWithProperty instanceof UnionType) {
5704-
$typeWithProperty = $typeWithProperty->filterTypes(fn (Type $innerType) => $innerType->hasProperty($propertyName)->yes());
5705-
if ($typeWithProperty === null) {
5706-
return null;
5707-
}
5701+
$typeWithProperty = $typeWithProperty->filterTypes(static fn (Type $innerType) => $innerType->hasProperty($propertyName)->yes());
57085702
}
57095703
if (!$typeWithProperty->hasProperty($propertyName)->yes()) {
57105704
return null;
@@ -5733,10 +5727,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
57335727
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ConstantReflection
57345728
{
57355729
if ($typeWithConstant instanceof UnionType) {
5736-
$typeWithConstant = $typeWithConstant->filterTypes(fn (Type $innerType) => $innerType->hasConstant($constantName)->yes());
5737-
if ($typeWithConstant === null) {
5738-
return null;
5739-
}
5730+
$typeWithConstant = $typeWithConstant->filterTypes(static fn (Type $innerType) => $innerType->hasConstant($constantName)->yes());
57405731
}
57415732
if (!$typeWithConstant->hasConstant($constantName)->yes()) {
57425733
return null;
@@ -5780,7 +5771,10 @@ private function getNativeConstantTypes(): array
57805771
public function getIterableKeyType(Type $iteratee): Type
57815772
{
57825773
if ($iteratee instanceof UnionType) {
5783-
$iteratee = $iteratee->filterTypes(fn (Type $innerType) => $innerType->isIterable()->yes()) ?? $iteratee;
5774+
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5775+
if (!$filtered instanceof NeverType) {
5776+
$iteratee = $filtered;
5777+
}
57845778
}
57855779

57865780
return $iteratee->getIterableKeyType();
@@ -5789,7 +5783,10 @@ public function getIterableKeyType(Type $iteratee): Type
57895783
public function getIterableValueType(Type $iteratee): Type
57905784
{
57915785
if ($iteratee instanceof UnionType) {
5792-
$iteratee = $iteratee->filterTypes(fn (Type $innerType) => $innerType->isIterable()->yes()) ?? $iteratee;
5786+
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5787+
if (!$filtered instanceof NeverType) {
5788+
$iteratee = $filtered;
5789+
}
57935790
}
57945791

57955792
return $iteratee->getIterableValueType();

src/Type/UnionType.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ public function getTypes(): array
8888

8989
/**
9090
* @param callable(Type $type): bool $filterCb
91-
*
92-
* @return Type|null
9391
*/
94-
public function filterTypes(callable $filterCb): ?Type
92+
public function filterTypes(callable $filterCb): Type
9593
{
9694
$newTypes = [];
9795
foreach ($this->getTypes() as $innerType) {
@@ -101,9 +99,6 @@ public function filterTypes(callable $filterCb): ?Type
10199

102100
$newTypes[] = $innerType;
103101
}
104-
if (count($newTypes) === 0) {
105-
return null;
106-
}
107102

108103
return TypeCombinator::union(...$newTypes);
109104
}

0 commit comments

Comments
 (0)