-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathHighOrderFunctionArgInfo.php
90 lines (77 loc) · 2.57 KB
/
HighOrderFunctionArgInfo.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Union;
use function array_merge;
/**
* @internal
*/
final class HighOrderFunctionArgInfo
{
public const TYPE_FIRST_CLASS_CALLABLE = 'first-class-callable';
public const TYPE_CLASS_CALLABLE = 'class-callable';
public const TYPE_STRING_CALLABLE = 'string-callable';
public const TYPE_CALLABLE = 'callable';
/** @psalm-var HighOrderFunctionArgInfo::TYPE_* */
private string $type;
private FunctionLikeStorage $function_storage;
private ?ClassLikeStorage $class_storage;
/**
* @psalm-param HighOrderFunctionArgInfo::TYPE_* $type
*/
public function __construct(
string $type,
FunctionLikeStorage $function_storage,
?ClassLikeStorage $class_storage = null
) {
$this->type = $type;
$this->function_storage = $function_storage;
$this->class_storage = $class_storage;
}
public function getTemplates(): TemplateResult
{
$templates = $this->class_storage
? array_merge(
$this->function_storage->template_types ?? [],
$this->class_storage->template_types ?? [],
)
: $this->function_storage->template_types ?? [];
return new TemplateResult($templates, []);
}
public function getType(): string
{
return $this->type;
}
public function getFunctionType(): Union
{
switch ($this->type) {
case self::TYPE_FIRST_CLASS_CALLABLE:
return new Union([
new TClosure(
'Closure',
$this->function_storage->params,
$this->function_storage->return_type,
$this->function_storage->pure,
),
]);
case self::TYPE_STRING_CALLABLE:
case self::TYPE_CLASS_CALLABLE:
return new Union([
new TCallable(
'callable',
$this->function_storage->params,
$this->function_storage->return_type,
$this->function_storage->pure,
),
]);
default:
return $this->function_storage->return_type ?? Type::getMixed();
}
}
}