Skip to content

Commit 5817211

Browse files
committed
Improve internal codebase
1 parent 46e6a70 commit 5817211

File tree

8 files changed

+53
-90
lines changed

8 files changed

+53
-90
lines changed

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ parameters:
99
- src
1010
ignoreErrors:
1111
reportUnmatchedIgnoredErrors: true
12-
typeAliases:
13-
DataType: '\Bakame\Http\StructuredFields\ByteSequence|\Bakame\Http\StructuredFields\Token|\DateTimeInterface|\Stringable|string|int|float|bool'

src/Dictionary.php

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
/**
1818
* @implements MemberOrderedMap<string, Item|InnerList<int, Item>>
19+
* @phpstan-import-type DataType from Item
1920
*/
2021
final class Dictionary implements MemberOrderedMap
2122
{
2223
/** @var array<string, Item|InnerList<int, Item>> */
2324
private array $members = [];
2425

25-
private function __construct()
26+
/**
27+
* @param iterable<string, InnerList<int, Item>|Item|DataType> $members
28+
*/
29+
private function __construct(iterable $members = [])
2630
{
31+
foreach ($members as $key => $member) {
32+
$this->set($key, $member);
33+
}
2734
}
2835

2936
/**
@@ -44,12 +51,7 @@ public static function create(): self
4451
*/
4552
public static function fromAssociative(iterable $members): self
4653
{
47-
$instance = new self();
48-
foreach ($members as $key => $member) {
49-
$instance->set($key, $member);
50-
}
51-
52-
return $instance;
54+
return new self($members);
5355
}
5456

5557
/**
@@ -124,8 +126,6 @@ public function getIterator(): Iterator
124126
}
125127

126128
/**
127-
* Returns an iterable construct of dictionary pairs.
128-
*
129129
* @return Iterator<array{0:string, 1:Item|InnerList<int, Item>}>
130130
*/
131131
public function toPairs(): Iterator
@@ -136,26 +136,19 @@ public function toPairs(): Iterator
136136
}
137137

138138
/**
139-
* Returns an ordered list of the instance keys.
140-
*
141139
* @return array<string>
142140
*/
143141
public function keys(): array
144142
{
145143
return array_keys($this->members);
146144
}
147145

148-
/**
149-
* Tells whether an item or an inner-list is attached to the given key.
150-
*/
151146
public function has(string|int $offset): bool
152147
{
153148
return is_string($offset) && array_key_exists($offset, $this->members);
154149
}
155150

156151
/**
157-
* Returns the item or the inner-list is attached to the given key otherwise throw.
158-
*
159152
* @throws SyntaxError If the key is invalid
160153
* @throws InvalidOffset If the key is not found
161154
*/
@@ -168,9 +161,6 @@ public function get(string|int $offset): Item|InnerList
168161
return $this->members[$offset];
169162
}
170163

171-
/**
172-
* Tells whether an item or an inner-list and a key are attached to the given index position.
173-
*/
174164
public function hasPair(int $index): bool
175165
{
176166
try {
@@ -197,9 +187,6 @@ private function filterIndex(int $index): int
197187
}
198188

199189
/**
200-
* Returns the item or the inner-list and its key as attached to the given
201-
* collection according to their index position otherwise throw.
202-
*
203190
* @throws InvalidOffset If the key is not found
204191
*
205192
* @return array{0:string, 1:Item|InnerList<int, Item>}
@@ -220,8 +207,6 @@ public function pair(int $index): array
220207
}
221208

222209
/**
223-
* Adds a member at the end of the instance otherwise updates the value associated with the key if already present.
224-
*
225210
* @throws SyntaxError If the string key is not a valid
226211
*/
227212
public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
@@ -240,9 +225,6 @@ private static function filterMember(StructuredField|Token|ByteSequence|DateTime
240225
};
241226
}
242227

