1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Node\Scalar;
4:
5: use PhpParser\Error;
6: use PhpParser\Node\Scalar;
7:
8: class String_ extends Scalar {
9: /* For use in "kind" attribute */
10: public const KIND_SINGLE_QUOTED = 1;
11: public const KIND_DOUBLE_QUOTED = 2;
12: public const KIND_HEREDOC = 3;
13: public const KIND_NOWDOC = 4;
14:
15: /** @var string String value */
16: public string $value;
17:
18: /** @var array<string, string> Escaped character to its decoded value */
19: protected static array $replacements = [
20: '\\' => '\\',
21: '$' => '$',
22: 'n' => "\n",
23: 'r' => "\r",
24: 't' => "\t",
25: 'f' => "\f",
26: 'v' => "\v",
27: 'e' => "\x1B",
28: ];
29:
30: /**
31: * Constructs a string scalar node.
32: *
33: * @param string $value Value of the string
34: * @param array<string, mixed> $attributes Additional attributes
35: */
36: public function __construct(string $value, array $attributes = []) {
37: $this->attributes = $attributes;
38: $this->value = $value;
39: }
40:
41: public function getSubNodeNames(): array {
42: return ['value'];
43: }
44:
45: /**
46: * @param array<string, mixed> $attributes
47: * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
48: */
49: public static function fromString(string $str, array $attributes = [], bool $parseUnicodeEscape = true): self {
50: $attributes['kind'] = ($str[0] === "'" || ($str[1] === "'" && ($str[0] === 'b' || $str[0] === 'B')))
51: ? Scalar\String_::KIND_SINGLE_QUOTED
52: : Scalar\String_::KIND_DOUBLE_QUOTED;
53:
54: $attributes['rawValue'] = $str;
55:
56: $string = self::parse($str, $parseUnicodeEscape);
57:
58: return new self($string, $attributes);
59: }
60:
61: /**
62: * @internal
63: *
64: * Parses a string token.
65: *
66: * @param string $str String token content
67: * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
68: *
69: * @return string The parsed string
70: */
71: public static function parse(string $str, bool $parseUnicodeEscape = true): string {
72: $bLength = 0;
73: if ('b' === $str[0] || 'B' === $str[0]) {
74: $bLength = 1;
75: }
76:
77: $quote = $str[$bLength];
78: $str = substr($str, $bLength + 1, -1);
79:
80: if ('\'' === $quote) {
81: // Fast path: a string without any backslash contains no escape sequences.
82: if (false === strpos($str, '\\')) {
83: return $str;
84: }
85:
86: return str_replace(
87: ['\\\\', '\\\''],
88: ['\\', '\''],
89: $str
90: );
91: }
92:
93: return self::parseEscapeSequences($str, '"', $parseUnicodeEscape);
94: }
95:
96: /**
97: * @internal
98: *
99: * Parses escape sequences in strings (all string types apart from single quoted).
100: *
101: * @param string $str String without quotes
102: * @param null|string $quote Quote type
103: * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
104: *
105: * @return string String with escape sequences parsed
106: */
107: public static function parseEscapeSequences(string $str, ?string $quote, bool $parseUnicodeEscape = true): string {
108: // Fast path: a string without any backslash contains no escape sequences.
109: if (false === strpos($str, '\\')) {
110: return $str;
111: }
112:
113: if (null !== $quote) {
114: $str = str_replace('\\' . $quote, $quote, $str);
115: }
116:
117: $extra = '';
118: if ($parseUnicodeEscape) {
119: $extra = '|u\{([0-9a-fA-F]+)\}|u\{';
120: }
121:
122: return preg_replace_callback(
123: '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
124: function ($matches) {
125: $str = $matches[1];
126:
127: if (isset(self::$replacements[$str])) {
128: return self::$replacements[$str];
129: }
130: if ('x' === $str[0] || 'X' === $str[0]) {
131: return chr(hexdec(substr($str, 1)));
132: }
133: if ('u' === $str[0]) {
134: if (!isset($matches[2])) {
135: throw new Error('Invalid UTF-8 codepoint escape sequence');
136: }
137: // PHP 8.6 warns when hexdec() produces a float.
138: $dec = @hexdec($matches[2]);
139: // If it overflowed to float, treat as INT_MAX, it will throw an error anyway.
140: return self::codePointToUtf8(\is_int($dec) ? $dec : \PHP_INT_MAX);
141: } else {
142: return chr(octdec($str) & 255);
143: }
144: },
145: $str
146: );
147: }
148:
149: /**
150: * Converts a Unicode code point to its UTF-8 encoded representation.
151: *
152: * @param int $num Code point
153: *
154: * @return string UTF-8 representation of code point
155: */
156: private static function codePointToUtf8(int $num): string {
157: if ($num <= 0x7F) {
158: return chr($num);
159: }
160: if ($num <= 0x7FF) {
161: return chr(($num >> 6) + 0xC0) . chr(($num & 0x3F) + 0x80);
162: }
163: if ($num <= 0xFFFF) {
164: return chr(($num >> 12) + 0xE0) . chr((($num >> 6) & 0x3F) + 0x80) . chr(($num & 0x3F) + 0x80);
165: }
166: if ($num <= 0x10FFFF) {
167: return chr(($num >> 18) + 0xF0) . chr((($num >> 12) & 0x3F) + 0x80)
168: . chr((($num >> 6) & 0x3F) + 0x80) . chr(($num & 0x3F) + 0x80);
169: }
170: throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
171: }
172:
173: public function getType(): string {
174: return 'Scalar_String';
175: }
176: }
177: