Skip to content

Commit 7e26b10

Browse files
committed
Improve internal codebase
1 parent 842c851 commit 7e26b10

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/Item.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ private static function filterInteger(int $value): int
101101
*/
102102
public static function fromHttpValue(string $httpValue): self
103103
{
104-
$httpValue = trim($httpValue, ' ');
104+
$itemString = trim($httpValue, ' ');
105+
105106
[$value, $parameters] = match (true) {
106-
'' === $httpValue,
107-
1 === preg_match("/[\r\t\n]/", $httpValue),
108-
1 === preg_match("/[^\x20-\x7E]/", $httpValue) => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item contains invalid characters."),
109-
1 === preg_match('/^(-?[0-9])/', $httpValue) => self::parseNumber($httpValue),
110-
'"' === $httpValue[0] => self::parseString($httpValue),
111-
':' === $httpValue[0] => self::parseBytesSequence($httpValue),
112-
'?' === $httpValue[0] => self::parseBoolean($httpValue),
113-
1 === preg_match('/^([a-z*])/i', $httpValue) => self::parseToken($httpValue),
107+
1 === preg_match("/[\r\t\n]|[^\x20-\x7E]/", $itemString),
108+
'' === $itemString => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item contains invalid characters."),
109+
'"' === $itemString[0] => self::parseString($itemString),
110+
':' === $itemString[0] => self::parseBytesSequence($itemString),
111+
'?' === $itemString[0] => self::parseBoolean($itemString),
112+
1 === preg_match('/^(-?[0-9])/', $itemString) => self::parseNumber($itemString),
113+
1 === preg_match('/^([a-z*])/i', $itemString) => self::parseToken($itemString),
114114
default => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item is unknown or unsupported."),
115115
};
116116

src/Parser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use function ltrim;
99
use function ord;
1010
use function preg_match;
11-
use function preg_quote;
1211
use function strlen;
1312
use function substr;
1413

@@ -211,11 +210,10 @@ private static function parseInnerListValue(string $httpValue): array
211210
private static function parseBareItem(string $httpValue): array
212211
{
213212
return match (true) {
214-
'' === $httpValue => throw new SyntaxError('Unexpected empty string for The HTTP textual representation of an item.'),
215-
1 === preg_match('/^(-|\d)/', $httpValue) => self::parseNumber($httpValue),
216213
'"' === $httpValue[0] => self::parseString($httpValue),
217214
':' === $httpValue[0] => self::parseByteSequence($httpValue),
218215
'?' === $httpValue[0] => self::parseBoolean($httpValue),
216+
1 === preg_match('/^(-|\d)/', $httpValue) => self::parseNumber($httpValue),
219217
1 === preg_match('/^([a-z*])/i', $httpValue) => self::parseToken($httpValue),
220218
default => throw new SyntaxError('Unknown or unsupported string for The HTTP textual representation of an item.'),
221219
};
@@ -353,7 +351,7 @@ private static function parseString(string $httpValue): array
353351
*/
354352
private static function parseToken(string $httpValue): array
355353
{
356-
preg_match('/^(?<token>[a-z*][a-z0-9:\/'.preg_quote("!#$%&'*+-.^_`|~").']*)/i', $httpValue, $found);
354+
preg_match("/^(?<token>[a-z*][a-z0-9:\/\!\#\$%&'\*\+\-\.\^_`\|~]*)/i", $httpValue, $found);
357355

358356
return [Token::fromString($found['token']), strlen($found['token'])];
359357
}

0 commit comments

Comments
 (0)