Skip to content

Commit cccfd69

Browse files
committed
Complete exceptions refactoring
1 parent f364362 commit cccfd69

21 files changed

Lines changed: 261 additions & 187 deletions

src/Exception/Definition/Template/Hint/TemplateArgumentHintsNotSupportedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function becauseTooManyHints(
2121
. 'but "{{hint}}" were passed';
2222

2323
assert($argument->hint !== null, new \InvalidArgumentException(
24-
'Incorrect exception usage',
24+
'Semantic Violation: Argument should contain argument hint',
2525
));
2626

2727
return new self(

src/Exception/Definition/Template/MissingTemplateArgumentsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function becauseNoRequiredArgument(
2222
$passedArgumentsCount = $type->arguments?->count() ?? 0;
2323

2424
assert($passedArgumentsCount < $minArgumentsCount, new \InvalidArgumentException(
25-
'Incorrect exception usage',
25+
'Semantic Violation: Passed type`s argument count should be less than min bound',
2626
));
2727

2828
$template = 'Type "{{type}}" expects at least {{expectedArgumentsCount}}'

src/Exception/Definition/Template/MissingTemplateArgumentsInRangeException.php

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,37 @@
88

99
/**
1010
* Occurs when a type requires more template arguments to be specified than required
11-
*
12-
* @deprecated TODO
1311
*/
1412
class MissingTemplateArgumentsInRangeException extends TemplateArgumentsInRangeException
1513
{
1614
/**
17-
* @param int<0, max> $minSupportedArgumentsCount
18-
* @param int<1, max> $maxSupportedArgumentsCount
15+
* @param int<0, max> $minArgumentsCount
16+
* @param int<1, max> $maxArgumentsCount
1917
*/
20-
public static function becauseTemplateArgumentsRequired(
21-
int $minSupportedArgumentsCount,
22-
int $maxSupportedArgumentsCount,
18+
public static function becauseNoRequiredArgument(
19+
int $minArgumentsCount,
20+
int $maxArgumentsCount,
2321
NamedTypeNode $type,
2422
?\Throwable $previous = null
2523
): self {
26-
$template = 'Type "{{type}}" expects at least %s template argument(s), '
27-
. 'but {{passedArgumentsCount}} were passed';
24+
$passedArgumentsCount = $type->arguments?->count() ?? 0;
2825

29-
$template = $minSupportedArgumentsCount === $maxSupportedArgumentsCount
30-
? \sprintf($template, '{{minSupportedArgumentsCount}}')
31-
: \sprintf($template, 'from {{minSupportedArgumentsCount}} to {{maxSupportedArgumentsCount}}');
26+
assert($maxArgumentsCount > $minArgumentsCount, new \InvalidArgumentException(
27+
'Semantic Violation: Passed max bound must be greater than min bound',
28+
));
3229

33-
return new self(
34-
passedArgumentsCount: $type->arguments?->count() ?? 0,
35-
minSupportedArgumentsCount: $minSupportedArgumentsCount,
36-
maxSupportedArgumentsCount: $maxSupportedArgumentsCount,
37-
type: $type,
38-
template: $template,
39-
previous: $previous,
40-
);
41-
}
30+
assert($passedArgumentsCount < $minArgumentsCount, new \InvalidArgumentException(
31+
'Semantic Violation: Passed type`s argument count should be greater than min bound',
32+
));
4233

43-
/**
44-
* @param int<1, max> $maxSupportedArgumentsCount
45-
*/
46-
public static function becauseNoMoreTemplateArguments(
47-
int $maxSupportedArgumentsCount,
48-
NamedTypeNode $type,
49-
?\Throwable $previous = null,
50-
): self {
51-
$template = 'Type "{{type}}" only accepts {{maxSupportedArgumentsCount}} template argument(s), '
52-
. 'but {{passedArgumentsCount}} were passed';
34+
$template = 'Type "{{type}}" expects at least from {{minArgumentsCount}}'
35+
. ' to {{maxArgumentsCount}} template argument(s),'
36+
. ' but {{passedArgumentsCount}} were passed';
5337

5438
return new self(
55-
passedArgumentsCount: $type->arguments?->count() ?? 0,
56-
minSupportedArgumentsCount: 0,
57-
maxSupportedArgumentsCount: $maxSupportedArgumentsCount,
39+
passedArgumentsCount: $passedArgumentsCount,
40+
minArgumentsCount: $minArgumentsCount,
41+
maxArgumentsCount: $maxArgumentsCount,
5842
type: $type,
5943
template: $template,
6044
previous: $previous,

src/Exception/Definition/Template/OneOfTemplateArgumentsCountException.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,52 +34,33 @@ public function __construct(
3434
}
3535

3636
/**
37-
* @param array<array-key, int<0, max>> $variants
37+
* @param non-empty-array<array-key, int<0, max>> $variants
3838
*/
3939
public static function becauseArgumentsCountDoesNotMatch(
4040
array $variants,
4141
NamedTypeNode $type,
4242
?\Throwable $previous = null,
43-
): self|TemplateArgumentsException {
43+
): self {
44+
assert(\count($variants) !== 0, new \InvalidArgumentException(
45+
'Semantic Violation: Argument variants should be greater than 0',
46+
));
47+
4448
$passedArgumentsCount = $type->arguments?->count() ?? 0;
4549

4650
assert(!\in_array($passedArgumentsCount, $variants, true), new \InvalidArgumentException(
47-
'Incorrect exception usage',
51+
'Semantic Violation: Passed arguments count should not be in variants',
4852
));
4953

50-
$simplified = self::simplifyException($variants, $type, $previous);
51-
52-
if ($simplified !== null) {
53-
return $simplified;
54-
}
55-
5654
$template = 'Type "{{type}}" only accepts {{expectedArgumentCountVariants}}'
5755
. ' template argument(s), but {{passedArgumentsCount}} were passed';
5856

5957
/** @var non-empty-array<array-key, int<0, max>> $variants */
6058
return new self(
61-
passedArgumentsCount: $passedArgumentsCount,
59+
passedArgumentsCount: $type->arguments?->count() ?? 0,
6260
expectedArgumentCountVariants: \array_values($variants),
6361
type: $type,
6462
template: $template,
6563
previous: $previous,
6664
);
6765
}
68-
69-
/**
70-
* @param array<array-key, int<0, max>> $variants
71-
*/
72-
private static function simplifyException(
73-
array $variants,
74-
NamedTypeNode $type,
75-
?\Throwable $previous = null,
76-
): ?TemplateArgumentsException {
77-
return match (\count($variants)) {
78-
0 => TemplateArgumentsNotSupportedException::becauseTooManyArguments($type),
79-
1 => ($expectedArgumentsCount = \reset($variants)) < ($type->arguments?->count() ?? 0)
80-
? TooManyTemplateArgumentsException::becauseHasRedundantArgument($expectedArgumentsCount, $type, $previous)
81-
: MissingTemplateArgumentsException::becauseNoRequiredArgument($expectedArgumentsCount, $type, $previous),
82-
default => null,
83-
};
84-
}
8566
}

src/Exception/Definition/Template/TemplateArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function getArgumentIndex(NamedTypeNode $type, TemplateArgumentNode $arg
3434
$index = $type->arguments?->findIndex($argument);
3535

3636
assert($index !== null, new \InvalidArgumentException(
37-
'Template argument is not a part of passed type',
37+
'Semantic Violation: Template argument is not a part of passed type',
3838
));
3939

4040
return $index + 1;

src/Exception/Definition/Template/TemplateArgumentsInRangeException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
/**
1010
* Group of errors related to incorrect number (range) of template arguments
11-
*
12-
* @deprecated TODO
1311
*/
1412
abstract class TemplateArgumentsInRangeException extends TemplateArgumentsException
1513
{
@@ -21,11 +19,11 @@ public function __construct(
2119
/**
2220
* @var int<0, max>
2321
*/
24-
public readonly int $minSupportedArgumentsCount,
22+
public readonly int $minArgumentsCount,
2523
/**
2624
* @var int<0, max>
2725
*/
28-
public readonly int $maxSupportedArgumentsCount,
26+
public readonly int $maxArgumentsCount,
2927
NamedTypeNode $type,
3028
string $template,
3129
int $code = 0,

src/Exception/Definition/Template/TemplateArgumentsNotSupportedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function becauseTooManyArguments(
1818
$passedArgumentsCount = $type->arguments?->count() ?? 0;
1919

2020
assert($passedArgumentsCount > 0, new \InvalidArgumentException(
21-
'Incorrect exception usage',
21+
'Semantic Violation: Passed type should contain template arguments',
2222
));
2323

2424
$template = 'Type "{{type}}" does not support template arguments, '

src/Exception/Definition/Template/TooManyTemplateArgumentsException.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ public static function becauseHasRedundantArgument(
1818
int $maxArgumentsCount,
1919
NamedTypeNode $type,
2020
?\Throwable $previous = null,
21-
): self|TemplateArgumentsException {
21+
): self {
2222
$passedArgumentsCount = $type->arguments?->count() ?? 0;
2323

2424
assert($passedArgumentsCount > $maxArgumentsCount, new \InvalidArgumentException(
25-
'Incorrect exception usage',
25+
'Semantic Violation: Passed type`s argument count should be greater than max bound',
2626
));
2727

28-
$simplified = self::simplifyException($maxArgumentsCount, $type, $previous);
29-
30-
if ($simplified !== null) {
31-
return $simplified;
32-
}
33-
3428
$template = 'Type "{{type}}" only accepts {{expectedArgumentsCount}}'
3529
. ' template argument(s), but {{passedArgumentsCount}} were passed';
3630

@@ -42,19 +36,4 @@ public static function becauseHasRedundantArgument(
4236
previous: $previous,
4337
);
4438
}
45-
46-
/**
47-
* @param int<0, max> $maxArgumentsCount
48-
*/
49-
private static function simplifyException(
50-
int $maxArgumentsCount,
51-
NamedTypeNode $type,
52-
?\Throwable $previous = null,
53-
): ?TemplateArgumentsException {
54-
if ($maxArgumentsCount <= 0) {
55-
return TemplateArgumentsNotSupportedException::becauseTooManyArguments($type, $previous);
56-
}
57-
58-
return null;
59-
}
6039
}

src/Exception/Definition/Template/TooManyTemplateArgumentsInRangeException.php

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,43 @@
88

99
/**
1010
* Occurs when a type supports fewer arguments than were passed
11-
*
12-
* @deprecated TODO
1311
*/
1412
class TooManyTemplateArgumentsInRangeException extends TemplateArgumentsInRangeException
1513
{
1614
public const ERROR_CODE_IN_RANGE = 0x01;
17-
public const ERROR_CODE_NO_MORE_THAN = 0x02;
1815

1916
/**
20-
* @param int<0, max> $minSupportedArgumentsCount
21-
* @param int<1, max> $maxSupportedArgumentsCount
17+
* @param int<0, max> $minArgumentsCount
18+
* @param int<1, max> $maxArgumentsCount
2219
*/
23-
public static function becauseTooManyThanRangeTemplateArguments(
24-
int $minSupportedArgumentsCount,
25-
int $maxSupportedArgumentsCount,
20+
public static function becauseHasRedundantArgument(
21+
int $minArgumentsCount,
22+
int $maxArgumentsCount,
2623
NamedTypeNode $type,
2724
?\Throwable $previous = null,
28-
): self {
29-
if ($minSupportedArgumentsCount === 0 || $minSupportedArgumentsCount === $maxSupportedArgumentsCount) {
30-
return self::becauseTooManyTemplateArguments($maxSupportedArgumentsCount, $type, $previous);
31-
}
25+
): self|TemplateArgumentsException {
26+
$passedArgumentsCount = $type->arguments?->count() ?? 0;
3227

33-
if ($maxSupportedArgumentsCount < $minSupportedArgumentsCount) {
34-
[$minSupportedArgumentsCount, $maxSupportedArgumentsCount]
35-
= [$maxSupportedArgumentsCount, $minSupportedArgumentsCount];
36-
}
28+
assert($maxArgumentsCount > $minArgumentsCount, new \InvalidArgumentException(
29+
'Semantic Violation: Passed max bound must be greater than min bound',
30+
));
3731

38-
$template = 'Type "{{type}}" only accepts from {{minSupportedArgumentsCount}}'
39-
. ' to {{maxSupportedArgumentsCount}} template argument(s),'
32+
assert($passedArgumentsCount > $maxArgumentsCount, new \InvalidArgumentException(
33+
'Semantic Violation: Passed type`s argument count should be greater than max bound',
34+
));
35+
36+
$template = 'Type "{{type}}" only accepts from {{minArgumentsCount}}'
37+
. ' to {{maxArgumentsCount}} template argument(s),'
4038
. ' but {{passedArgumentsCount}} were passed';
4139

4240
return new self(
4341
passedArgumentsCount: $type->arguments?->count() ?? 0,
44-
minSupportedArgumentsCount: $minSupportedArgumentsCount,
45-
maxSupportedArgumentsCount: $maxSupportedArgumentsCount,
42+
minArgumentsCount: $minArgumentsCount,
43+
maxArgumentsCount: $maxArgumentsCount,
4644
type: $type,
4745
template: $template,
4846
code: self::ERROR_CODE_IN_RANGE,
4947
previous: $previous,
5048
);
5149
}
52-
53-
/**
54-
* @param int<1, max> $requiredArgumentsCount
55-
*/
56-
public static function becauseTooManyTemplateArguments(
57-
int $requiredArgumentsCount,
58-
NamedTypeNode $type,
59-
?\Throwable $previous = null,
60-
): self {
61-
$template = 'Type "{{type}}" only accepts {{maxSupportedArgumentsCount}} template argument(s), '
62-
. 'but {{passedArgumentsCount}} were passed';
63-
64-
return new self(
65-
passedArgumentsCount: $type->arguments?->count() ?? 0,
66-
minSupportedArgumentsCount: 0,
67-
maxSupportedArgumentsCount: $requiredArgumentsCount,
68-
type: $type,
69-
template: $template,
70-
code: self::ERROR_CODE_NO_MORE_THAN,
71-
previous: $previous,
72-
);
73-
}
7450
}

src/Type/Builder/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ protected function expectTemplateArgumentsLessOrEqualThan(NamedTypeNode $stmt, i
9292
return;
9393
}
9494

95-
throw TooManyTemplateArgumentsInRangeException::becauseTooManyThanRangeTemplateArguments(
96-
minSupportedArgumentsCount: $min,
97-
maxSupportedArgumentsCount: $max,
95+
throw TooManyTemplateArgumentsInRangeException::becauseHasRedundantArgument(
96+
minArgumentsCount: $min,
97+
maxArgumentsCount: $max,
9898
type: $stmt,
9999
);
100100
}
@@ -128,9 +128,9 @@ protected function expectTemplateArgumentsGreaterOrEqualThan(NamedTypeNode $stmt
128128
return;
129129
}
130130

131-
throw MissingTemplateArgumentsInRangeException::becauseTemplateArgumentsRequired(
132-
minSupportedArgumentsCount: $min,
133-
maxSupportedArgumentsCount: $max ?? $min,
131+
throw MissingTemplateArgumentsInRangeException::becauseNoRequiredArgument(
132+
minArgumentsCount: $min,
133+
maxArgumentsCount: $max ?? $min,
134134
type: $stmt,
135135
);
136136
}

0 commit comments

Comments
 (0)