[Type] add psalm assertation

This commit is contained in:
azjezz 2020-08-03 21:16:32 +01:00
parent 4784d00e03
commit 00602d718a
14 changed files with 57 additions and 26 deletions

View File

@ -24,7 +24,7 @@ namespace Psl\Iter;
*
* @psalm-param mixed $value
*
* @psalm-assert-if-true iterable<array-key, mixed> $value
* @psalm-assert-if-true iterable<mixed, mixed> $value
*/
function is_iterable($value): bool
{

View File

@ -24,23 +24,23 @@ final class ArrayType extends Type
/**
* @psalm-var Type<Tk>
*/
private Type $key_type_spec;
private Type $key_type;
/**
* @psalm-var Type<Tv>
*/
private Type $value_type_spec;
private Type $value_type;
/**
* @psalm-param Type<Tk> $key_type_spec
* @psalm-param Type<Tv> $value_type_spec
* @psalm-param Type<Tk> $key_type
* @psalm-param Type<Tv> $value_type
*/
public function __construct(
Type $key_type_spec,
Type $value_type_spec
Type $key_type,
Type $value_type
) {
$this->key_type_spec = $key_type_spec;
$this->value_type_spec = $value_type_spec;
$this->key_type = $key_type;
$this->value_type = $value_type;
}
/**
@ -53,13 +53,13 @@ final class ArrayType extends Type
public function coerce($value): array
{
if (Iter\is_iterable($value)) {
$key_trace = $this->getTrace()->withFrame(Str\format('iterable<%s, _>', $this->key_type_spec->toString()));
$value_trace = $this->getTrace()->withFrame(Str\format('iterable<_, %s>', $this->value_type_spec->toString()));
$key_trace = $this->getTrace()->withFrame(Str\format('array<%s, _>', $this->key_type->toString()));
$value_trace = $this->getTrace()->withFrame(Str\format('array<_, %s>', $this->value_type->toString()));
/** @psalm-var Type<Tk> $key_type_spec */
$key_type_spec = $this->key_type_spec->withTrace($key_trace);
/** @psalm-var Type<Tv> $value_type_spec */
$value_type_spec = $this->value_type_spec->withTrace($value_trace);
/** @psalm-var Type<Tk> $key_type */
$key_type = $this->key_type->withTrace($key_trace);
/** @psalm-var Type<Tv> $value_type */
$value_type = $this->value_type->withTrace($value_trace);
/**
* @psalm-var list<array{0: Tk, 1: Tv}> $entries
@ -71,15 +71,16 @@ final class ArrayType extends Type
*/
foreach ($value as $k => $v) {
/** @psalm-var Tk $k */
$k = $key_type_spec->coerce($k);
$k = $key_type->coerce($k);
/** @psalm-var Tv $v */
$v = $value_type_spec->coerce($v);
$v = $value_type->coerce($v);
$entries[] = [$k, $v];
}
/** @psalm-var Iter\Iterator<Tk, Tv> $iterator */
$iterator = Iter\from_entries($entries);
/** @psalm-var array<Tk, Tv> */
return Iter\to_array_with_keys($iterator);
}
@ -92,18 +93,20 @@ final class ArrayType extends Type
*
* @psalm-return array<Tk, Tv>
*
* @psalm-assert array<Tk, Tv> $value
*
* @throws TypeAssertException
*/
public function assert($value): array
{
if (Arr\is_array($value)) {
$key_trace = $this->getTrace()->withFrame(Str\format('iterable<%s, _>', $this->key_type_spec->toString()));
$value_trace = $this->getTrace()->withFrame(Str\format('iterable<_, %s>', $this->value_type_spec->toString()));
$key_trace = $this->getTrace()->withFrame(Str\format('array<%s, _>', $this->key_type->toString()));
$value_trace = $this->getTrace()->withFrame(Str\format('array<_, %s>', $this->value_type->toString()));
/** @psalm-var Type<Tk> $key_type_spec */
$key_type_spec = $this->key_type_spec->withTrace($key_trace);
/** @psalm-var Type<Tv> $value_type_spec */
$value_type_spec = $this->value_type_spec->withTrace($value_trace);
/** @psalm-var Type<Tk> $key_type */
$key_type = $this->key_type->withTrace($key_trace);
/** @psalm-var Type<Tv> $value_type */
$value_type = $this->value_type->withTrace($value_trace);
/**
* @psalm-var list<array{0: Tk, 1: Tv}> $entries
@ -115,9 +118,9 @@ final class ArrayType extends Type
*/
foreach ($value as $k => $v) {
/** @psalm-var Tk $k */
$k = $key_type_spec->assert($k);
$k = $key_type->assert($k);
/** @psalm-var Tv $v */
$v = $value_type_spec->assert($v);
$v = $value_type->assert($v);
$entries[] = [$k, $v];
}
@ -132,6 +135,6 @@ final class ArrayType extends Type
public function toString(): string
{
return Str\format('array<%s, %s>', $this->key_type_spec->toString(), $this->value_type_spec->toString());
return Str\format('array<%s, %s>', $this->key_type->toString(), $this->value_type->toString());
}
}

View File

@ -40,6 +40,8 @@ final class BoolType extends Type
/**
* @psalm-return bool
*
* @psalm-assert bool $value
*
* @throws TypeAssertException
*/
public function assert($value): bool

View File

@ -59,6 +59,8 @@ final class FloatType extends Type
*
* @psalm-return float
*
* @psalm-assert float $value
*
* @throws TypeAssertException
*/
public function assert($value): float

View File

@ -56,6 +56,8 @@ final class IntType extends Type
*
* @psalm-return int
*
* @psalm-assert int $value
*
* @throws TypeAssertException
*/
public function assert($value): int

View File

@ -87,6 +87,8 @@ final class IntersectionType extends Type
*
* @psalm-return Tl&Tr
*
* @psalm-assert Tl&Tr $value
*
* @throws TypeAssertException
*/
public function assert($value)

View File

@ -90,6 +90,8 @@ final class IterableType extends Type
*
* @psalm-return iterable<Tk, Tv>
*
* @psalm-assert iterable<Tk, Tv> $value
*
* @throws TypeAssertException
*/
public function assert($value): iterable

View File

@ -26,6 +26,8 @@ final class MixedType extends Type
/**
* @psalm-param mixed $value
*
* @psalm-assert mixed $value
*
* @psalm-return mixed
*/
public function assert($value)

View File

@ -32,6 +32,8 @@ final class NullType extends Type
/**
* @psalm-param mixed $value
*
* @psalm-assert null $value
*
* @psalm-return null
*/
public function assert($value)

View File

@ -52,6 +52,8 @@ final class ObjectType extends Type
*
* @psalm-return T
*
* @psalm-assert T $value
*
* @throws TypeAssertException
*/
public function assert($value): object

View File

@ -27,6 +27,8 @@ final class ResourceType extends Type
* @psalm-param mixed $value
*
* @psalm-return resource
*
* @throws TypeCoercionException
*/
public function coerce($value)
{
@ -48,6 +50,10 @@ final class ResourceType extends Type
* @psalm-param mixed $value
*
* @psalm-return resource
*
* @psalm-assert resource $value
*
* @throws TypeAssertException
*/
public function assert($value)
{

View File

@ -45,6 +45,8 @@ final class StringType extends Type
*
* @psalm-return string
*
* @psalm-assert string $value
*
* @throws TypeAssertException
*/
public function assert($value): string

View File

@ -76,6 +76,8 @@ class UnionType extends Type
*
* @psalm-return Tl|Tr
*
* @psalm-assert Tl|Tr $value
*
* @throws TypeAssertException
*/
public function assert($value)

View File

@ -30,6 +30,8 @@ abstract class Type
*
* @psalm-return T
*
* @psalm-assert T $value
*
* @throws TypeAssertException
*/
abstract public function assert($value);