@@ -23,8 +23,8 @@ public static function parseDictionary(string $string): Dictionary
23
23
while (true ) {
24
24
$ key = self ::parseKey ($ input );
25
25
26
- if (! $ input ->empty () && $ input -> getChar () === '= ' ) {
27
- $ input ->consumeChar (' = ' );
26
+ if ($ input ->isChar ( '= ' ) ) {
27
+ $ input ->consumeChar ();
28
28
$ value ->{$ key } = self ::parseItemOrInnerList ($ input );
29
29
} else {
30
30
// Bare boolean true value.
@@ -88,7 +88,7 @@ public static function parseList(string $string): OuterList
88
88
89
89
private static function parseItemOrInnerList (ParsingInput $ input ): TupleInterface
90
90
{
91
- if ($ input ->getChar () === '( ' ) {
91
+ if ($ input ->isChar ( '( ' ) ) {
92
92
return self ::parseInnerList ($ input );
93
93
} else {
94
94
return self ::doParseItem ($ input );
@@ -107,8 +107,8 @@ private static function parseInnerList(ParsingInput $input): InnerList
107
107
while (!$ input ->empty ()) {
108
108
$ input ->trim ();
109
109
110
- if ($ input ->getChar () === ') ' ) {
111
- $ input ->consumeChar (' ) ' );
110
+ if ($ input ->isChar ( ') ' ) ) {
111
+ $ input ->consumeChar ();
112
112
return new InnerList (
113
113
$ value ,
114
114
self ::parseParameters ($ input )
@@ -117,7 +117,10 @@ private static function parseInnerList(ParsingInput $input): InnerList
117
117
118
118
$ value [] = self ::doParseItem ($ input );
119
119
120
- if (!$ input ->empty () && !in_array ($ input ->getChar (), [' ' , ') ' ])) {
120
+ if (!($ input ->isChar (' ' ) || $ input ->isChar (') ' ))) {
121
+ if ($ input ->empty ()) {
122
+ break ;
123
+ }
121
124
throw new ParseException ('Unexpected character in inner list at position ' . $ input ->position ());
122
125
}
123
126
}
@@ -153,6 +156,8 @@ public static function parseItem(string $string): Item
153
156
/**
154
157
* Internal implementation of parseItem that doesn't fail if input string
155
158
* has remaining characters after parsing.
159
+ *
160
+ * @phpstan-impure
156
161
*/
157
162
private static function doParseItem (ParsingInput $ input ): Item
158
163
{
@@ -171,13 +176,13 @@ private static function parseBareItem(ParsingInput $input): mixed
171
176
{
172
177
$ char = $ input ->getChar ();
173
178
return match (true ) {
174
- preg_match ('/(-|\d)/ ' , $ char ) == 1 => self ::parseNumber ($ input ),
175
- '" ' === $ char => self ::parseString ($ input ),
176
- preg_match ('/[a-z*]/i ' , $ char ) == 1 => self ::parseToken ($ input ),
177
- ': ' === $ char => self ::parseByteSequence ($ input ),
178
- '? ' === $ char => self ::parseBoolean ($ input ),
179
- '@ ' === $ char => self ::parseDate ($ input ),
180
- '% ' === $ char => self ::parseDisplayString ($ input ),
179
+ preg_match ('/(-|\d)/ ' , $ char ) === 1 => self ::parseNumber ($ input ),
180
+ '" ' === $ char => self ::parseString ($ input ),
181
+ preg_match ('/[a-z*]/i ' , $ char ) === 1 => self ::parseToken ($ input ),
182
+ ': ' === $ char => self ::parseByteSequence ($ input ),
183
+ '? ' === $ char => self ::parseBoolean ($ input ),
184
+ '@ ' === $ char => self ::parseDate ($ input ),
185
+ '% ' === $ char => self ::parseDisplayString ($ input ),
181
186
default => throw new ParseException ('Unknown item type at position ' . $ input ->position ()),
182
187
};
183
188
}
@@ -188,15 +193,15 @@ private static function parseBareItem(ParsingInput $input): mixed
188
193
private static function parseParameters (ParsingInput $ input ): Parameters
189
194
{
190
195
$ parameters = new Parameters ();
191
- while (! $ input ->empty () && $ input -> getChar () === '; ' ) {
192
- $ input ->consumeChar (' ; ' );
196
+ while ($ input ->isChar ( '; ' ) ) {
197
+ $ input ->consumeChar ();
193
198
$ input ->trim ();
194
199
195
200
$ key = self ::parseKey ($ input );
196
201
$ parameters ->{$ key } = true ;
197
202
198
- if (! $ input ->empty () && $ input -> getChar () === '= ' ) {
199
- $ input ->consumeChar (' = ' );
203
+ if ($ input ->isChar ( '= ' ) ) {
204
+ $ input ->consumeChar ();
200
205
$ parameters ->{$ key } = self ::parseBareItem ($ input );
201
206
}
202
207
}
@@ -221,12 +226,12 @@ private static function parseKey(ParsingInput $input): string
221
226
*/
222
227
private static function parseBoolean (ParsingInput $ input ): bool
223
228
{
224
- try {
225
- $ input ->consumeChar (' ? ' );
226
- return ' 1 ' === $ input -> consumeRegex ( ' /^[01]/ ' );
227
- } catch ( \ RuntimeException ) {
228
- throw new ParseException ('Invalid boolean at position ' . $ input ->position ());
229
- }
229
+ $ input -> consumeChar ( ' ? ' );
230
+ return match ( $ input ->consumeChar ()) {
231
+ ' 0 ' => false ,
232
+ ' 1 ' => true ,
233
+ default => throw new ParseException ('Invalid boolean at position ' . $ input ->position ()),
234
+ };
230
235
}
231
236
232
237
/**
@@ -266,12 +271,12 @@ private static function parseString(ParsingInput $input): string
266
271
}
267
272
268
273
$ char = $ input ->consumeChar ();
269
- if ($ char != '" ' && $ char != '\\' ) {
274
+ if ($ char !== '" ' && $ char != = '\\' ) {
270
275
throw new ParseException (
271
276
'Invalid escaped character in string at position ' . ($ input ->position () - 1 )
272
277
);
273
278
}
274
- } elseif ($ char == '" ' ) {
279
+ } elseif ($ char === '" ' ) {
275
280
return $ output ;
276
281
} elseif (ord ($ char ) <= 0x1f || ord ($ char ) >= 0x7f ) {
277
282
throw new ParseException ('Invalid character in string at position ' . ($ input ->position () - 1 ));
@@ -304,15 +309,15 @@ private static function parseDisplayString(ParsingInput $string): DisplayString
304
309
throw new ParseException (
305
310
'Invalid character in display string at position ' . ($ string ->position () - 1 )
306
311
);
307
- } elseif ($ char == '% ' ) {
312
+ } elseif ($ char === '% ' ) {
308
313
try {
309
314
$ encoded_string .= '% ' . $ string ->consumeRegex ('/^[0-9a-f]{2}/ ' );
310
315
} catch (\RuntimeException ) {
311
316
throw new ParseException (
312
317
'Invalid hex values in display string at position ' . ($ string ->position () - 1 )
313
318
);
314
319
}
315
- } elseif ($ char == '" ' ) {
320
+ } elseif ($ char === '" ' ) {
316
321
$ display_string = new DisplayString (rawurldecode ($ encoded_string ));
317
322
// An invalid UTF-8 subject will cause the preg_* function to match nothing.
318
323
// @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
0 commit comments