55namespace TypeLang \Parser \Node \Literal ;
66
77/**
8- * @template TValue of string
9- * @template-extends LiteralNode<TValue>
8+ * @template-extends LiteralNode<string>
109 *
1110 * @psalm-consistent-constructor
12- * @psalm -consistent-templates
11+ * @phpstan -consistent-constructor
1312 */
1413class StringLiteralNode extends LiteralNode implements ParsableLiteralNodeInterface
1514{
@@ -36,27 +35,16 @@ class StringLiteralNode extends LiteralNode implements ParsableLiteralNodeInterf
3635 '\$ ' => "\$" ,
3736 ];
3837
39- /**
40- * @param TValue $value
41- */
4238 final public function __construct (
4339 public readonly string $ value ,
4440 string $ raw = null ,
4541 ) {
4642 parent ::__construct ($ raw ?? $ this ->value );
4743 }
4844
49- /**
50- * @param non-empty-string $value
51- *
52- * @return static<string>
53- * @psalm-suppress MoreSpecificImplementedParamType : Strengthening the
54- * precondition will violate the LSP, but in this case it is
55- * acceptable.
56- */
5745 public static function parse (string $ value ): static
5846 {
59- assert (\strlen ($ value ) >= 2 );
47+ assert (\strlen ($ value ) >= 2 , new \ InvalidArgumentException ( ' Could not parse non-quoted string ' ) );
6048
6149 if ($ value [0 ] === '" ' ) {
6250 return static ::createFromDoubleQuotedString ($ value );
@@ -67,29 +55,25 @@ public static function parse(string $value): static
6755
6856 /**
6957 * @param non-empty-string $value
70- *
71- * @return static<string>
7258 */
7359 public static function createFromDoubleQuotedString (string $ value ): static
7460 {
75- assert (\strlen ($ value ) >= 2 );
61+ assert (\strlen ($ value ) >= 2 , new \ InvalidArgumentException ( ' Could not parse non-quoted string ' ) );
7662
7763 $ body = \substr ($ value , 1 , -1 );
7864
79- return static ::parseEncodedValue (
65+ return self ::parseEncodedValue (
8066 string: \str_replace ('\" ' , '" ' , $ body ),
8167 raw: $ value ,
8268 );
8369 }
8470
8571 /**
8672 * @param non-empty-string $value
87- *
88- * @return static<string>
8973 */
9074 public static function createFromSingleQuotedString (string $ value ): static
9175 {
92- assert (\strlen ($ value ) >= 2 );
76+ assert (\strlen ($ value ) >= 2 , new \ InvalidArgumentException ( ' Could not parse non-quoted string ' ) );
9377
9478 $ body = \substr ($ value , 1 , -1 );
9579
@@ -146,7 +130,9 @@ private static function renderEscapeSequences(string $body): string
146130 */
147131 private static function renderHexadecimalSequences (string $ body ): string
148132 {
149- $ callee = static fn (array $ matches ): string => \chr (\hexdec ((string ) $ matches [1 ]));
133+ $ callee = static fn (array $ matches ): string
134+ => \chr ((int ) \hexdec ((string ) $ matches [1 ]))
135+ ;
150136
151137 /** @psalm-suppress InvalidArgument */
152138 return @\preg_replace_callback (self ::HEX_SEQUENCE_PATTERN , $ callee , $ body ) ?? $ body ;
@@ -161,11 +147,10 @@ private static function renderHexadecimalSequences(string $body): string
161147 private static function renderUtfSequences (string $ body ): string
162148 {
163149 $ callee = static function (array $ matches ): string {
164- $ code = \hexdec ((string ) $ matches [1 ]);
150+ $ code = ( int ) \hexdec ((string ) $ matches [1 ]);
165151
166- if (\function_exists ('\\mb_chr ' )
167- && ($ result = \mb_chr ($ code )) !== false
168- ) {
152+ // @phpstan-ignore-next-line : PHPStan false-positive mb_chr evaluation
153+ if (\function_exists ('\\mb_chr ' ) && ($ result = \mb_chr ($ code )) !== false ) {
169154 return $ result ;
170155 }
171156
0 commit comments