Skip to content

Commit

Permalink
Fix Psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jan 14, 2024
1 parent f332d58 commit cbb254d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/StructuredFields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<bool>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<bool>
*/
class Boolean extends Item
final class Boolean extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(bool $item, array $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/Bytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<string>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<string>
*/
class Bytes extends Item
final class Bytes extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(string $item, array $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<int>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<int>
*/
class Date extends Item
final class Date extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(int $item, array $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/DisplayString.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<string>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<string>
*/
class DisplayString extends Item
final class DisplayString extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(string $item, array $parameters)
{
Expand Down
9 changes: 4 additions & 5 deletions src/StructuredFields/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @psalm-import-type Rfc8941BareItem from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<list<Item<Rfc8941BareItem>>>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<list<Item<scalar>>>
*/
class InnerList extends Item
final class InnerList extends Item
{
/**
* @psalm-param list<Item<Rfc8941BareItem>> $item
* @psalm-param list<Item<scalar>> $item
* @psalm-param Rfc8941Parameters $parameters
*/
public function __construct(array $item, array $parameters)
Expand Down
10 changes: 5 additions & 5 deletions src/StructuredFields/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Amp\Http\StructuredFields;

/**
* @template-covariant Inner
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-covariant Inner of scalar|list<Item<scalar>>
* @psalm-import-type Rfc8941Parameters from Rfc8941
*/
class Item
abstract class Item
{
/**
* @psalm-param Inner $item
* @psalm-param Rfc8941Parameters $parameters
* @param Inner $item
* @param Rfc8941Parameters $parameters
*/
protected function __construct(public readonly int|float|string|bool|array $item, public readonly array $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<int|float>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<int|float>
*/
class Number extends Item
final class Number extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(int|float $item, array $parameters)
{
Expand Down
36 changes: 20 additions & 16 deletions src/StructuredFields/Rfc8941.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

// Including support for the extension for RFC 8941, see https://datatracker.ietf.org/doc/draft-ietf-httpbis-sfbis
/**
* @psalm-type Rfc8941SingleItem = Item<Rfc8941BareItem>
* @psalm-type Rfc8941SingleItem = Boolean|Bytes|Date|DisplayString|Number|Str|Token
* @psalm-type Rfc8941ListItem = InnerList|Rfc8941SingleItem
* @psalm-type Rfc8941BareItem = int|float|string|bool
* @psalm-type Rfc8941Parameters = array<string, Rfc8941BareItem>
* @psalm-type Rfc8941Parameters = array<string, scalar>
*/
class Rfc8941
final class Rfc8941
{
/**
* @param string[]|string $value
* @psalm-return null|list<Rfc8941ListItem>
* @return null|list<Rfc8941ListItem>
*/
public static function parseList(array|string $value): ?array
{
Expand Down Expand Up @@ -48,7 +47,7 @@ public static function parseList(array|string $value): ?array

/**
* @param string[]|string $value
* @psalm-return array<string, Rfc8941ListItem>|null
* @return array<string, Rfc8941ListItem>|null
*/
public static function parseDictionary(array|string $value): ?array
{
Expand All @@ -68,9 +67,11 @@ public static function parseDictionary(array|string $value): ?array
}
if ($i < $len && $string[$i] === "=") {
++$i;
if (null === $values[$key] = self::parseItemOrInnerList($string, $i)) {
$value = self::parseItemOrInnerList($string, $i);
if ($value === null) {
return null;
}
$values[$key] = $value;
} else {
if (null === $parameters = self::parseParameters($string, $i)) {
return null;
Expand All @@ -91,7 +92,7 @@ public static function parseDictionary(array|string $value): ?array
}
}

/** @psalm-return null|Rfc8941SingleItem */
/** @return null|Rfc8941SingleItem */
public static function parseItem(string $string): ?Item
{
$i = \strspn($string, " ");
Expand All @@ -102,7 +103,7 @@ public static function parseItem(string $string): ?Item
return $i + \strspn($string, " ", $i) < \strlen($string) ? null : $parsed;
}

/** @psalm-return null|Rfc8941ListItem */
/** @return null|Rfc8941ListItem */
private static function parseItemOrInnerList(string $string, int &$i): ?Item
{
$len = \strlen($string);
Expand All @@ -125,15 +126,17 @@ private static function parseItemOrInnerList(string $string, int &$i): ?Item
if ($chr !== " " && $chr !== "(") {
return null;
}
if (null === $innerList[] = self::parseItemInternal($string, $i)) {
$value = self::parseItemInternal($string, $i);
if ($value === null) {
return null;
}
$innerList[] = $value;
}
}
return self::parseItemInternal($string, $i);
}

/** @psalm-return null|Rfc8941SingleItem */
/** @return null|Rfc8941SingleItem */
private static function parseItemInternal(string $string, int &$i): ?Item
{
if (null === $value = self::parseBareItem($string, $i, $class)) {
Expand All @@ -142,6 +145,7 @@ private static function parseItemInternal(string $string, int &$i): ?Item
if (null === $parameters = self::parseParameters($string, $i)) {
return null;
}
/** @var class-string<Rfc8941SingleItem> $class */
return new $class($value, $parameters);
}

Expand Down Expand Up @@ -397,11 +401,11 @@ private static function parseDisplayStringInternal(string $string, int &$i): ?st
}

/**
* @psalm-template TOutput of Rfc8941BareItem|null
* @param-out (TOutput is null ? null : class-string<Rfc8941SingleItem>) $class
* @psalm-return TOutput
* @param null $class
* @param-out class-string<Rfc8941SingleItem>|null $class
* @return scalar|null
*/
private static function parseBareItem(string $string, int &$i, ?string &$class = ""): null|int|float|string|bool
private static function parseBareItem(string $string, int &$i, ?string &$class = null): null|int|float|string|bool
{
$chr = \ord($string[$i]);
if ($chr === \ord("-") || ($chr >= \ord('0') && $chr <= \ord('9'))) {
Expand Down Expand Up @@ -442,7 +446,7 @@ private static function parseBareItem(string $string, int &$i, ?string &$class =
return null;
}

/** @psalm-return null|Rfc8941Parameters */
/** @return null|Rfc8941Parameters */
private static function parseParameters(string $string, int &$i): ?array
{
$parameters = [];
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<string>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<string>
*/
class Str extends Item
final class Str extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(string $item, array $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredFields/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Amp\Http\StructuredFields;

/**
* @psalm-import-type Rfc8941Parameters from \Amp\Http\StructuredFields\Rfc8941
* @template-extends Item<string>
* @psalm-import-type Rfc8941Parameters from Rfc8941
* @extends Item<string>
*/
class Token extends Item
final class Token extends Item
{
/**
* @psalm-param Rfc8941Parameters $parameters
* @param Rfc8941Parameters $parameters
*/
public function __construct(string $item, array $parameters)
{
Expand Down

0 comments on commit cbb254d

Please sign in to comment.