forked from vimeo/psalm
/
TLiteralString.php
58 lines (49 loc) · 1.34 KB
/
TLiteralString.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
<?php
namespace Psalm\Type\Atomic;
use function addcslashes;
use function mb_strlen;
use function mb_substr;
/**
* Denotes a string whose value is known.
*/
class TLiteralString extends TString
{
/** @var string */
public $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function getKey(bool $include_extra = true): string
{
return 'string(' . $this->value . ')';
}
public function getId(bool $exact = true, bool $nested = false): string
{
if (!$exact) {
return 'string';
}
// quote control characters, backslashes and double quote
$no_newline_value = addcslashes($this->value, "\0..\37\\\"");
if (mb_strlen($this->value) > 80) {
return "'" . mb_substr($no_newline_value, 0, 80) . '...' . "'";
}
return "'" . $no_newline_value . "'";
}
public function getAssertionString(): string
{
return 'string(' . $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 {
return $use_phpdoc_format ? 'string' : "'" . $this->value . "'";
}
}