Skip to content

Commit

Permalink
Improve type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 3, 2024
1 parent 390c4b7 commit 70ba07c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ final class InnerList implements ArrayAccess, Countable, IteratorAggregate
/**
* @param iterable<SfItemInput> $members
*/
private function __construct(iterable $members, Parameters $parameters)
private function __construct(iterable $members, ?Parameters $parameters = null)
{
$this->members = array_map($this->filterMember(...), array_values([...$members]));
$this->parameters = $parameters;
$this->parameters = $parameters ?? Parameters::new();
}

/**
Expand Down Expand Up @@ -101,7 +101,7 @@ public static function fromAssociative(
}

if (!$parameters instanceof Parameters) {
$parameters = Parameters::fromAssociative($parameters);
return new self($value, Parameters::fromAssociative($parameters));
}

return new self($value, $parameters);
Expand All @@ -121,7 +121,7 @@ public static function fromPair(array $pair): self
}

if (1 === count($pair)) {
return new self($pair[0], Parameters::new());
return new self($pair[0]);
}

if ($pair[1] instanceof StructuredFieldProvider) {
Expand All @@ -132,7 +132,7 @@ public static function fromPair(array $pair): self
}

if (!$pair[1] instanceof Parameters) {
$pair[1] = Parameters::fromPairs($pair[1]);
return new self($pair[0], Parameters::fromPairs($pair[1]));
}

return new self($pair[0], $pair[1]);
Expand All @@ -144,7 +144,7 @@ public static function fromPair(array $pair): self
public static function new(
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
return new self($members, Parameters::new());
return new self($members);
}

public static function fromRfc9651(Stringable|string $httpValue): self
Expand Down
2 changes: 1 addition & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function fromPair(array $pair): self
}

if (1 === count($pair)) {
return new self($pair[0], Parameters::new());
return new self($pair[0]);
}

if ($pair[1] instanceof StructuredFieldProvider) {
Expand Down
12 changes: 6 additions & 6 deletions src/ParameterAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ abstract public function withParameters(Parameters $parameters): static;
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
* @param StructuredFieldProvider|Item|SfType $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function addParameter(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): static {
return $this->withParameters($this->parameters()->add($key, $member));
}
Expand All @@ -93,13 +93,13 @@ public function addParameter(
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
* @param StructuredFieldProvider|Item|SfType $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function prependParameter(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): static {
return $this->withParameters($this->parameters()->prepend($key, $member));
}
Expand All @@ -110,13 +110,13 @@ public function prependParameter(
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
* @param StructuredFieldProvider|Item|SfType $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function appendParameter(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): static {
return $this->withParameters($this->parameters()->append($key, $member));
}
Expand Down
15 changes: 6 additions & 9 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,12 @@ public function valueByIndex(int $index, ?callable $validate = null, bool|string
}

/**
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
* @param StructuredFieldProvider|Item|SfType $member
*/
public function add(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): self {
if (null === $member) {
return $this;
}
$members = $this->members;
$members[MapKey::from($key)->value] = self::filterMember($member);

Expand Down Expand Up @@ -572,11 +569,11 @@ public function removeByKeys(string ...$keys): self
}

/**
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
* @param StructuredFieldProvider|Item|SfType $member
*/
public function append(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): self {
$members = $this->members;
unset($members[$key]);
Expand All @@ -585,11 +582,11 @@ public function append(
}

/**
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
* @param StructuredFieldProvider|Item|SfType $member
*/
public function prepend(
string $key,
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
StructuredFieldProvider|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
): self {
$members = $this->members;
unset($members[$key]);
Expand Down
4 changes: 2 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,10 @@ private static function extractToken(string $httpValue): array
*/
private static function extractBytes(string $httpValue): array
{
if (1 !== preg_match(self::REGEXP_BYTES, $httpValue, $matches)) {
if (1 !== preg_match(self::REGEXP_BYTES, $httpValue, $found)) {
throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a Byte Sequence contains invalid characters.");
}

return [Bytes::fromEncoded($matches['byte']), strlen($matches['sequence'])];
return [Bytes::fromEncoded($found['byte']), strlen($found['sequence'])];
}
}

0 comments on commit 70ba07c

Please sign in to comment.