/
TLiteralClassString.php
106 lines (88 loc) · 2.51 KB
/
TLiteralClassString.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
<?php
namespace Psalm\Type\Atomic;
use function preg_quote;
use function preg_replace;
use function stripos;
use function strpos;
use function strtolower;
/**
* Denotes a specific class string, generated by expressions like `A::class`.
*/
class TLiteralClassString extends TLiteralString
{
/**
* Whether or not this type can represent a child of the class named in $value
* @var bool
*/
public $definite_class = false;
public function __construct(string $value, bool $definite_class = false)
{
parent::__construct($value);
$this->definite_class = $definite_class;
}
public function __toString(): string
{
return 'class-string';
}
public function getKey(bool $include_extra = true): string
{
return 'class-string(' . $this->value . ')';
}
/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): string {
return 'string';
}
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
}
public function getId(bool $nested = false): string
{
return $this->value . '::class';
}
public function getAssertionString()
/** @psalm-mutation-free */: string
{
return $this->getKey();
}
/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
if ($use_phpdoc_format) {
return 'string';
}
if ($this->value === 'static') {
return 'static::class';
}
if ($this->value === $this_class) {
return 'self::class';
}
if ($namespace && stripos($this->value, $namespace . '\\') === 0) {
return preg_replace(
'/^' . preg_quote($namespace . '\\') . '/i',
'',
$this->value
) . '::class';
}
if (!$namespace && strpos($this->value, '\\') === false) {
return $this->value . '::class';
}
if (isset($aliased_classes[strtolower($this->value)])) {
return $aliased_classes[strtolower($this->value)] . '::class';
}
return '\\' . $this->value . '::class';
}
}