@@ -14,17 +14,17 @@ class StringLiteralNode extends LiteralNode implements ParsableLiteralNodeInterf
1414 /**
1515 * @var non-empty-string
1616 */
17- private const UTF_SEQUENCE_PATTERN = '/(?<! \\\\) \\\\u\{([0-9a-fA-F]+)}/u ' ;
17+ private const string UTF_SEQUENCE_PATTERN = '/(?<! \\\\) \\\\u\{([0-9a-fA-F]+)}/u ' ;
1818
1919 /**
2020 * @var non-empty-string
2121 */
22- private const HEX_SEQUENCE_PATTERN = '/(?<! \\\\) \\\\x([0-9a-fA-F]{1,2})/iu ' ;
22+ private const string HEX_SEQUENCE_PATTERN = '/(?<! \\\\) \\\\x([0-9a-fA-F]{1,2})/iu ' ;
2323
2424 /**
2525 * @var non-empty-array<non-empty-string, non-empty-string>
2626 */
27- private const ESCAPED_CHARS = [
27+ private const array ESCAPED_CHARS = [
2828 '\n ' => "\n" ,
2929 '\r ' => "\r" ,
3030 '\t ' => "\t" ,
@@ -35,23 +35,19 @@ class StringLiteralNode extends LiteralNode implements ParsableLiteralNodeInterf
3535 ];
3636
3737 final public function __construct (
38- public readonly string $ value ,
38+ string $ value ,
3939 ?string $ raw = null ,
4040 ) {
41- parent ::__construct ($ raw ?? $ this ->value );
42- }
41+ $ raw ??= \sprintf ('"%s" ' , \addcslashes ($ value , '" ' ));
4342
44- public static function createFromValue (string $ value ): static
45- {
46- return new static (
47- value: $ value ,
48- raw: \sprintf ('"%s" ' , \addcslashes ($ value , '" ' )),
49- );
43+ parent ::__construct ($ value , $ raw );
5044 }
5145
5246 public static function parse (string $ value ): static
5347 {
54- assert (\strlen ($ value ) >= 2 , new \InvalidArgumentException ('Could not parse non-quoted string ' ));
48+ if (\strlen ($ value ) < 2 ) {
49+ throw new \InvalidArgumentException ('Could not parse non-quoted string ' );
50+ }
5551
5652 if ($ value [0 ] === '" ' ) {
5753 return static ::createFromDoubleQuotedString ($ value );
@@ -65,7 +61,9 @@ public static function parse(string $value): static
6561 */
6662 public static function createFromDoubleQuotedString (string $ value ): static
6763 {
68- assert (\strlen ($ value ) >= 2 , new \InvalidArgumentException ('Could not parse non-quoted string ' ));
64+ if (\strlen ($ value ) < 2 ) {
65+ throw new \InvalidArgumentException ('Could not parse non-quoted string ' );
66+ }
6967
7068 $ body = \substr ($ value , 1 , -1 );
7169
@@ -80,7 +78,9 @@ public static function createFromDoubleQuotedString(string $value): static
8078 */
8179 public static function createFromSingleQuotedString (string $ value ): static
8280 {
83- assert (\strlen ($ value ) >= 2 , new \InvalidArgumentException ('Could not parse non-quoted string ' ));
81+ if (\strlen ($ value ) < 2 ) {
82+ throw new \InvalidArgumentException ('Could not parse non-quoted string ' );
83+ }
8484
8585 $ body = \substr ($ value , 1 , -1 );
8686
@@ -163,24 +163,19 @@ private static function renderUtfSequences(string $body): string
163163
164164 if (0x800 > $ code ) {
165165 return \chr (0xC0 | $ code >> 6 )
166- . \chr (0x80 | $ code & 0x3F );
166+ . \chr (0x80 | $ code & 0x3F );
167167 }
168168
169169 if (0x10000 > $ code ) {
170170 return \chr (0xE0 | $ code >> 12 )
171- . \chr (0x80 | $ code >> 6 & 0x3F )
172- . \chr (0x80 | $ code & 0x3F );
171+ . \chr (0x80 | $ code >> 6 & 0x3F )
172+ . \chr (0x80 | $ code & 0x3F );
173173 }
174174
175175 return \chr (0xF0 | $ code >> 18 )
176- . \chr (0x80 | $ code >> 12 & 0x3F )
177- . \chr (0x80 | $ code >> 6 & 0x3F )
178- . \chr (0x80 | $ code & 0x3F );
176+ . \chr (0x80 | $ code >> 12 & 0x3F )
177+ . \chr (0x80 | $ code >> 6 & 0x3F )
178+ . \chr (0x80 | $ code & 0x3F );
179179 }, $ body ) ?? $ body ;
180180 }
181-
182- public function getValue (): string
183- {
184- return $ this ->value ;
185- }
186181}
0 commit comments