Skip to content

Commit 3ec261a

Browse files
committed
Improve parameters invalid state check
1 parent 3418440 commit 3ec261a

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/Parameters.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use IteratorAggregate;
1010
use function array_key_exists;
1111
use function array_keys;
12-
use function array_values;
1312
use function count;
1413
use function explode;
1514
use function ltrim;
@@ -24,7 +23,7 @@ final class Parameters implements Countable, IteratorAggregate, StructuredField
2423
{
2524
private function __construct(
2625
/** @var array<string, Item> */
27-
private array $members = []
26+
private array $members
2827
) {
2928
}
3029

@@ -46,7 +45,7 @@ public static function __set_state(array $properties): self
4645
*/
4746
public static function fromAssociative(iterable $members = []): self
4847
{
49-
$instance = new self();
48+
$instance = new self([]);
5049
foreach ($members as $key => $member) {
5150
$instance->set($key, $member);
5251
}
@@ -65,7 +64,7 @@ public static function fromAssociative(iterable $members = []): self
6564
*/
6665
public static function fromPairs(iterable $pairs = []): self
6766
{
68-
$instance = new self();
67+
$instance = new self([]);
6968
foreach ($pairs as [$key, $member]) {
7069
$instance->set($key, $member);
7170
}
@@ -80,7 +79,7 @@ public static function fromPairs(iterable $pairs = []): self
8079
*/
8180
public static function fromHttpValue(string $httpValue): self
8281
{
83-
$instance = new self();
82+
$instance = new self([]);
8483
if ('' === $httpValue) {
8584
return $instance;
8685
}
@@ -105,9 +104,7 @@ public function toHttpValue(): string
105104
$returnValue = [];
106105

107106
foreach ($this->members as $key => $val) {
108-
if (!$val->parameters->isEmpty()) {
109-
throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.');
110-
}
107+
$val = $this->validateMember($val);
111108

112109
$value = ';'.$key;
113110
if ($val->value !== true) {
@@ -120,6 +117,15 @@ public function toHttpValue(): string
120117
return implode('', $returnValue);
121118
}
122119

120+
private function validateMember(Item $item): Item
121+
{
122+
if (!$item->parameters->isEmpty()) {
123+
throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.');
124+
}
125+
126+
return $item;
127+
}
128+
123129
public function count(): int
124130
{
125131
return count($this->members);
@@ -138,8 +144,8 @@ public function isEmpty(): bool
138144
*/
139145
public function getIterator(): Iterator
140146
{
141-
foreach ($this->members as $key => $value) {
142-
yield $key => $value;
147+
foreach ($this->members as $key => $member) {
148+
yield $key => $this->validateMember($member);
143149
}
144150
}
145151

@@ -151,7 +157,7 @@ public function getIterator(): Iterator
151157
public function toPairs(): Iterator
152158
{
153159
foreach ($this->members as $index => $member) {
154-
yield [$index, $member];
160+
yield [$index, $this->validateMember($member)];
155161
}
156162
}
157163

@@ -172,7 +178,7 @@ public function keys(): array
172178
*/
173179
public function values(): array
174180
{
175-
return array_map(fn (Item $item): Token|ByteSequence|float|int|bool|string => $item->value, $this->members);
181+
return array_map(fn (Item $item): Token|ByteSequence|float|int|bool|string => $this->validateMember($item)->value, $this->members);
176182
}
177183

178184
/**
@@ -198,12 +204,7 @@ public function get(string $key): Item
198204
throw InvalidOffset::dueToKeyNotFound($key);
199205
}
200206

201-
$item = $this->members[$key];
202-
if (!$item->parameters->isEmpty()) {
203-
throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.');
204-
}
205-
206-
return $item;
207+
return $this->validateMember($this->members[$key]);
207208
}
208209

209210
/**
@@ -217,12 +218,7 @@ public function value(string $key): Token|ByteSequence|float|int|bool|string|nul
217218
return null;
218219
}
219220

220-
$item = $this->members[$key];
221-
if (!$item->parameters->isEmpty()) {
222-
throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.');
223-
}
224-
225-
return $item->value;
221+
return $this->validateMember($this->members[$key])->value;
226222
}
227223

228224
/**
@@ -259,12 +255,15 @@ public function pair(int $index): array
259255
throw InvalidOffset::dueToIndexNotFound($index);
260256
}
261257

262-
$item = array_values($this->members)[$offset];
263-
if (!$item->parameters->isEmpty()) {
264-
throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.');
258+
foreach ($this->toPairs() as $k => $pair) {
259+
if ($k === $offset) {
260+
return $pair;
261+
}
265262
}
266263

267-
return [array_keys($this->members)[$offset], $item];
264+
// @codeCoverageIgnoreStart
265+
throw InvalidOffset::dueToIndexNotFound($index);
266+
// @codeCoverageIgnoreEnd
268267
}
269268

270269
/**

src/ParametersTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,14 @@ public function it_can_return_bare_items_values(): void
270270
self::assertSame('helloWorld', $instance->value('string'));
271271
self::assertSame(['string' => 'helloWorld', 'boolean' => true], $instance->values());
272272
}
273+
274+
/**
275+
* @test
276+
*/
277+
public function it_fails_to_parse_invalid_parameters_pairs(): void
278+
{
279+
$this->expectException(SyntaxError::class);
280+
281+
Parameters::fromHttpValue(';foo = bar');
282+
}
273283
}

0 commit comments

Comments
 (0)