Skip to content

Commit 274ce36

Browse files
committed
Improve internal codebase
1 parent 63b36af commit 274ce36

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

src/Dictionary.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use function count;
1515
use function implode;
1616
use function is_array;
17+
use function is_iterable;
18+
use function is_string;
1719

1820
/**
1921
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.2
@@ -95,6 +97,8 @@ public static function fromPairs(iterable $pairs): self
9597
* Returns an instance from an HTTP textual representation.
9698
*
9799
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.2
100+
*
101+
* @throws SyntaxError If the string is not a valid
98102
*/
99103
public static function fromHttpValue(Stringable|string $httpValue): self
100104
{
@@ -191,7 +195,7 @@ public function hasPair(int $index): bool
191195
}
192196

193197
/**
194-
* Validates and Format the submitted index position.
198+
* Filters and format instance index.
195199
*/
196200
private function filterIndex(int $index): int
197201
{

src/InnerList.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use function array_splice;
1414
use function array_values;
1515
use function count;
16+
use function implode;
17+
use function is_int;
1618

1719
/**
1820
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1.1
@@ -49,6 +51,20 @@ public static function fromList(iterable $members = [], iterable $parameters = [
4951
return new self(Parameters::fromAssociative($parameters), $members);
5052
}
5153

54+
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Value
55+
{
56+
return match (true) {
57+
$member instanceof Value => $member,
58+
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Value::class.'" instance; received a "'.$member::class.'" instead.'),
59+
default => Item::from($member),
60+
};
61+
}
62+
63+
/**
64+
* Returns an instance from an HTTP textual representation.
65+
*
66+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1
67+
*/
5268
public static function fromHttpValue(Stringable|string $httpValue): self
5369
{
5470
return InnerList::fromList(...Parser::parseInnerList($httpValue));
@@ -119,7 +135,7 @@ public function hasMembers(): bool
119135
}
120136

121137
/**
122-
* @return Iterator<array-key, Value>
138+
* @return Iterator<int, Value>
123139
*/
124140
public function getIterator(): Iterator
125141
{
@@ -140,7 +156,9 @@ private function filterIndex(string|int $index): int|null
140156
$max = count($this->members);
141157

142158
return match (true) {
143-
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => null,
159+
[] === $this->members,
160+
0 > $max + $index,
161+
0 > $max - $index - 1 => null,
144162
0 > $index => $max + $index,
145163
default => $index,
146164
};
@@ -157,7 +175,7 @@ public function get(string|int $offset): Value
157175
}
158176

159177
/**
160-
* Insert members at the beginning of the list.
178+
* Inserts members at the beginning of the list.
161179
*/
162180
public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): static
163181
{
@@ -180,17 +198,8 @@ public function push(StructuredField|Token|ByteSequence|DateTimeInterface|String
180198
return new self($this->parameters, [...$this->members, ...array_map(self::filterMember(...), array_values($members))]);
181199
}
182200

183-
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Value
184-
{
185-
return match (true) {
186-
$member instanceof Value => $member,
187-
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Value::class.'" instance; received a "'.$member::class.'" instead.'),
188-
default => Item::from($member),
189-
};
190-
}
191-
192201
/**
193-
* Replace the member associated with the index.
202+
* Inserts members starting at the given index.
194203
*
195204
* @throws InvalidOffset If the index does not exist
196205
*/
@@ -224,7 +233,7 @@ public function replace(int $index, StructuredField|Token|ByteSequence|DateTimeI
224233
}
225234

226235
/**
227-
* Delete members associated with the list of instance indexes.
236+
* Deletes members associated with the list of instance indexes.
228237
*/
229238
public function remove(string|int ...$indexes): static
230239
{

src/OuterList.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function has(string|int $offset): bool
109109
return null !== $this->filterIndex($offset);
110110
}
111111