243-
/**
244-
* Deletes members associated with the list of submitted keys.
245-
*/
246228
public function delete(string ...$keys): self
247229
{
248230
foreach ($keys as $key) {
@@ -259,23 +241,13 @@ public function clear(): self
259241
return $this;
260242
}
261243

262-
/**
263-
* Adds a member at the end of the instance and deletes any previous reference to the key if present.
264-
*
265-
* @throws SyntaxError If the string key is not a valid
266-
*/
267244
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
268245
{
269246
unset($this->members[$key]);
270247

271248
return $this->set($key, $member);
272249
}
273250

274-
/**
275-
* Adds a member at the beginning of the instance and deletes any previous reference to the key if present.
276-
*
277-
* @throws SyntaxError If the string key is not a valid
278-
*/
279251
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
280252
{
281253
unset($this->members[$key]);
@@ -286,8 +258,6 @@ public function prepend(string $key, StructuredField|Token|ByteSequence|DateTime
286258
}
287259

288260
/**
289-
* Merges multiple instances using iterable associative structures.
290-
*
291261
* @param iterable<string, InnerList<int, Item>|Item|DataType> ...$others
292262
*/
293263
public function mergeAssociative(iterable ...$others): self
@@ -300,8 +270,6 @@ public function mergeAssociative(iterable ...$others): self
300270
}
301271

302272
/**
303-
* Merges multiple instances using iterable pairs.
304-
*
305273
* @param MemberOrderedMap<string, Item|InnerList<int, Item>>|iterable<array{0:string, 1:InnerList<int, Item>|Item|DataType}> ...$others
306274
*/
307275
public function mergePairs(MemberOrderedMap|iterable ...$others): self

src/InnerList.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@
1515

1616
/**
1717
* @implements MemberList<int, Item>
18+
* @phpstan-import-type DataType from Item
1819
*/
1920
final class InnerList implements MemberList, ParameterAccess
2021
{
21-
/** @var array<int, Item> */
22-
private array $members = [];
22+
/** @var list<Item> */
23+
private array $members;
2324

24-
private function __construct(private readonly Parameters $parameters)
25+
/**
26+
* @param iterable<Item|DataType> $members
27+
*/
28+
private function __construct(private readonly Parameters $parameters, iterable $members)
2529
{
30+
$this->members = array_map(self::filterMember(...), array_values([...$members]));
2631
}
2732

2833
/**
2934
* Returns a new instance.
3035
*/
3136
public static function from(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3237
{
33-
return (new self(Parameters::create()))->push(...$members);
38+
return new self(Parameters::create(), $members);
3439
}
3540

3641
/**
3742
* @param iterable<Item|DataType> $members
3843
* @param iterable<string, Item|DataType> $parameters
3944
*/
40-
public static function fromList(iterable $members, iterable $parameters = []): self
45+
public static function fromList(iterable $members = [], iterable $parameters = []): self
4146
{
42-
$instance = new self(Parameters::fromAssociative($parameters));
43-
foreach ($members as $member) {
44-
$instance->push($member);
45-
}
46-
47-
return $instance;
47+
return new self(Parameters::fromAssociative($parameters), $members);
4848
}
4949

5050
public static function fromHttpValue(Stringable|string $httpValue): self
@@ -83,10 +83,7 @@ public function withParameters(Parameters $parameters): static
8383
return $this;
8484
}
8585

86-
$newInstance = new self($parameters);
87-
$newInstance->members = $this->members;
88-
89-
return $newInstance;
86+
return new self($parameters, $this->members);
9087
}
9188

9289
public function toHttpValue(): string
@@ -162,7 +159,7 @@ public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Str
162159
*/
163160
public function push(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
164161
{
165-
$this->members = [...$this->members, ...array_map(self::filterMember(...), array_values($members))];
162+
$this->members = [...$this->members, ...array_map(self::filterMember(...), array_values($members))];
166163

167164
return $this;
168165
}

src/Item.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
use function trim;
2323
use const PHP_ROUND_HALF_EVEN;
2424

25+
/**
26+
* @phpstan-type DataType ByteSequence|Token|DateTimeInterface|Stringable|string|int|float|bool
27+
*/
2528
final class Item implements StructuredField, ParameterAccess
2629
{
2730
private function __construct(
@@ -188,15 +191,13 @@ public function withValue(Token|ByteSequence|DateTimeInterface|Stringable|string
188191
$value = (string) $value;
189192
}
190193

191-
if (($value instanceof ByteSequence && $this->value instanceof ByteSequence && $value->encoded() === $this->value->encoded())
192-
|| ($value instanceof Token && $this->value instanceof Token && $value->value === $this->value->value)
193-
|| ($value instanceof DateTimeInterface && $this->value instanceof DateTimeInterface && $value == $this->value)
194-
|| $value === $this->value
195-
) {
196-
return $this;
197-
}
198-
199-
return self::from($value, $this->parameters);
194+
return match (true) {
195+
$value instanceof ByteSequence && $this->value instanceof ByteSequence && $value->encoded() === $this->value->encoded(),
196+
$value instanceof Token && $this->value instanceof Token && $value->value === $this->value->value,
197+
$value instanceof DateTimeInterface && $this->value instanceof DateTimeInterface && $value == $this->value,
198+
$value === $this->value => $this,
199+
default => self::from($value, $this->parameters),
200+
};
200201
}
201202

202203
public function parameters(): Parameters
@@ -238,7 +239,7 @@ public function toHttpValue(): string
238239
{
239240
return match (true) {
240241
is_string($this->value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $this->value).'"',
241-
is_int($this->value) => (string)$this->value,
242+
is_int($this->value) => (string) $this->value,
242243
is_float($this->value) => $this->serializeDecimal($this->value),
243244
is_bool($this->value) => '?'.($this->value ? '1' : '0'),
244245
$this->value instanceof Token => $this->value->value,

src/MemberOrderedMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface MemberOrderedMap extends MemberContainer
2121
public function toPairs(): Iterator;
2222

2323
/**
24-
* Tells whether an item or an inner-list and a key are attached to the given index position.
24+
* Tells whether a pair is attached to the given index position.
2525
*/
2626
public function hasPair(int $index): bool;
2727

src/OrderedList.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
/**
1919
* @implements MemberList<int, Item|InnerList<int, Item>>
20+
* @phpstan-import-type DataType from Item
2021
*/
2122
final class OrderedList implements MemberList
2223
{
23-
/** @var array<int, Item|InnerList<int, Item>> */
24+
/** @var list<Item|InnerList<int, Item>> */
2425
private array $members;
2526

2627
private function __construct(InnerList|Item ...$members)
@@ -36,14 +37,9 @@ public static function from(InnerList|Item|Token|ByteSequence|DateTimeInterface|
3637
/**
3738
* @param iterable<InnerList<int, Item>|Item|DataType> $members
3839
*/
39-
public static function fromList(iterable $members): self
40+
public static function fromList(iterable $members = []): self
4041
{
41-
$instance = new self();
42-
foreach ($members as $member) {
43-
$instance->push($member);
44-
}
45-
46-
return $instance;
42+
return new self(...array_map(self::filterMember(...), [...$members]));
4743
}
4844

4945
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
@@ -62,11 +58,10 @@ private static function filterMember(StructuredField|Token|ByteSequence|DateTime
6258
*/
6359
public static function fromHttpValue(Stringable|string $httpValue): self
6460
{
65-
return self::from()
66-
->push(...array_map(
67-
fn ($value) => is_array($value) ? InnerList::fromList(...$value) : $value,
68-
Parser::parseList($httpValue)
69-
));
61+
return self::from(...array_map(
62+
fn ($value) => is_array($value) ? InnerList::fromList(...$value) : $value,
63+
Parser::parseList($httpValue)
64+
));
7065
}
7166

7267
public function toHttpValue(): string

src/Parameters.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
/**
1818
* @implements MemberOrderedMap<string, Item>
19+
* @phpstan-import-type DataType from Item
1920
*/
2021
final class Parameters implements MemberOrderedMap
2122
{
2223
/** @var array<string, Item> */
2324
private array $members = [];
2425

25-
private function __construct()
26+
/**
27+
* @param iterable<string, Item|DataType> $members
28+
*/
29+
private function __construct(iterable $members = [])
2630
{
31+
foreach ($members as $key => $member) {
32+
$this->set($key, $member);
33+
}
2734
}
2835

2936
/**
@@ -44,12 +51,7 @@ public static function create(): self
4451
*/
4552
public static function fromAssociative(iterable $members): self
4653
{
47-
$instance = new self();
48-
foreach ($members as $key => $member) {
49-
$instance->set($key, $member);
50-
}
51-
52-
return $instance;
54+
return new self($members);
5355
}
5456

5557
/**

src/Parser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*
2525
* @internal Use Dictionary::fromHttpValue(), Parameters::fromHttpValue(),
2626
* OrderedList::fromHttpValue(), InnerList::fromHttpValue() or Item::fromHttpValue() instead
27+
*
28+
* @phpstan-import-type DataType from Item
2729
*/
2830
final class Parser
2931
{

0 commit comments

Comments
 (0)