Skip to content

Commit

Permalink
Merge 25f2ed7 into 934c879
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Feb 17, 2021
2 parents 934c879 + 25f2ed7 commit f73a2c0
Show file tree
Hide file tree
Showing 74 changed files with 1,107 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

$argument = $call_args[0] ?? null;
if (null === $argument) {
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\Type', [
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\TypeInterface', [
new Type\Union([
new Type\Atomic\TArray([
new Type\Union([new Type\Atomic\TArrayKey()]),
Expand All @@ -41,7 +41,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$argument_value = $argument->value;
$type = $statements_source->getNodeTypeProvider()->getType($argument_value);
if (null === $type) {
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\Type', [
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\TypeInterface', [
new Type\Union([
new Type\Atomic\TArray([
new Type\Union([new Type\Atomic\TArrayKey()]),
Expand All @@ -54,7 +54,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$atomic = $type->getAtomicTypes();
$argument_shape = $atomic['array'] ?? null;
if (!$argument_shape instanceof Type\Atomic\TKeyedArray) {
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\Type', [
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\TypeInterface', [
new Type\Union([
new Type\Atomic\TArray([
new Type\Union([new Type\Atomic\TArrayKey()]),
Expand All @@ -76,7 +76,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$properties[$name] = $property_type;
}

return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\Type', [
return new Type\Union([new Type\Atomic\TGenericObject('Psl\Type\TypeInterface', [
new Type\Union([
new Type\Atomic\TKeyedArray($properties)
])
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Dict/count_values.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function count_values(iterable $values): array

foreach ($values as $value) {
Psl\invariant(
Type\is_arraykey($value),
Type\array_key()->matches($value),
'Expected all values to be of type array-key, value of type (%s) provided.',
gettype($value)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Dict/flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function flip(iterable $iterable): array
$result = [];
foreach ($iterable as $key => $value) {
Psl\invariant(
Type\is_arraykey($value),
Type\array_key()->matches($value),
'Expected all values to be of type array-key, value of type (%s) provided.',
gettype($value)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Dict/group_by.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function group_by(iterable $values, callable $key_func): array
}

Psl\invariant(
Type\is_arraykey($key),
Type\array_key()->matches($key),
'Expected $key_func to return a value of type array-key, value of type (%s) returned.',
gettype($key)
);
Expand Down
9 changes: 6 additions & 3 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ final class Loader
'Psl\Type\scalar',
'Psl\Type\shape',
'Psl\Type\union',
'Psl\Type\vec',
'Psl\Type\dict',
'Psl\Type\is_array',
'Psl\Type\is_arraykey',
'Psl\Type\is_bool',
Expand Down Expand Up @@ -455,11 +457,10 @@ final class Loader
'Psl\Observer\ObserverInterface',
'Psl\Result\ResultInterface',
'Psl\Encoding\Exception\ExceptionInterface',
'Psl\Type\TypeInterface',
];

public const TRAITS = [
'Psl\Type\Internal\TypeTraceTrait',
];
public const TRAITS = [];

public const CLASSES = [
'Psl\Exception\InvariantViolationException',
Expand Down Expand Up @@ -492,6 +493,8 @@ final class Loader
'Psl\Type\Internal\ShapeType',
'Psl\Type\Internal\NonEmptyStringType',
'Psl\Type\Internal\UnionType',
'Psl\Type\Internal\VecType',
'Psl\Type\Internal\DictType',
'Psl\Type\Exception\TypeTrace',
'Psl\Type\Exception\AssertException',
'Psl\Type\Exception\CoercionException',
Expand Down
7 changes: 5 additions & 2 deletions src/Psl/Internal/internal_encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
function internal_encoding(?string $encoding = null): string
{
Psl\invariant(null === $encoding || is_encoding_valid($encoding), 'Invalid encoding.');
/** @psalm-suppress ImpureFunctionCall */
return $encoding ?? (Type\is_string($internal_encoding = mb_internal_encoding()) ? $internal_encoding : 'UTF-8');
/**
* @psalm-suppress ImpureFunctionCall - see https://github.com/azjezz/psl/issues/130
* @psalm-suppress ImpureMethodCall - see https://github.com/azjezz/psl/issues/130
*/
return $encoding ?? (Type\string()->matches($internal_encoding = mb_internal_encoding()) ? $internal_encoding : 'UTF-8');
}
2 changes: 1 addition & 1 deletion src/Psl/SecureRandom/bytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function bytes(int $length): string
// @codeCoverageIgnoreStart
} catch (PHPException $e) {
$code = $e->getCode();
if (Type\is_string($code)) {
if (Type\string()->matches($code)) {
$code = Str\to_int($code) ?? 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Psl/SecureRandom/int.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function int(int $min = Math\INT64_MIN, int $max = Math\INT64_MAX): int
// @codeCoverageIgnoreStart
} catch (PHPException $e) {
$code = $e->getCode();
if (Type\is_string($code)) {
if (Type\string()->matches($code)) {
$code = Str\to_int($code) ?? 0;
}

Expand Down
9 changes: 6 additions & 3 deletions src/Psl/Str/Byte/split.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function split(string $string, string $delimiter, ?int $limit = null): array
$result = explode($delimiter, $string, $limit);
}

Psl\invariant(Type\is_array($result), 'Unexpected error');

return $result;
/**
* @psalm-suppress MissingThrowsDocblock - should not throw
* @psalm-suppress ImpureFunctionCall - see https://github.com/azjezz/psl/issues/130
* @psalm-suppress ImpureMethodCall - see https://github.com/azjezz/psl/issues/130
*/
return Type\vec(Type\string())->coerce($result);
}
16 changes: 14 additions & 2 deletions src/Psl/Type/Internal/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,31 @@
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_bool;

/**
* @extends Type\Type<bool>
*
* @internal
*/
final class BoolType extends Type\Type
{
/**
* @param mixed $value
*
* @psalm-assert-if-true bool $value
*/
public function matches($value): bool
{
return is_bool($value);
}

/**
* @throws CoercionException
*/
public function coerce($value): bool
{
if (Type\is_bool($value)) {
if (is_bool($value)) {
return $value;
}

Expand All @@ -42,7 +54,7 @@ public function coerce($value): bool
*/
public function assert($value): bool
{
if (Type\is_bool($value)) {
if (is_bool($value)) {
return $value;
}

Expand Down
134 changes: 134 additions & 0 deletions src/Psl/Type/Internal/DictType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Psl\Dict;
use Psl\Str;
use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_array;
use function is_iterable;

/**
* @template Tk of array-key
* @template Tv
*
* @extends Type\Type<array<Tk, Tv>>
*
* @internal
*/
final class DictType extends Type\Type
{
/**
* @psalm-var Type\TypeInterface<Tk>
*/
private Type\TypeInterface $key_type;

/**
* @psalm-var Type\TypeInterface<Tv>
*/
private Type\TypeInterface $value_type;

/**
* @psalm-param Type\TypeInterface<Tk> $key_type
* @psalm-param Type\TypeInterface<Tv> $value_type
*/
public function __construct(
Type\TypeInterface $key_type,
Type\TypeInterface $value_type
) {
$this->key_type = $key_type;
$this->value_type = $value_type;
}

/**
* @psalm-param mixed $value
*
* @psalm-return array<Tk, Tv>
*
* @throws CoercionException
*/
public function coerce($value): array
{
if (is_iterable($value)) {
$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()));

$key_type = $this->key_type->withTrace($key_trace);
$value_type = $this->value_type->withTrace($value_trace);

/**
* @psalm-var list<array{0: Tk, 1: Tv}> $entries
*/
$entries = [];

/**
* @psalm-var Tk $k
* @psalm-var Tv $v
*/
foreach ($value as $k => $v) {
$entries[] = [
$key_type->coerce($k),
$value_type->coerce($v),
];
}

return Dict\from_entries($entries);
}

throw CoercionException::withValue($value, $this->toString(), $this->getTrace());
}

/**
* @psalm-param mixed $value
*
* @psalm-return array<Tk, Tv>
*
* @psalm-assert array<Tk, Tv> $value
*
* @throws AssertException
*/
public function assert($value): array
{
if (is_array($value)) {
$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()));

$key_type = $this->key_type->withTrace($key_trace);
$value_type = $this->value_type->withTrace($value_trace);

/**
* @psalm-var list<array{0: Tk, 1: Tv}> $entries
*/
$entries = [];

/**
* @psalm-var Tk $k
* @psalm-var Tv $v
*/
foreach ($value as $k => $v) {
$entries[] = [
$key_type->assert($k),
$value_type->assert($v),
];
}

return Dict\from_entries($entries);
}

throw AssertException::withValue($value, $this->toString(), $this->getTrace());
}

public function toString(): string
{
return Str\format('array<%s, %s>', $this->key_type->toString(), $this->value_type->toString());
}
}
22 changes: 17 additions & 5 deletions src/Psl/Type/Internal/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Psl\Type\Exception\CoercionException;

use function ctype_digit;
use function is_float;
use function is_int;
use function is_object;
use function is_string;

/**
* @extends Type\Type<float>
Expand All @@ -17,7 +21,15 @@
*/
final class FloatType extends Type\Type
{
private const TYPE = 'float';
/**
* @param mixed $value
*
* @psalm-assert-if-true float $value
*/
public function matches($value): bool
{
return is_float($value);
}

/**
* @psalm-param mixed $value
Expand All @@ -28,15 +40,15 @@ final class FloatType extends Type\Type
*/
public function coerce($value): float
{
if (Type\is_float($value)) {
if (is_float($value)) {
return $value;
}

if (Type\is_int($value)) {
if (is_int($value)) {
return $value;
}

if (Type\is_string($value) || (Type\is_object($value) && method_exists($value, '__toString'))) {
if (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
$str = (string) $value;
if ('' === $str) {
throw CoercionException::withValue($value, $this->toString(), $this->getTrace());
Expand Down Expand Up @@ -65,7 +77,7 @@ public function coerce($value): float
*/
public function assert($value): float
{
if (Type\is_float($value)) {
if (is_float($value)) {
return $value;
}

Expand Down
Loading

0 comments on commit f73a2c0

Please sign in to comment.