Skip to content

Commit 3f85a1f

Browse files
committed
Merge similar dynamic return type extensions into a single class
1 parent 216c8b1 commit 3f85a1f

File tree

3 files changed

+14
-90
lines changed

3 files changed

+14
-90
lines changed

src/Type/Php/ArrayFirstLastDynamicReturnTypeExtension.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\ShouldNotHappenException;
910
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1011
use PHPStan\Type\NullType;
1112
use PHPStan\Type\Type;
@@ -19,7 +20,12 @@ final class ArrayFirstLastDynamicReturnTypeExtension implements DynamicFunctionR
1920

2021
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2122
{
22-
return in_array($functionReflection->getName(), ['array_first', 'array_last'], true);
23+
return in_array($functionReflection->getName(), [
24+
'array_key_first',
25+
'array_key_last',
26+
'array_first',
27+
'array_last',
28+
], true);
2329
}
2430

2531
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
@@ -37,13 +43,17 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3743
return new NullType();
3844
}
3945

40-
$valueType = $argType->getIterableValueType();
46+
$resultType = match ($functionReflection->getName()) {
47+
'array_key_first', 'array_key_last' => $argType->getIterableKeyType(),
48+
'array_first', 'array_last' => $argType->getIterableValueType(),
49+
default => throw new ShouldNotHappenException(),
50+
};
4151

4252
if ($iterableAtLeastOnce->yes()) {
43-
return $valueType;
53+
return $resultType;
4454
}
4555

46-
return TypeCombinator::union($valueType, new NullType());
56+
return TypeCombinator::union($resultType, new NullType());
4757
}
4858

4959
}

src/Type/Php/ArrayKeyFirstDynamicReturnTypeExtension.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Type/Php/ArrayKeyLastDynamicReturnTypeExtension.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)