Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Traceless types #453

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/component/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@

#### `Interfaces`

- [TypeInterface](./../../src/Psl/Type/TypeInterface.php#L14)
- [TypeInterface](./../../src/Psl/Type/TypeInterface.php#L13)

#### `Classes`

- [Type](./../../src/Psl/Type/Type.php#L15)
- [Type](./../../src/Psl/Type/Type.php#L14)


1 change: 0 additions & 1 deletion src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ final class Loader
'Psl\\Type\\Internal\\LiteralScalarType' => 'Psl/Type/Internal/LiteralScalarType.php',
'Psl\\Type\\Internal\\BackedEnumType' => 'Psl/Type/Internal/BackedEnumType.php',
'Psl\\Type\\Internal\\UnitEnumType' => 'Psl/Type/Internal/UnitEnumType.php',
'Psl\\Type\\Exception\\TypeTrace' => 'Psl/Type/Exception/TypeTrace.php',
'Psl\\Type\\Exception\\AssertException' => 'Psl/Type/Exception/AssertException.php',
'Psl\\Type\\Exception\\CoercionException' => 'Psl/Type/Exception/CoercionException.php',
'Psl\\Type\\Exception\\Exception' => 'Psl/Type/Exception/Exception.php',
Expand Down
28 changes: 24 additions & 4 deletions src/Psl/Type/Exception/AssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@
namespace Psl\Type\Exception;

use Psl\Str;
use Psl\Vec;
use Throwable;

use function get_debug_type;

final class AssertException extends Exception
{
private string $expected;

public function __construct(string $actual, string $expected, TypeTrace $typeTrace)
/**
* @param list<string> $paths
*/
public function __construct(string $actual, string $expected, array $paths = [], ?Throwable $previous = null)
veewee marked this conversation as resolved.
Show resolved Hide resolved
{
parent::__construct(Str\format('Expected "%s", got "%s".', $expected, $actual), $actual, $typeTrace);
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

parent::__construct(
Str\format(
'Expected "%s", got "%s"%s.',
$expected,
$first,
$paths ? ' at path "' . Str\join($paths, '.') . '"' : ''
),
$actual,
$paths,
$previous
);

$this->expected = $expected;
}
Expand All @@ -27,8 +44,11 @@ public function getExpectedType(): string
public static function withValue(
mixed $value,
string $expected_type,
TypeTrace $trace
?string $path = null,
?Throwable $previous = null
): self {
return new self(get_debug_type($value), $expected_type, $trace);
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];

return new self(get_debug_type($value), $expected_type, Vec\filter_nulls($paths), $previous);
}
}
38 changes: 17 additions & 21 deletions src/Psl/Type/Exception/CoercionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Psl\Type\Exception;

use Psl\Str;
use Psl\Vec;
use Throwable;

use function get_debug_type;
Expand All @@ -13,18 +14,24 @@ final class CoercionException extends Exception
{
private string $target;

public function __construct(string $actual, string $target, TypeTrace $typeTrace, string $additionalInfo = '')
/**
* @param list<string> $paths
*/
public function __construct(string $actual, string $target, array $paths = [], ?Throwable $previous = null)
veewee marked this conversation as resolved.
Show resolved Hide resolved
{
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

parent::__construct(
Str\format(
'Could not coerce "%s" to type "%s"%s%s',
$actual,
'Could not coerce "%s" to type "%s"%s%s.',
$first,
$target,
$additionalInfo ? ': ' : '.',
$additionalInfo
$paths ? ' at path "' . Str\join($paths, '.') . '"' : '',
$previous && !$previous instanceof self ? ': ' . $previous->getMessage() : '',
),
$actual,
$typeTrace,
$paths,
$previous
);

$this->target = $target;
Expand All @@ -38,22 +45,11 @@ public function getTargetType(): string
public static function withValue(
mixed $value,
string $target,
TypeTrace $typeTrace
?string $path = null,
?Throwable $previous = null
): self {
return new self(get_debug_type($value), $target, $typeTrace);
}
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];

public static function withConversionFailureOnValue(
mixed $value,
string $target,
TypeTrace $typeTrace,
Throwable $failure,
): self {
return new self(
get_debug_type($value),
$target,
$typeTrace,
$failure->getMessage()
);
return new self(get_debug_type($value), $target, Vec\filter_nulls($paths), $previous);
}
}
34 changes: 27 additions & 7 deletions src/Psl/Type/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,50 @@
namespace Psl\Type\Exception;

use Psl\Exception\RuntimeException;
use Throwable;

abstract class Exception extends RuntimeException implements ExceptionInterface
{
private TypeTrace $typeTrace;
private string $actual;

/**
* @var list<string>
*/
private array $paths;

private string $first;

/**
* @param list<string> $paths
*/
public function __construct(
veewee marked this conversation as resolved.
Show resolved Hide resolved
string $message,
string $actual,
TypeTrace $typeTrace
array $paths,
?Throwable $previous = null
) {
parent::__construct($message);
parent::__construct($message, 0, $previous);

$this->actual = $actual;
$this->typeTrace = $typeTrace;
$this->paths = $paths;
$this->first = $previous instanceof self ? $previous->first : $actual;
$this->actual = $actual;
}

/**
* @return list<string>
*/
public function getPaths(): array
{
return $this->paths;
}

public function getActualType(): string
{
return $this->actual;
}

public function getTypeTrace(): TypeTrace
public function getFirstFailingActualType(): string
{
return $this->typeTrace;
return $this->first;
}
}
64 changes: 64 additions & 0 deletions src/Psl/Type/Exception/PathExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Exception;

use Psl\Str;

/**
* @psalm-immutable
veewee marked this conversation as resolved.
Show resolved Hide resolved
*/
final class PathExpression
{
/**
* @pure
*/
public static function path(mixed $path): string
{
return match (true) {
is_bool($path) => $path ? 'true' : 'false',
is_scalar($path) => (string) $path,
default => get_debug_type($path),
};
}

/**
* @pure
*/
public static function expression(string $expression, mixed $path): string
{
return Str\format($expression, self::path($path));
}

/**
* @pure
*/
public static function iteratorKey(mixed $key): string
{
return self::expression('key(%s)', $key);
}

/**
* @pure
*/
public static function iteratorError(mixed $previousKey): string
{
return self::expression($previousKey === null ? 'first()' : '%s.next()', $previousKey);
}

public static function coerceInput(mixed $input, string $expectedType): string
{
return Str\format('coerce_input(%s): %s', get_debug_type($input), $expectedType);
}

public static function convert(mixed $input, string $expectedType): string
{
return Str\format('convert(%s): %s', get_debug_type($input), $expectedType);
}

public static function coerceOutput(mixed $input, string $expectedType): string
{
return Str\format('coerce_output(%s): %s', get_debug_type($input), $expectedType);
}
}
34 changes: 0 additions & 34 deletions src/Psl/Type/Exception/TypeTrace.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Psl/Type/Internal/BackedEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public function coerce(mixed $value): BackedEnum

foreach (($this->enum)::cases() as $case) {
if (Type\string()->matches($case->value)) {
$string_value = Type\string()->withTrace($this->getTrace())->coerce($value);
$string_value = Type\string()->coerce($value);

if ($string_value === $case->value) {
return $case;
}
} else {
$integer_value = Type\int()->withTrace($this->getTrace())->coerce($value);
$integer_value = Type\int()->coerce($value);

if ($integer_value === $case->value) {
return $case;
}
}
}

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

/**
Expand All @@ -73,7 +73,7 @@ public function assert(mixed $value): BackedEnum
return $value;
}

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

public function toString(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Type/Internal/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function coerce(mixed $value): bool
return true;
}

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

/**
Expand All @@ -56,7 +56,7 @@ public function assert(mixed $value): bool
return $value;
}

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

public function toString(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Type/Internal/ClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function coerce(mixed $value): string
return $value;
}

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

/**
Expand All @@ -66,7 +66,7 @@ public function assert(mixed $value): string
return $value;
}

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

public function toString(): string
Expand Down
Loading