generated from php-standard-library/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FunctionReturnTypeProvider.php
55 lines (45 loc) · 1.62 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
<?php
declare(strict_types=1);
namespace Psl\Psalm\EventHandler\Iter\First;
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\first',
];
}
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[1];
}
if ($array_argument_type instanceof Type\Atomic\TNonEmptyList) {
return $array_argument_type->type_param;
}
if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// TODO(azjezz): add support for this once psalm starts enforcing the shape order ( if ever ).
//
// foreach ($properties as $property) {
// return $property;
// }
return $array_argument_type->getGenericValueType();
}
return null;
}
}