Skip to content

Commit 7b16f3d

Browse files
committed
Normalize variable name and SyntaxError messages
1 parent 93f41dc commit 7b16f3d

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

src/Dictionary.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ public function __construct(iterable $elements = [])
2727
}
2828
}
2929

30-
public static function fromHttpValue(string $field): self
30+
public static function fromHttpValue(string $httpValue): self
3131
{
3232
$instance = new self();
33-
$field = trim($field, ' ');
34-
if ('' === $field) {
33+
$httpValue = trim($httpValue, ' ');
34+
if ('' === $httpValue) {
3535
return $instance;
3636
}
3737

38-
if (1 === preg_match("/[^\x20-\x7E\t]/", $field) || str_starts_with($field, "\t")) {
39-
throw new SyntaxError("Dictionary field `$field` contains invalid characters.");
38+
if (1 === preg_match("/[^\x20-\x7E\t]/", $httpValue) || str_starts_with($httpValue, "\t")) {
39+
throw new SyntaxError("The HTTP textual representation `$httpValue` for dictionary contains invalid characters.");
4040
}
4141

4242
$parser = fn (string $element): Item|InnerList => str_starts_with($element, '(')
4343
? InnerList::fromHttpValue($element)
4444
: Item::fromHttpValue($element);
4545

46-
return array_reduce(explode(',', $field), function (self $instance, string $element) use ($parser): self {
46+
return array_reduce(explode(',', $httpValue), function (self $instance, string $element) use ($parser): self {
4747
[$key, $value] = self::extractPair($element);
4848

4949
$instance->set($key, $parser($value));
@@ -66,11 +66,11 @@ private static function extractPair(string $element): array
6666
}
6767

6868
if (1 !== preg_match('/^(?<key>[a-z*][a-z0-9.*_-]*)(=)?(?<value>.*)/', $element, $found)) {
69-
throw new SyntaxError("Dictionary pair `$element` contains invalid characters.");
69+
throw new SyntaxError("The HTTP textual representation `$element` for a dictionary pair contains invalid characters.");
7070
}
7171

7272
if (rtrim($found['key']) !== $found['key'] || ltrim($found['value']) !== $found['value']) {
73-
throw new SyntaxError("Dictionary pair `$element` contains invalid characters.");
73+
throw new SyntaxError("The HTTP textual representation `$element` for a dictionary pair contains invalid characters.");
7474
}
7575

7676
$found['value'] = trim($found['value']);
@@ -161,7 +161,7 @@ public function set(string $key, InnerList|Item|ByteSequence|Token|bool|int|floa
161161
private static function validateKey(string $key): void
162162
{
163163
if (1 !== preg_match('/^[a-z*][a-z0-9.*_-]*$/', $key)) {
164-
throw new SyntaxError("Key `$key` contains invalid characters.");
164+
throw new SyntaxError("The dictionary key `$key` contains invalid characters.");
165165
}
166166
}
167167

src/InnerList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ public function __construct(iterable $elements = [], iterable $parameters = [])
3131
$this->parameters = $parameters instanceof Parameters ? $parameters : Parameters::fromItems($parameters);
3232
}
3333

34-
public static function fromHttpValue(string $field): self
34+
public static function fromHttpValue(string $httpValue): self
3535
{
36-
$field = trim($field);
36+
$field = trim($httpValue);
3737

3838
if (1 !== preg_match("/^\((?<content>.*)\)(?<parameters>[^,]*)/", $field, $found)) {
39-
throw new SyntaxError("InnerList field `$field` contains invalid characters.");
39+
throw new SyntaxError("InnerList field `$httpValue` contains invalid characters.");
4040
}
4141

4242
if ('' !== $found['parameters'] && !str_starts_with($found['parameters'], ';')) {
43-
throw new SyntaxError("InnerList field `$field` contains invalid characters.");
43+
throw new SyntaxError("InnerList field `$httpValue` contains invalid characters.");
4444
}
4545

4646
/** @var string $content */

src/Item.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ private static function filterInteger(int $value): int
5454
return $value;
5555
}
5656

57-
public static function fromHttpValue(string $field): self
57+
public static function fromHttpValue(string $httpValue): self
5858
{
59-
$field = trim($field, ' ');
59+
$httpValue = trim($httpValue, ' ');
6060
[$value, $parameters] = match (true) {
61-
$field === '',
62-
1 === preg_match("/[\r\t\n]/", $field),
63-
1 === preg_match("/[^\x20-\x7E]/", $field) => throw new SyntaxError("Item field `$field` contains invalid characters."),
64-
1 === preg_match('/^(-?[0-9])/', $field) => self::parseNumber($field),
65-
$field[0] == '"' => self::parseString($field),
66-
$field[0] == ':' => self::parseBytesSequence($field),
67-
$field[0] == '?' => self::parseBoolean($field),
68-
1 === preg_match('/^([a-z*])/i', $field) => self::parseToken($field),
69-
default => throw new SyntaxError("Item field `$field` is unknown or unsupported."),
61+
$httpValue === '',
62+
1 === preg_match("/[\r\t\n]/", $httpValue),
63+
1 === preg_match("/[^\x20-\x7E]/", $httpValue) => throw new SyntaxError("Item field `$httpValue` contains invalid characters."),
64+
1 === preg_match('/^(-?[0-9])/', $httpValue) => self::parseNumber($httpValue),
65+
$httpValue[0] == '"' => self::parseString($httpValue),
66+
$httpValue[0] == ':' => self::parseBytesSequence($httpValue),
67+
$httpValue[0] == '?' => self::parseBoolean($httpValue),
68+
1 === preg_match('/^([a-z*])/i', $httpValue) => self::parseToken($httpValue),
69+
default => throw new SyntaxError("Item field `$httpValue` is unknown or unsupported."),
7070
};
7171

7272
return new self($value, Parameters::fromHttpValue($parameters));

src/OrderedList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function __construct(iterable $elements = [])
2727
}
2828
}
2929

30-
public static function fromHttpValue(string $field): self
30+
public static function fromHttpValue(string $httpValue): self
3131
{
32-
$field = trim($field, ' ');
33-
if ('' === $field) {
32+
$httpValue = trim($httpValue, ' ');
33+
if ('' === $httpValue) {
3434
return new self();
3535
}
3636

@@ -40,7 +40,7 @@ public static function fromHttpValue(string $field): self
4040
return $carry;
4141
};
4242

43-
return array_reduce(explode(',', $field), $reducer, new self());
43+
return array_reduce(explode(',', $httpValue), $reducer, new self());
4444
}
4545

4646
private static function parseItemOrInnerList(string $element): Item|InnerList

src/Parameters.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ private static function filterElement(Item|ByteSequence|Token|bool|int|float|str
4848
};
4949
}
5050

51-
public static function fromHttpValue(string $field): self
51+
public static function fromHttpValue(string $httpValue): self
5252
{
53-
$instance = new self();
54-
if ('' === $field) {
55-
return $instance;
53+
$parameters = new self();
54+
if ('' === $httpValue) {
55+
return $parameters;
5656
}
5757

58-
$parameters = new self();
59-
foreach (explode(';', $field) as $pair) {
58+
foreach (explode(';', $httpValue) as $pair) {
6059
[$key, $value] = explode('=', $pair, 2) + [1 => '?1'];
6160
if (rtrim($key) !== $key || ltrim($value) !== $value) {
62-
throw new SyntaxError("Invalid parameter pair: `$field`.");
61+
throw new SyntaxError("The HTTP textual representation `$pair` for a parameter pair contains invalid characters.");
6362
}
6463

6564
$key = trim($key);
@@ -145,7 +144,7 @@ public function toHttpValue(): string
145144

146145
foreach ($this->elements as $key => $val) {
147146
if (!$val->parameters()->isEmpty()) {
148-
throw new SyntaxError('the Item cannot be parameterized.');
147+
throw new SyntaxError('Parameters instances can not contain parameterized Items.');
149148
}
150149

151150
$value = ';'.$key;
@@ -170,11 +169,11 @@ public function set(string $key, Item|ByteSequence|Token|bool|int|float|string $
170169
private static function validate(string $key, Item $item): void
171170
{
172171
if (1 !== preg_match('/^[a-z*][a-z0-9.*_-]*$/', $key)) {
173-
throw new SyntaxError('Invalid characters in key: `'.$key.'`');
172+
throw new SyntaxError("The Parameters key `$key` contains invalid characters.");
174173
}
175174

176175
if (!$item->parameters()->isEmpty()) {
177-
throw new SyntaxError('the Item cannot be parameterized.');
176+
throw new SyntaxError('Parameters instances can not contain parameterized Items.');
178177
}
179178
}
180179

0 commit comments

Comments
 (0)