Skip to content

Commit 75f243d

Browse files
takaramondrejmirtes
authored andcommitted
Merge similar dynamic return type extensions into a single class
1 parent 59589cb commit 75f243d

File tree

3 files changed

+21
-90
lines changed

3 files changed

+21
-90
lines changed

src/Type/Php/ArrayFirstLastDynamicReturnTypeExtension.php

Lines changed: 21 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,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3743
return new NullType();
3844
}
3945

40-
$valueType = $argType->getIterableValueType();
46+
switch ($functionReflection->getName()) {
47+
case 'array_key_first':
48+
case 'array_key_last':
49+
$resultType = $argType->getIterableKeyType();
50+
break;
51+
case 'array_first':
52+
case 'array_last':
53+
$resultType = $argType->getIterableValueType();
54+
break;
55+
default:
56+
throw new ShouldNotHappenException();
57+
}
4158

4259
if ($iterableAtLeastOnce->yes()) {
43-
return $valueType;
60+
return $resultType;
4461
}
4562

46-
return TypeCombinator::union($valueType, new NullType());
63+
return TypeCombinator::union($resultType, new NullType());
4764
}
4865

4966
}

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)