1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\PhpDocParser\Ast\ConstExpr; |
4: | |
5: | use PHPStan\PhpDocParser\Ast\NodeAttributes; |
6: | use function addcslashes; |
7: | use function assert; |
8: | use function dechex; |
9: | use function ord; |
10: | use function preg_replace_callback; |
11: | use function sprintf; |
12: | use function str_pad; |
13: | use function strlen; |
14: | use const STR_PAD_LEFT; |
15: | |
16: | class ConstExprStringNode implements ConstExprNode |
17: | { |
18: | |
19: | public const SINGLE_QUOTED = 1; |
20: | public const DOUBLE_QUOTED = 2; |
21: | |
22: | use NodeAttributes; |
23: | |
24: | public string $value; |
25: | |
26: | |
27: | public $quoteType; |
28: | |
29: | |
30: | |
31: | |
32: | public function __construct(string $value, int $quoteType) |
33: | { |
34: | $this->value = $value; |
35: | $this->quoteType = $quoteType; |
36: | } |
37: | |
38: | |
39: | public function __toString(): string |
40: | { |
41: | if ($this->quoteType === self::SINGLE_QUOTED) { |
42: | |
43: | return sprintf("'%s'", addcslashes($this->value, '\'\\')); |
44: | } |
45: | |
46: | |
47: | return sprintf('"%s"', $this->escapeDoubleQuotedString()); |
48: | } |
49: | |
50: | private function escapeDoubleQuotedString(): string |
51: | { |
52: | $quote = '"'; |
53: | $escaped = addcslashes($this->value, "\n\r\t\f\v$" . $quote . '\\'); |
54: | |
55: | |
56: | |
57: | $regex = '/( |
58: | [\x00-\x08\x0E-\x1F] # Control characters |
59: | | [\xC0-\xC1] # Invalid UTF-8 Bytes |
60: | | [\xF5-\xFF] # Invalid UTF-8 Bytes |
61: | | \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point |
62: | | \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point |
63: | | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start |
64: | | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start |
65: | | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start |
66: | | (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle |
67: | | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence |
68: | | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence |
69: | | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence |
70: | | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) |
71: | )/x'; |
72: | return preg_replace_callback($regex, static function ($matches) { |
73: | assert(strlen($matches[0]) === 1); |
74: | $hex = dechex(ord($matches[0])); |
75: | |
76: | return '\\x' . str_pad($hex, 2, '0', STR_PAD_LEFT); |
77: | }, $escaped); |
78: | } |
79: | |
80: | } |
81: | |