Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Type/Php/ArrayFirstLastDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
Expand All @@ -19,7 +20,12 @@ final class ArrayFirstLastDynamicReturnTypeExtension implements DynamicFunctionR

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['array_first', 'array_last'], true);
return in_array($functionReflection->getName(), [
'array_key_first',
'array_key_last',
'array_first',
'array_last',
], true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
Expand All @@ -37,13 +43,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new NullType();
}

$valueType = $argType->getIterableValueType();
switch ($functionReflection->getName()) {
case 'array_key_first':
case 'array_key_last':
$resultType = $argType->getIterableKeyType();
break;
case 'array_first':
case 'array_last':
$resultType = $argType->getIterableValueType();
break;
default:
throw new ShouldNotHappenException();
}

if ($iterableAtLeastOnce->yes()) {
return $valueType;
return $resultType;
}

return TypeCombinator::union($valueType, new NullType());
return TypeCombinator::union($resultType, new NullType());
}

}
43 changes: 0 additions & 43 deletions src/Type/Php/ArrayKeyFirstDynamicReturnTypeExtension.php

This file was deleted.

43 changes: 0 additions & 43 deletions src/Type/Php/ArrayKeyLastDynamicReturnTypeExtension.php

This file was deleted.

Loading