Skip to content

Commit 680bf6e

Browse files
committed
Improve methods return type and docblocks
1 parent 2db2c4e commit 680bf6e

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

src/Dictionary.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
/**
2121
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.2
2222
*
23-
* @implements MemberOrderedMap<string, Value|InnerList<int, Value>>
23+
* @implements MemberOrderedMap<string, Value|(MemberList<int, Value>&ParameterAccess)>
2424
* @phpstan-import-type DataType from Value
2525
*/
2626
final class Dictionary implements MemberOrderedMap
2727
{
28-
/** @var array<string, Value|InnerList<int, Value>> */
28+
/** @var array<string, Value|(MemberList<int, Value>&ParameterAccess)> */
2929
private array $members = [];
3030

3131
/**
32-
* @param iterable<string, InnerList<int, Value>|iterable<Value|DataType>|Value|DataType> $members
32+
* @param iterable<string, (MemberList<int, Value>&ParameterAccess)|iterable<Value|DataType>|Value|DataType> $members
3333
*/
3434
private function __construct(iterable $members = [])
3535
{
@@ -40,11 +40,14 @@ private function __construct(iterable $members = [])
4040

4141
/**
4242
* @param StructuredField|iterable<Value|DataType>|DataType $member
43+
*
44+
* @return Value|(MemberList<int, Value>&ParameterAccess)
4345
*/
44-
private static function filterMember(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Value
46+
private static function filterMember(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): mixed
4547
{
4648
return match (true) {
47-
$member instanceof InnerList, $member instanceof Value => $member,
49+
($member instanceof MemberList && $member instanceof ParameterAccess),
50+
$member instanceof Value => $member,
4851
is_iterable($member) => InnerList::fromList($member),
4952
default => Item::from($member),
5053
};
@@ -64,7 +67,7 @@ public static function create(): self
6467
* its keys represent the dictionary entry key
6568
* its values represent the dictionary entry value
6669
*
67-
* @param iterable<string, InnerList<int, Value>|list<Value|DataType>|Value|DataType> $members
70+
* @param iterable<string, (MemberList<int, Value>&ParameterAccess)|list<Value|DataType>|Value|DataType> $members
6871
*/
6972
public static function fromAssociative(iterable $members): self
7073
{
@@ -78,7 +81,7 @@ public static function fromAssociative(iterable $members): self
7881
* the first member represents the instance entry key
7982
* the second member represents the instance entry value
8083
*
81-
* @param MemberOrderedMap<string, Value|InnerList<int, Value>>|iterable<array{0:string, 1:InnerList<int, Value>|list<Value|DataType>|Value|DataType}> $pairs
84+
* @param MemberOrderedMap<string, Value|(MemberList<int, Value>&ParameterAccess)>|iterable<array{0:string, 1:(MemberList<int, Value>&ParameterAccess)|list<Value|DataType>|Value|DataType}> $pairs
8285
*/
8386
public static function fromPairs(iterable $pairs): self
8487
{
@@ -111,7 +114,7 @@ public static function fromHttpValue(Stringable|string $httpValue): self
111114

112115
public function toHttpValue(): string
113116
{
114-
$formatter = static fn (Value|InnerList $member, string $key): string => match (true) {
117+
$formatter = static fn (StructuredField $member, string $key): string => match (true) {
115118
$member instanceof Value && true === $member->value() => $key.$member->parameters()->toHttpValue(),
116119
default => $key.'='.$member->toHttpValue(),
117120
};
@@ -140,15 +143,15 @@ public function hasMembers(): bool
140143
}
141144

142145
/**
143-
* @return Iterator<string, Value|InnerList<int, Value>>
146+
* @return Iterator<string, Value|(MemberList<int, Value>&ParameterAccess)>
144147
*/
145148
public function getIterator(): Iterator
146149
{
147150
yield from $this->members;
148151
}
149152

150153
/**
151-
* @return Iterator<array{0:string, 1:Value|InnerList<int, Value>}>
154+
* @return Iterator<array{0:string, 1:Value|(MemberList<int, Value>&ParameterAccess)}>
152155
*/
153156
public function toPairs(): Iterator
154157
{
@@ -179,8 +182,10 @@ public function has(string|int ...$keys): bool
179182
/**
180183
* @throws SyntaxError If the key is invalid
181184
* @throws InvalidOffset If the key is not found
185+
*
186+
* @return Value|(MemberList<int, Value>&ParameterAccess)
182187
*/
183-
public function get(string|int $key): Value|InnerList
188+
public function get(string|int $key): StructuredField
184189
{
185190
if (!$this->has($key)) {
186191
throw InvalidOffset::dueToKeyNotFound($key);
@@ -219,7 +224,7 @@ private function filterIndex(int $index): int
219224
/**
220225
* @throws InvalidOffset If the key is not found
221226
*
222-
* @return array{0:string, 1:Value|InnerList<int, Value>}
227+
* @return array{0:string, 1:Value|(MemberList<int, Value>&ParameterAccess)}
223228
*/
224229
public function pair(int $index): array
225230
{
@@ -265,7 +270,7 @@ public function prepend(string $key, StructuredField|Token|ByteSequence|DateTime
265270
}
266271

267272
/**
268-
* @param iterable<string, InnerList<int, Value>|Value|DataType> ...$others
273+
* @param iterable<string, (MemberList<int, Value>&ParameterAccess)|Value|DataType> ...$others
269274
*/
270275
public function mergeAssociative(iterable ...$others): static
271276
{
@@ -278,7 +283,7 @@ public function mergeAssociative(iterable ...$others): static
278283
}
279284

280285
/**
281-
* @param MemberOrderedMap<string, Value|InnerList<int, Value>>|iterable<array{0:string, 1:InnerList<int, Value>|Value|DataType}> ...$others
286+
* @param MemberOrderedMap<string, Value|(MemberList<int, Value>&ParameterAccess)>|iterable<array{0:string, 1:(MemberList<int, Value>&ParameterAccess)|Value|DataType}> ...$others
282287
*/
283288
public function mergePairs(MemberOrderedMap|iterable ...$others): static
284289
{
@@ -301,9 +306,9 @@ public function offsetExists(mixed $offset): bool
301306
/**
302307
* @param string $offset
303308
*
304-
* @return Value|InnerList<int, Value>
309+
* @return Value|(MemberList<int, Value>&ParameterAccess)
305310
*/
306-
public function offsetGet(mixed $offset): InnerList|Value
311+
public function offsetGet(mixed $offset): mixed
307312
{
308313
return $this->get($offset);
309314
}

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static function filterMember(StructuredField|Token|ByteSequence|DateTime
6767
*/
6868
public static function fromHttpValue(Stringable|string $httpValue): self
6969
{
70-
return InnerList::fromList(...Parser::parseInnerList($httpValue));
70+
return self::fromList(...Parser::parseInnerList($httpValue));
7171
}
7272

7373
public function parameters(): Parameters

src/OuterList.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
/**
2020
* @see https://www.rfc-editor.org/rfc/rfc8941.html#name-lists
2121
*
22-
* @implements MemberList<int, Value|InnerList<int, Value>>
22+
* @implements MemberList<int, Value|(MemberList<int, Value>&ParameterAccess)>
2323
* @phpstan-import-type DataType from Value
2424
*/
2525
final class OuterList implements MemberList
2626
{
27-
/** @var list<Value|InnerList<int, Value>> */
27+
/** @var list<Value|(MemberList<int, Value>&ParameterAccess)> */
2828
private array $members;
2929

30-
private function __construct(InnerList|Value ...$members)
30+
/**
31+
* @param Value|(MemberList<int, Value>&ParameterAccess) ...$members
32+
*/
33+
private function __construct(MemberList|Value ...$members)
3134
{
3235
$this->members = array_values($members);
3336
}
@@ -41,7 +44,7 @@ public static function from(iterable|StructuredField|Token|ByteSequence|DateTime
4144
}
4245

4346
/**
44-
* @param iterable<InnerList<int, Value>|list<Value|DataType>|Value|DataType> $members
47+
* @param iterable<(MemberList<int, Value>&ParameterAccess)|list<Value|DataType>|Value|DataType> $members
4548
*/
4649
public static function fromList(iterable $members = []): self
4750
{
@@ -50,11 +53,14 @@ public static function fromList(iterable $members = []): self
5053

5154
/**
5255
* @param StructuredField|iterable<Value|DataType>|DataType $member
56+
*
57+
* @return (MemberList<int, Value>&ParameterAccess)|Value
5358
*/
54-
private static function filterMember(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Value
59+
private static function filterMember(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): mixed
5560
{
5661
return match (true) {
57-
$member instanceof InnerList, $member instanceof Value => $member,
62+
($member instanceof MemberList && $member instanceof ParameterAccess),
63+
$member instanceof Value => $member,
5864
is_iterable($member) => InnerList::fromList($member),
5965
default => Item::from($member),
6066
};
@@ -107,7 +113,7 @@ public function keys(): array
107113
}
108114

109115
/**
110-
* @return Iterator<int, Value|InnerList<int, Value>>
116+
* @return Iterator<int, Value|(MemberList<int, Value>&ParameterAccess)>
111117
*/
112118
public function getIterator(): Iterator
113119
{
@@ -142,7 +148,10 @@ private function filterIndex(string|int $index): int|null
142148
};
143149
}
144150

145-
public function get(string|int $key): Value|InnerList
151+
/**
152+
* @return (MemberList<int, Value>&ParameterAccess)|Value
153+
*/
154+
public function get(string|int $key): MemberList|Value
146155
{
147156
$index = $this->filterIndex($key);
148157
if (null === $index) {
@@ -246,9 +255,9 @@ public function offsetExists(mixed $offset): bool
246255
/**
247256
* @param int $offset
248257
*
249-
* @return Value|InnerList<int, Value>
258+
* @return Value|(MemberList<int, Value>&ParameterAccess)
250259
*/
251-
public function offsetGet(mixed $offset): InnerList|Value
260+
public function offsetGet(mixed $offset): mixed
252261
{
253262
return $this->get($offset);
254263
}

0 commit comments

Comments
 (0)