From 2dafbfde216b1f3ba90a0d730d3d28619c70edaf Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Sat, 24 Feb 2024 13:53:12 +0300 Subject: [PATCH] Refactored naming of most parameters --- README.md | 2 +- src/ArrayType.php | 10 +-- src/CallableType.php | 4 +- src/ClassConstantType.php | 4 +- src/ClosureType.php | 4 +- src/DefaultTypeVisitor.php | 158 +++++++++++++++++------------------ src/IterableType.php | 10 +-- src/ListType.php | 6 +- src/NamedClassStringType.php | 4 +- src/TypeVisitor.php | 126 ++++++++++++++-------------- src/types.php | 74 ++++++++-------- 11 files changed, 201 insertions(+), 201 deletions(-) diff --git a/README.md b/README.md index 8f6ad6d..2e0f179 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $type = types::arrayShape([ types::param(types::template('TSend', types::atClass(Generator::class)), hasDefault: true), types::param(types::scalar, variadic: true), ], - returnType: types::void, + return: types::void, ), ], sealed: false); ``` diff --git a/src/ArrayType.php b/src/ArrayType.php index c6ed39c..bbc0119 100644 --- a/src/ArrayType.php +++ b/src/ArrayType.php @@ -14,16 +14,16 @@ final class ArrayType implements Type { /** - * @param Type $keyType - * @param Type $valueType + * @param Type $key + * @param Type $value */ public function __construct( - private readonly Type $keyType, - private readonly Type $valueType, + private readonly Type $key, + private readonly Type $value, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->array($this, $this->keyType, $this->valueType); + return $visitor->array($this, $this->key, $this->value); } } diff --git a/src/CallableType.php b/src/CallableType.php index bbcb81e..195926e 100644 --- a/src/CallableType.php +++ b/src/CallableType.php @@ -16,11 +16,11 @@ final class CallableType implements Type */ public function __construct( private readonly array $parameters, - private readonly Type $returnType, + private readonly Type $return, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->callable($this, $this->parameters, $this->returnType); + return $visitor->callable($this, $this->parameters, $this->return); } } diff --git a/src/ClassConstantType.php b/src/ClassConstantType.php index 26dc01f..6a52d2e 100644 --- a/src/ClassConstantType.php +++ b/src/ClassConstantType.php @@ -15,12 +15,12 @@ final class ClassConstantType implements Type * @param non-empty-string $name */ public function __construct( - private readonly Type $classType, + private readonly Type $class, private readonly string $name, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->classConstant($this, $this->classType, $this->name); + return $visitor->classConstant($this, $this->class, $this->name); } } diff --git a/src/ClosureType.php b/src/ClosureType.php index 6a9ac00..d9c5925 100644 --- a/src/ClosureType.php +++ b/src/ClosureType.php @@ -16,11 +16,11 @@ final class ClosureType implements Type */ public function __construct( private readonly array $parameters, - private readonly Type $returnType, + private readonly Type $return, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->closure($this, $this->parameters, $this->returnType); + return $visitor->closure($this, $this->parameters, $this->return); } } diff --git a/src/DefaultTypeVisitor.php b/src/DefaultTypeVisitor.php index a1acb87..1cb31b2 100644 --- a/src/DefaultTypeVisitor.php +++ b/src/DefaultTypeVisitor.php @@ -11,203 +11,203 @@ */ abstract class DefaultTypeVisitor implements TypeVisitor { - public function alias(Type $type, string $class, string $name): mixed + public function alias(Type $self, string $class, string $name): mixed { - return $this->default($type); + return $this->default($self); } - public function anyLiteral(Type $type, Type $innerType): mixed + public function anyLiteral(Type $self, Type $type): mixed { - return $this->default($type); + return $this->default($self); } - public function array(Type $type, Type $keyType, Type $valueType): mixed + public function array(Type $self, Type $key, Type $value): mixed { - return $this->default($type); + return $this->default($self); } - public function arrayShape(Type $type, array $elements, bool $sealed): mixed + public function arrayShape(Type $self, array $elements, bool $sealed): mixed { - return $this->default($type); + return $this->default($self); } - public function bool(Type $type): mixed + public function bool(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function callable(Type $type, array $parameters, ?Type $returnType): mixed + public function callable(Type $self, array $parameters, ?Type $return): mixed { - return $this->default($type); + return $this->default($self); } - public function classConstant(Type $type, Type $classType, string $name): mixed + public function classConstant(Type $self, Type $class, string $name): mixed { - return $this->default($type); + return $this->default($self); } - public function classString(Type $type): mixed + public function classString(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function classStringLiteral(Type $type, string $class): mixed + public function classStringLiteral(Type $self, string $class): mixed { - return $this->default($type); + return $this->default($self); } - public function closure(Type $type, array $parameters, ?Type $returnType): mixed + public function closure(Type $self, array $parameters, ?Type $return): mixed { - return $this->default($type); + return $this->default($self); } - public function conditional(Type $type, Argument|Type $subject, Type $if, Type $then, Type $else): mixed + public function conditional(Type $self, Argument|Type $subject, Type $if, Type $then, Type $else): mixed { - return $this->default($type); + return $this->default($self); } - public function constant(Type $type, string $name): mixed + public function constant(Type $self, string $name): mixed { - return $this->default($type); + return $this->default($self); } - public function float(Type $type): mixed + public function float(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function int(Type $type): mixed + public function int(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function intersection(Type $type, array $types): mixed + public function intersection(Type $self, array $types): mixed { - return $this->default($type); + return $this->default($self); } - public function intMask(Type $type, Type $innerType): mixed + public function intMask(Type $self, Type $type): mixed { - return $this->default($type); + return $this->default($self); } - public function intRange(Type $type, ?int $min, ?int $max): mixed + public function intRange(Type $self, ?int $min, ?int $max): mixed { - return $this->default($type); + return $this->default($self); } - public function iterable(Type $type, Type $keyType, Type $valueType): mixed + public function iterable(Type $self, Type $key, Type $value): mixed { - return $this->default($type); + return $this->default($self); } - public function key(Type $type, Type $innerType): mixed + public function key(Type $self, Type $type): mixed { - return $this->default($type); + return $this->default($self); } - public function list(Type $type, Type $valueType): mixed + public function list(Type $self, Type $value): mixed { - return $this->default($type); + return $this->default($self); } - public function literal(Type $type, float|bool|int|string $value): mixed + public function literal(Type $self, float|bool|int|string $value): mixed { - return $this->default($type); + return $this->default($self); } - public function mixed(Type $type): mixed + public function mixed(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function namedClassString(Type $type, Type $objectType): mixed + public function namedClassString(Type $self, Type $object): mixed { - return $this->default($type); + return $this->default($self); } - public function namedObject(Type $type, string $class, array $templateArguments): mixed + public function namedObject(Type $self, string $class, array $templateArguments): mixed { - return $this->default($type); + return $this->default($self); } - public function never(Type $type): mixed + public function never(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function nonEmpty(Type $type, Type $innerType): mixed + public function nonEmpty(Type $self, Type $type): mixed { - return $this->default($type); + return $this->default($self); } - public function null(Type $type): mixed + public function null(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function numericString(Type $type): mixed + public function numericString(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function object(Type $type): mixed + public function object(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function objectShape(Type $type, array $properties): mixed + public function objectShape(Type $self, array $properties): mixed { - return $this->default($type); + return $this->default($self); } - public function offset(Type $type, Type $innerType, Type $offset): mixed + public function offset(Type $self, Type $type, Type $offset): mixed { - return $this->default($type); + return $this->default($self); } - public function resource(Type $type): mixed + public function resource(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function string(Type $type): mixed + public function string(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function template(Type $type, string $name, AtClass|AtFunction|AtMethod $declaredAt, Type $constraint): mixed + public function template(Type $self, string $name, AtClass|AtFunction|AtMethod $declaredAt, Type $constraint): mixed { - return $this->default($type); + return $this->default($self); } - public function truthyString(Type $type): mixed + public function truthyString(Type $self): mixed { - return $this->default($type); + return $this->default($self); } - public function union(Type $type, array $types): mixed + public function union(Type $self, array $types): mixed { - return $this->default($type); + return $this->default($self); } - public function value(Type $type, Type $innerType): mixed + public function value(Type $self, Type $type): mixed { - return $this->default($type); + return $this->default($self); } - public function varianceAware(Type $type, Type $innerType, Variance $variance): mixed + public function varianceAware(Type $self, Type $type, Variance $variance): mixed { - return $this->default($type); + return $this->default($self); } - public function void(Type $type): mixed + public function void(Type $self): mixed { - return $this->default($type); + return $this->default($self); } /** * @return TReturn */ - abstract protected function default(Type $type): mixed; + abstract protected function default(Type $self): mixed; } diff --git a/src/IterableType.php b/src/IterableType.php index 37b3190..94a9584 100644 --- a/src/IterableType.php +++ b/src/IterableType.php @@ -14,16 +14,16 @@ final class IterableType implements Type { /** - * @param Type $keyType - * @param Type $valueType + * @param Type $key + * @param Type $value */ public function __construct( - private readonly Type $keyType, - private readonly Type $valueType, + private readonly Type $key, + private readonly Type $value, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->iterable($this, $this->keyType, $this->valueType); + return $visitor->iterable($this, $this->key, $this->value); } } diff --git a/src/ListType.php b/src/ListType.php index 5c9dd57..0172d70 100644 --- a/src/ListType.php +++ b/src/ListType.php @@ -13,14 +13,14 @@ final class ListType implements Type { /** - * @param Type $valueType + * @param Type $value */ public function __construct( - private readonly Type $valueType, + private readonly Type $value, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->list($this, $this->valueType); + return $visitor->list($this, $this->value); } } diff --git a/src/NamedClassStringType.php b/src/NamedClassStringType.php index caaeca7..abd7638 100644 --- a/src/NamedClassStringType.php +++ b/src/NamedClassStringType.php @@ -12,11 +12,11 @@ final class NamedClassStringType implements Type { public function __construct( - private readonly Type $objectType, + private readonly Type $object, ) {} public function accept(TypeVisitor $visitor): mixed { - return $visitor->namedClassString($this, $this->objectType); + return $visitor->namedClassString($this, $this->object); } } diff --git a/src/TypeVisitor.php b/src/TypeVisitor.php index d19a179..b58f58b 100644 --- a/src/TypeVisitor.php +++ b/src/TypeVisitor.php @@ -15,231 +15,231 @@ interface TypeVisitor * @param non-empty-string $name * @return TReturn */ - public function alias(Type $type, string $class, string $name): mixed; + public function alias(Type $self, string $class, string $name): mixed; /** * @return TReturn */ - public function anyLiteral(Type $type, Type $innerType): mixed; + public function anyLiteral(Type $self, Type $type): mixed; /** - * @param Type> $type + * @param Type> $self * @return TReturn */ - public function array(Type $type, Type $keyType, Type $valueType): mixed; + public function array(Type $self, Type $key, Type $value): mixed; /** - * @param Type> $type + * @param Type> $self * @param array $elements * @return TReturn */ - public function arrayShape(Type $type, array $elements, bool $sealed): mixed; + public function arrayShape(Type $self, array $elements, bool $sealed): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function bool(Type $type): mixed; + public function bool(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @param list $parameters * @return TReturn */ - public function callable(Type $type, array $parameters, Type $returnType): mixed; + public function callable(Type $self, array $parameters, Type $return): mixed; /** * @param non-empty-string $name * @return TReturn */ - public function classConstant(Type $type, Type $classType, string $name): mixed; + public function classConstant(Type $self, Type $class, string $name): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function classString(Type $type): mixed; + public function classString(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @param non-empty-string $class * @return TReturn */ - public function classStringLiteral(Type $type, string $class): mixed; + public function classStringLiteral(Type $self, string $class): mixed; /** - * @param Type<\Closure> $type + * @param Type<\Closure> $self * @param list $parameters * @return TReturn */ - public function closure(Type $type, array $parameters, Type $returnType): mixed; + public function closure(Type $self, array $parameters, Type $return): mixed; /** * @return TReturn */ - public function conditional(Type $type, Argument|Type $subject, Type $if, Type $then, Type $else): mixed; + public function conditional(Type $self, Argument|Type $subject, Type $if, Type $then, Type $else): mixed; /** * @param non-empty-string $name * @return TReturn */ - public function constant(Type $type, string $name): mixed; + public function constant(Type $self, string $name): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function float(Type $type): mixed; + public function float(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function int(Type $type): mixed; + public function int(Type $self): mixed; /** * @param non-empty-list $types * @return TReturn */ - public function intersection(Type $type, array $types): mixed; + public function intersection(Type $self, array $types): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function intMask(Type $type, Type $innerType): mixed; + public function intMask(Type $self, Type $type): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function intRange(Type $type, ?int $min, ?int $max): mixed; + public function intRange(Type $self, ?int $min, ?int $max): mixed; /** - * @param Type> $type + * @param Type> $self * @return TReturn */ - public function iterable(Type $type, Type $keyType, Type $valueType): mixed; + public function iterable(Type $self, Type $key, Type $value): mixed; /** * @return TReturn */ - public function key(Type $type, Type $innerType): mixed; + public function key(Type $self, Type $type): mixed; /** - * @param Type> $type + * @param Type> $self * @return TReturn */ - public function list(Type $type, Type $valueType): mixed; + public function list(Type $self, Type $value): mixed; /** * @return TReturn */ - public function literal(Type $type, bool|int|float|string $value): mixed; + public function literal(Type $self, bool|int|float|string $value): mixed; /** * @return TReturn */ - public function mixed(Type $type): mixed; + public function mixed(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function namedClassString(Type $type, Type $objectType): mixed; + public function namedClassString(Type $self, Type $object): mixed; /** - * @param Type $type + * @param Type $self * @param non-empty-string $class * @param list $templateArguments * @return TReturn */ - public function namedObject(Type $type, string $class, array $templateArguments): mixed; + public function namedObject(Type $self, string $class, array $templateArguments): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function never(Type $type): mixed; + public function never(Type $self): mixed; /** * @return TReturn */ - public function nonEmpty(Type $type, Type $innerType): mixed; + public function nonEmpty(Type $self, Type $type): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function null(Type $type): mixed; + public function null(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function numericString(Type $type): mixed; + public function numericString(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function object(Type $type): mixed; + public function object(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @param array $properties * @return TReturn */ - public function objectShape(Type $type, array $properties): mixed; + public function objectShape(Type $self, array $properties): mixed; /** * @return TReturn */ - public function offset(Type $type, Type $innerType, Type $offset): mixed; + public function offset(Type $self, Type $type, Type $offset): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function resource(Type $type): mixed; + public function resource(Type $self): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function string(Type $type): mixed; + public function string(Type $self): mixed; /** * @param non-empty-string $name * @return TReturn */ - public function template(Type $type, string $name, AtFunction|AtClass|AtMethod $declaredAt, Type $constraint): mixed; + public function template(Type $self, string $name, AtFunction|AtClass|AtMethod $declaredAt, Type $constraint): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function truthyString(Type $type): mixed; + public function truthyString(Type $self): mixed; /** * @param non-empty-list $types * @return TReturn */ - public function union(Type $type, array $types): mixed; + public function union(Type $self, array $types): mixed; /** * @return TReturn */ - public function value(Type $type, Type $innerType): mixed; + public function value(Type $self, Type $type): mixed; /** * @return TReturn */ - public function varianceAware(Type $type, Type $innerType, Variance $variance): mixed; + public function varianceAware(Type $self, Type $type, Variance $variance): mixed; /** - * @param Type $type + * @param Type $self * @return TReturn */ - public function void(Type $type): mixed; + public function void(Type $self): mixed; } diff --git a/src/types.php b/src/types.php index f75f5c2..ad830a2 100644 --- a/src/types.php +++ b/src/types.php @@ -70,17 +70,17 @@ public static function arg(string $name): Argument /** * @template TKey * @template TValue - * @param Type $keyType - * @param Type $valueType + * @param Type $key + * @param Type $value * @return Type> */ - public static function array(Type $keyType = self::arrayKey, Type $valueType = self::mixed): Type + public static function array(Type $key = self::arrayKey, Type $value = self::mixed): Type { - if ($keyType === self::arrayKey && $valueType === self::mixed) { + if ($key === self::arrayKey && $value === self::mixed) { return self::array; } - return new ArrayType($keyType, $valueType); + return new ArrayType($key, $value); } /** @@ -136,12 +136,12 @@ public static function atMethod(string $class, string $name): AtMethod /** * @template TReturn * @param list $parameters - * @param Type $returnType + * @param Type $return * @return Type */ - public static function callable(array $parameters = [], Type $returnType = self::mixed): Type + public static function callable(array $parameters = [], Type $return = self::mixed): Type { - if ($parameters === [] && $returnType === self::mixed) { + if ($parameters === [] && $return === self::mixed) { return self::callable; } @@ -150,25 +150,25 @@ public static function callable(array $parameters = [], Type $returnType = self: static fn(Type|Parameter $parameter): Parameter => $parameter instanceof Type ? new Parameter($parameter) : $parameter, $parameters, ), - $returnType, + $return, ); } /** * @param non-empty-string $name */ - public static function classConstant(Type $classType, string $name): Type + public static function classConstant(Type $class, string $name): Type { - return new ClassConstantType($classType, $name); + return new ClassConstantType($class, $name); } /** * @template TObject of object - * @return ($objectType is Type ? Type> : Type) + * @return ($object is Type ? Type> : Type) */ - public static function classString(Type $objectType): Type + public static function classString(Type $object): Type { - return new NamedClassStringType($objectType); + return new NamedClassStringType($object); } /** @@ -185,9 +185,9 @@ public static function classStringLiteral(string $class): Type * @param list $parameters * @return Type<\Closure> */ - public static function closure(array $parameters = [], Type $returnType = self::mixed): Type + public static function closure(array $parameters = [], Type $return = self::mixed): Type { - if ($parameters === [] && $returnType === self::mixed) { + if ($parameters === [] && $return === self::mixed) { return self::closure; } @@ -196,7 +196,7 @@ public static function closure(array $parameters = [], Type $returnType = self:: static fn(Type|Parameter $parameter): Parameter => $parameter instanceof Type ? new Parameter($parameter) : $parameter, $parameters, ), - $returnType, + $return, ); } @@ -216,9 +216,9 @@ public static function constant(string $name): Type /** * @no-named-arguments */ - public static function intersection(Type $type1, Type $type2, Type ...$moreTypes): Type + public static function intersection(Type $type1, Type $type2, Type ...$types): Type { - return new IntersectionType([$type1, $type2, ...$moreTypes]); + return new IntersectionType([$type1, $type2, ...$types]); } /** @@ -244,17 +244,17 @@ public static function intRange(?int $min = null, ?int $max = null): Type /** * @template TKey * @template TValue - * @param Type $keyType - * @param Type $valueType + * @param Type $key + * @param Type $value * @return Type> */ - public static function iterable(Type $keyType = self::mixed, Type $valueType = self::mixed): Type + public static function iterable(Type $key = self::mixed, Type $value = self::mixed): Type { - if ($keyType === self::mixed && $valueType === self::mixed) { + if ($key === self::mixed && $value === self::mixed) { return self::iterable; } - return new IterableType($keyType, $valueType); + return new IterableType($key, $value); } public static function key(Type $type): Type @@ -264,12 +264,12 @@ public static function key(Type $type): Type /** * @template TValue - * @param Type $valueType + * @param Type $value * @return Type> */ - public static function list(Type $valueType = self::mixed): Type + public static function list(Type $value = self::mixed): Type { - return new ListType($valueType); + return new ListType($value); } /** @@ -285,27 +285,27 @@ public static function literal(bool|int|float|string $value): Type /** * @template TKey * @template TValue - * @param Type $keyType - * @param Type $valueType + * @param Type $key + * @param Type $value * @return Type> * @psalm-suppress MoreSpecificReturnType, LessSpecificReturnStatement */ - public static function nonEmptyArray(Type $keyType = self::arrayKey, Type $valueType = self::mixed): Type + public static function nonEmptyArray(Type $key = self::arrayKey, Type $value = self::mixed): Type { /** @phpstan-ignore return.type */ - return new NonEmptyType(self::array($keyType, $valueType)); + return new NonEmptyType(self::array($key, $value)); } /** * @template TValue - * @param Type $valueType + * @param Type $value * @return Type> * @psalm-suppress InvalidReturnType, InvalidReturnStatement */ - public static function nonEmptyList(Type $valueType = self::mixed): Type + public static function nonEmptyList(Type $value = self::mixed): Type { /** @phpstan-ignore return.type */ - return new NonEmptyType(self::list($valueType)); + return new NonEmptyType(self::list($value)); } /** @@ -379,12 +379,12 @@ public static function template(string $name, AtMethod|AtClass|AtFunction $decla * @template TType * @param Type $type1 * @param Type $type2 - * @param Type ...$moreTypes + * @param Type ...$types * @return Type */ - public static function union(Type $type1, Type $type2, Type ...$moreTypes): Type + public static function union(Type $type1, Type $type2, Type ...$types): Type { - return new UnionType([$type1, $type2, ...$moreTypes]); + return new UnionType([$type1, $type2, ...$types]); } public static function value(Type $type): Type