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