Skip to content

Commit c26d43c

Browse files
author
Kirill Nesmeyanov
committed
Improve types
1 parent 26354ea commit c26d43c

13 files changed

+65
-22
lines changed

src/Type/ArrayKeyType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class ArrayKeyType extends UnionType
88
{
99
public function __construct()
1010
{
11-
parent::__construct([new IntType(), new StringType()]);
11+
parent::__construct([
12+
new IntType(),
13+
new StringType(),
14+
]);
1215
}
1316
}

src/Type/ArrayType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
88
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
9+
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
910
use TypeLang\Mapper\Runtime\Context\LocalContext;
1011
use TypeLang\Mapper\Runtime\Path\Entry\ArrayIndexEntry;
1112

@@ -54,6 +55,8 @@ public function match(mixed $value, LocalContext $context): bool
5455
* @return array<array-key, mixed>
5556
* @throws InvalidValueException
5657
* @throws TypeNotFoundException
58+
* @throws \Throwable
59+
* @throws RuntimeExceptionInterface
5760
*/
5861
public function cast(mixed $value, LocalContext $context): array
5962
{

src/Type/BackedEnumType.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TypeLang\Mapper\Type;
66

77
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
8+
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
89
use TypeLang\Mapper\Runtime\Context\LocalContext;
910

1011
class BackedEnumType extends AsymmetricType
@@ -40,6 +41,10 @@ public function normalize(mixed $value, LocalContext $context): int|string
4041
return $value->value;
4142
}
4243

44+
/**
45+
* @throws \Throwable
46+
* @throws RuntimeExceptionInterface
47+
*/
4348
protected function isDenormalizable(mixed $value, LocalContext $context): bool
4449
{
4550
$isSupportsType = $this->type->match($value, $context);
@@ -62,6 +67,8 @@ protected function isDenormalizable(mixed $value, LocalContext $context): bool
6267
* Converts a scalar representation of an enum to an enum case object.
6368
*
6469
* @throws InvalidValueException
70+
* @throws RuntimeExceptionInterface
71+
* @throws \Throwable
6572
*/
6673
public function denormalize(mixed $value, LocalContext $context): \BackedEnum
6774
{

src/Type/Builder/BackedEnumTypeBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function isSupported(TypeStatement $statement): bool
2525
$enum = $statement->name->toString();
2626

2727
return \enum_exists($statement->name->toString())
28-
&& \is_subclass_of($enum, \BackedEnum::class, true);
28+
&& \is_subclass_of($enum, \BackedEnum::class);
2929
}
3030

3131
/**

src/Type/Builder/IntTypeBuilder.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use TypeLang\Parser\Node\Literal\IntLiteralNode;
1313
use TypeLang\Parser\Node\Literal\StringLiteralNode;
1414
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
15-
use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode;
15+
use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode as ArgNode;
1616
use TypeLang\Parser\Node\Stmt\TypeStatement;
1717
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
1818

@@ -29,13 +29,8 @@ public function build(TypeStatement $statement, RepositoryInterface $types): Int
2929

3030
return match (\count($arguments)) {
3131
0 => new IntType(),
32-
1 => new IntType(
33-
min: $this->fetchTemplateArgumentValue($statement, $arguments[0]),
34-
),
35-
2 => new IntType(
36-
min: $this->fetchTemplateArgumentValue($statement, $arguments[0]),
37-
max: $this->fetchTemplateArgumentValue($statement, $arguments[1]),
38-
),
32+
1 => $this->buildWithMinValue($statement, $arguments[0]),
33+
2 => $this->buildWithMinMaxValues($statement, $arguments[0], $arguments[1]),
3934
default => throw TooManyTemplateArgumentsException::becauseTemplateArgumentsRangeOverflows(
4035
passedArgumentsCount: \count($arguments),
4136
minSupportedArgumentsCount: 0,
@@ -49,7 +44,30 @@ public function build(TypeStatement $statement, RepositoryInterface $types): Int
4944
* @throws InvalidTemplateArgumentException
5045
* @throws TemplateArgumentHintsNotSupportedException
5146
*/
52-
private function fetchTemplateArgumentValue(TypeStatement $statement, TemplateArgumentNode $argument): int
47+
private function buildWithMinValue(NamedTypeNode $statement, ArgNode $min): IntType
48+
{
49+
$value = $this->fetchTemplateArgumentValue($statement, $min);
50+
51+
return new IntType($value);
52+
}
53+
54+
/**
55+
* @throws InvalidTemplateArgumentException
56+
* @throws TemplateArgumentHintsNotSupportedException
57+
*/
58+
private function buildWithMinMaxValues(NamedTypeNode $statement, ArgNode $min, ArgNode $max): IntType
59+
{
60+
$from = $this->fetchTemplateArgumentValue($statement, $min);
61+
$to = $this->fetchTemplateArgumentValue($statement, $max);
62+
63+
return new IntType($from, $to);
64+
}
65+
66+
/**
67+
* @throws InvalidTemplateArgumentException
68+
* @throws TemplateArgumentHintsNotSupportedException
69+
*/
70+
private function fetchTemplateArgumentValue(TypeStatement $statement, ArgNode $argument): int
5371
{
5472
$this->expectNoTemplateArgumentHint($statement, $argument);
5573

src/Type/FloatLiteralType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function match(mixed $value, LocalContext $context): bool
1919
return $value === (float) $this->value;
2020
}
2121

22+
/**
23+
* @throws InvalidValueException
24+
*/
2225
#[\Override]
2326
public function cast(mixed $value, LocalContext $context): float
2427
{

src/Type/IntLiteralType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function match(mixed $value, LocalContext $context): bool
2121
return $value === $this->value;
2222
}
2323

24+
/**
25+
* @throws InvalidValueException
26+
*/
2427
#[\Override]
2528
public function cast(mixed $value, LocalContext $context): int
2629
{

src/Type/MixedType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function match(mixed $value, LocalContext $context): bool
1616
}
1717

1818
/**
19-
* @throws \Throwable
2019
* @throws TypeNotFoundException
2120
* @throws RuntimeExceptionInterface
21+
* @throws \Throwable
2222
*/
2323
public function cast(mixed $value, LocalContext $context): mixed
2424
{

src/Type/NamedType.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Type/NonEmpty.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TypeLang\Mapper\Type;
66

77
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
8+
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
89
use TypeLang\Mapper\Runtime\Context\LocalContext;
910

1011
class NonEmpty implements TypeInterface
@@ -26,6 +27,8 @@ public function match(mixed $value, LocalContext $context): bool
2627

2728
/**
2829
* @throws InvalidValueException
30+
* @throws RuntimeExceptionInterface
31+
* @throws \Throwable
2932
*/
3033
public function cast(mixed $value, LocalContext $context): mixed
3134
{

0 commit comments

Comments
 (0)