/
TNamedObject.php
153 lines (130 loc) · 3.87 KB
/
TNamedObject.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Type;
use Psalm\Type\Atomic;
use function array_map;
use function implode;
use function strrpos;
use function substr;
/**
* Denotes an object type where the type of the object is known e.g. `Exception`, `Throwable`, `Foo\Bar`
*/
class TNamedObject extends Atomic
{
use HasIntersectionTrait;
/**
* @var string
*/
public $value;
/**
* @var bool
*/
public $is_static = false;
/**
* Whether or not this type can represent a child of the class named in $value
* @var bool
*/
public $definite_class = false;
/**
* @param string $value the name of the object
*/
public function __construct(string $value, bool $is_static = false, bool $definite_class = false)
{
if ($value[0] === '\\') {
$value = substr($value, 1);
}
$this->value = $value;
$this->is_static = $is_static;
$this->definite_class = $definite_class;
}
public function __toString(): string
{
return $this->getKey();
}
public function getKey(bool $include_extra = true): string
{
if ($include_extra && $this->extra_types) {
return $this->value . '&' . implode('&', $this->extra_types);
}
return $this->value;
}
public function getId(bool $nested = false): string
{
if ($this->extra_types) {
return $this->value . '&' . implode(
'&',
array_map(
fn($type) => $type->getId(true),
$this->extra_types
)
);
}
return $this->is_static ? $this->value . '&static' : $this->value;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
if ($this->value === 'static') {
return 'static';
}
$intersection_types = $this->getNamespacedIntersectionTypes(
$namespace,
$aliased_classes,
$this_class,
$use_phpdoc_format
);
return Type::getStringFromFQCLN(
$this->value,
$namespace,
$aliased_classes,
$this_class,
true,
$this->is_static
) . $intersection_types;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): ?string {
if ($this->value === 'static') {
return $analysis_php_version_id >= 8_00_00 ? 'static' : null;
}
if ($this->is_static && $this->value === $this_class) {
return $analysis_php_version_id >= 8_00_00 ? 'static' : 'self';
}
$result = $this->toNamespacedString($namespace, $aliased_classes, $this_class, false);
$intersection = strrpos($result, '&');
if ($intersection === false || $analysis_php_version_id >= 8_01_00) {
return $result;
}
return substr($result, $intersection+1);
}
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return ($this->value !== 'static' && $this->is_static === false) || $analysis_php_version_id >= 8_00_00;
}
public function replaceTemplateTypesWithArgTypes(
TemplateResult $template_result,
?Codebase $codebase
): void {
$this->replaceIntersectionTemplateTypesWithArgTypes($template_result, $codebase);
}
public function getChildNodes(): array
{
return $this->extra_types ?? [];
}
}