diff --git a/README.md b/README.md index b9b22a4..6a6e948 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ composer require typhoon/type ## Usage ```php -use Typhoon\types; +use Typhoon\Type\types; /** * array{ @@ -28,17 +28,17 @@ use Typhoon\types; * ... * } */ -$type = types::unsealedShape([ +$type = types::arrayShape([ types::nonEmptyString, 'a' => types::optional(types::union(types::int, types::float)), 'b' => types::object(Traversable::class, types::numericString, types::false), 'c' => types::callable( parameters: [ types::classConstant(PDO::class, '*'), - types::defaultParam(types::classTemplate(Generator::class, 'TSend')), - types::variadicParam(types::scalar), + types::param(types::classTemplate(Generator::class, 'TSend'), hasDefault: true), + types::param(types::scalar, variadic: true), ], returnType: types::void, ), -]); +], sealed: false); ``` diff --git a/src/ShapeElement.php b/src/ArrayElement.php similarity index 95% rename from src/ShapeElement.php rename to src/ArrayElement.php index cbbbe7e..3984341 100644 --- a/src/ShapeElement.php +++ b/src/ArrayElement.php @@ -9,7 +9,7 @@ * @psalm-immutable * @template-covariant TType */ -final class ShapeElement +final class ArrayElement { /** * @var Type diff --git a/src/ShapeType.php b/src/ArrayShapeType.php similarity index 77% rename from src/ShapeType.php rename to src/ArrayShapeType.php index 0a80d8a..4917743 100644 --- a/src/ShapeType.php +++ b/src/ArrayShapeType.php @@ -10,10 +10,10 @@ * @template-covariant TArray of array * @implements Type */ -final class ShapeType implements Type +final class ArrayShapeType implements Type { /** - * @var array + * @var array */ public readonly array $elements; @@ -22,18 +22,18 @@ final class ShapeType implements Type /** * @internal * @psalm-internal Typhoon\Type - * @param array $elements + * @param array $elements */ public function __construct( array $elements = [], bool $sealed = true, ) { - $this->sealed = $sealed; $this->elements = $elements; + $this->sealed = $sealed; } public function accept(TypeVisitor $visitor): mixed { - return $visitor->visitShape($this); + return $visitor->visitArrayShape($this); } } diff --git a/src/TypeVisitor.php b/src/TypeVisitor.php index 76fb896..c3ab1b1 100644 --- a/src/TypeVisitor.php +++ b/src/TypeVisitor.php @@ -105,7 +105,7 @@ public function visitNonEmptyList(NonEmptyListType $type): mixed; public function visitList(ListType $type): mixed; /** @return TReturn */ - public function visitShape(ShapeType $type): mixed; + public function visitArrayShape(ArrayShapeType $type): mixed; /** @return TReturn */ public function visitNonEmptyArray(NonEmptyArrayType $type): mixed; diff --git a/src/types.php b/src/types.php index 235c33e..3311b7b 100644 --- a/src/types.php +++ b/src/types.php @@ -211,14 +211,14 @@ public static function list(Type $valueType = self::mixed): ListType /** * @psalm-pure - * @param array $elements + * @param array $elements */ - public static function shape(array $elements = [], bool $sealed = true): ShapeType + public static function arrayShape(array $elements = [], bool $sealed = true): ArrayShapeType { - return new ShapeType( + return new ArrayShapeType( array_map( - static fn (Type|ShapeElement $element): ShapeElement => $element instanceof Type - ? new ShapeElement($element) + static fn (Type|ArrayElement $element): ArrayElement => $element instanceof Type + ? new ArrayElement($element) : $element, $elements, ), @@ -226,35 +226,15 @@ public static function shape(array $elements = [], bool $sealed = true): ShapeTy ); } - /** - * @psalm-pure - * @param array $elements - */ - public static function unsealedShape(array $elements = []): ShapeType - { - return self::shape($elements, false); - } - - /** - * @psalm-pure - * @template TType - * @param Type $type - * @return ShapeElement - */ - public static function element(Type $type, bool $optional): ShapeElement - { - return new ShapeElement($type, $optional); - } - /** * @psalm-pure * @template TType * @param Type $type - * @return ShapeElement + * @return ArrayElement */ - public static function optional(Type $type): ShapeElement + public static function arrayElement(Type $type, bool $optional = false): ArrayElement { - return new ShapeElement($type, true); + return new ArrayElement($type, $optional); } /** @@ -343,28 +323,6 @@ public static function param(Type $type = self::mixed, bool $hasDefault = false, return new Parameter($type, $hasDefault, $variadic); } - /** - * @psalm-pure - * @template TType - * @param Type $type - * @return Parameter - */ - public static function defaultParam(Type $type = self::mixed): Parameter - { - return new Parameter($type, true); - } - - /** - * @psalm-pure - * @template TType - * @param Type $type - * @return Parameter - */ - public static function variadicParam(Type $type = self::mixed): Parameter - { - return new Parameter($type, variadic: true); - } - /** * @psalm-pure * @template TReturn diff --git a/tests/psalm/ArrayShapeTest.php b/tests/psalm/ArrayShapeTest.php new file mode 100644 index 0000000..5fb4e77 --- /dev/null +++ b/tests/psalm/ArrayShapeTest.php @@ -0,0 +1,20 @@ + */ +$_shapeType = new ArrayShapeType([ + 'a' => new ArrayElement(StringType::type, optional: true), + 10 => new ArrayElement(IntType::type), +]); +/** @psalm-check-type-exact $_shape = array{a?: string, 10: int} */ +$_shape = extractType($_shapeType); + +function testShapeIsCovariant(ArrayShapeType $_type): void {} + +testShapeIsCovariant($_shapeType); diff --git a/tests/psalm/ShapeTest.php b/tests/psalm/ShapeTest.php deleted file mode 100644 index 987c627..0000000 --- a/tests/psalm/ShapeTest.php +++ /dev/null @@ -1,20 +0,0 @@ - */ -$_shapeType = new ShapeType([ - 'a' => new ShapeElement(StringType::type, optional: true), - 10 => new ShapeElement(IntType::type), -]); -/** @psalm-check-type-exact $_shape = array{a?: string, 10: int} */ -$_shape = extractType($_shapeType); - -function testShapeIsCovariant(ShapeType $_type): void {} - -testShapeIsCovariant($_shapeType);