112-
private function filterIndex(int|string $index): int|null
112+
private function filterIndex(string|int $index): int|null
113113
{
114114
if (!is_int($index)) {
115115
return null;
@@ -182,11 +182,6 @@ public function insert(int $index, StructuredField|Token|ByteSequence|DateTimeIn
182182
};
183183
}
184184

185-
/**
186-
* Replaces the member associated with the index.
187-
*
188-
* @throws InvalidOffset If the index does not exist
189-
*/
190185
public function replace(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
191186
{
192187
if (null === ($offset = $this->filterIndex($index))) {
@@ -212,7 +207,7 @@ public function remove(string|int ...$indexes): static
212207
fn (int|null $index): bool => null !== $index
213208
);
214209

215-
if ($offsets === []) {
210+
if ([] === $offsets) {
216211
return $this;
217212
}
218213

src/Parameters.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_keys;
1313
use function array_map;
1414
use function count;
15+
use function implode;
1516
use function is_string;
1617
use function trim;
1718

@@ -74,7 +75,7 @@ public static function fromAssociative(iterable $members): self
7475
*
7576
* @param MemberOrderedMap<string, Value>|iterable<array{0:string, 1:Value|DataType}> $pairs
7677
*/
77-
public static function fromPairs(MemberOrderedMap|iterable $pairs): self
78+
public static function fromPairs(iterable $pairs): self
7879
{
7980
if ($pairs instanceof MemberOrderedMap) {
8081
$pairs = $pairs->toPairs();
@@ -144,8 +145,6 @@ public function getIterator(): Iterator
144145
}
145146

146147
/**
147-
* Returns an iterable construct of dictionary pairs.
148-
*
149148
* @return Iterator<array{0:string, 1:Value}>
150149
*/
151150
public function toPairs(): Iterator
@@ -156,27 +155,21 @@ public function toPairs(): Iterator
156155
}
157156

158157
/**
159-
* Returns all the container keys.
160-
*
161158
* @return array<string>
162159
*/
163160
public function keys(): array
164161
{
165162
return array_keys($this->members);
166163
}
167164

168-
/**
169-
* Tells whether the key is present in the container.
170-
*/
171165
public function has(string|int $offset): bool
172166
{
173167
return is_string($offset) && array_key_exists($offset, $this->members);
174168
}
175169

176170
/**
177-
* Returns the value associated to the key.
178-
*
179-
* @throws InvalidOffset if the key is not found
171+
* @throws SyntaxError If the key is invalid
172+
* @throws InvalidOffset If the key is not found
180173
*/
181174
public function get(string|int $offset): Value
182175
{
@@ -187,9 +180,6 @@ public function get(string|int $offset): Value
187180
return $this->members[$offset];
188181
}
189182

190-
/**
191-
* Tells whether the index is present in the container.
192-
*/
193183
public function hasPair(int $index): bool
194184
{
195185
try {
@@ -216,8 +206,6 @@ private function filterIndex(int $index): int
216206
}
217207

218208
/**
219-
* Returns the key-value pair found at a given index.
220-
*
221209
* @throws InvalidOffset if the index is not found
222210
*
223211
* @return array{0:string, 1:Value}
@@ -230,7 +218,7 @@ public function pair(int $index): array
230218
public function add(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
231219
{
232220
$members = $this->members;
233-
$members[$key] = self::filterMember($member);
221+
$members[MapKey::fromString($key)->value] = self::filterMember($member);
234222

235223
return new self($members);
236224
}
@@ -249,9 +237,6 @@ public function remove(string|int ...$keys): static
249237
return new self($members);
250238
}
251239

252-
/**
253-
* Adds a member at the end of the instance and deletes any previous reference to the key if present.
254-
*/
255240
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
256241
{
257242
$members = $this->members;
@@ -260,9 +245,6 @@ public function append(string $key, StructuredField|Token|ByteSequence|DateTimeI
260245
return new self([...$members, $key => self::filterMember($member)]);
261246
}
262247

263-
/**
264-
* Adds a member at the beginning of the instance and deletes any previous reference to the key if present.
265-
*/
266248
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
267249
{
268250
$members = $this->members;
@@ -272,8 +254,6 @@ public function prepend(string $key, StructuredField|Token|ByteSequence|DateTime
272254
}
273255

274256
/**
275-
* Merges multiple instances using iterable associative structures.
276-
*
277257
* @param iterable<string, Value|DataType> ...$others
278258
*/
279259
public function mergeAssociative(iterable ...$others): static
@@ -287,8 +267,6 @@ public function mergeAssociative(iterable ...$others): static
287267
}
288268

289269
/**
290-
* Merge multiple instances using iterable pairs.
291-
*
292270
* @param MemberOrderedMap<string, Value>|iterable<array{0:string, 1:Value|DataType}> ...$others
293271
*/
294272
public function mergePairs(MemberOrderedMap|iterable ...$others): static

0 commit comments

Comments
 (0)