generated from php-standard-library/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FunctionReturnTypeProvider.php
59 lines (49 loc) · 1.77 KB
/
FunctionReturnTypeProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Psl\Psalm\EventHandler\Iter\LastKey;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psl\Psalm\Argument;
final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return non-empty-list<lowercase-string>
*/
public static function getFunctionIds(): array
{
return [
'psl\iter\last_key',
];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union
{
$argument_type = Argument::getType($event->getCallArgs(), $event->getStatementsSource(), 0);
if (null === $argument_type) {
return null;
}
$array_argument_type = $argument_type->getAtomicTypes()['array'] ?? null;
if (null === $array_argument_type) {
return null;
}
if ($array_argument_type instanceof Type\Atomic\TNonEmptyArray) {
return $array_argument_type->type_params[0];
}
if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
return Type::getInt();
}
if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
//
// $properties = $array_argument_type->properties;
// $last_property = null;
// foreach ($properties as $property) {
// $last_property = $property;
// }
//
// return $last_property;
return $array_argument_type->getGenericKeyType();
}
return null;
}
}