Skip to content

Commit f378568

Browse files
committed
Improve exception templates and replace methods to properties
1 parent 0c6adb3 commit f378568

30 files changed

+292
-361
lines changed

src/Exception/Definition/DefinitionException.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ abstract class DefinitionException extends \InvalidArgumentException implements
1717
public readonly Template $template;
1818

1919
public function __construct(
20-
protected readonly TypeStatement $type,
20+
/**
21+
* Gets the type statement whose definition caused the error.
22+
*/
23+
public readonly TypeStatement $type,
2124
string $template,
2225
int $code = 0,
2326
?\Throwable $previous = null,
@@ -28,16 +31,6 @@ public function __construct(
2831
$this->message = $this->template = new Template($template, $this);
2932
}
3033

31-
/**
32-
* Returns the type statement whose definition caused the error.
33-
*
34-
* @api
35-
*/
36-
public function getType(): TypeStatement
37-
{
38-
return $this->type;
39-
}
40-
4134
/**
4235
* @api
4336
*

src/Exception/Definition/PropertyTypeNotFoundException.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
class PropertyTypeNotFoundException extends TypeNotFoundException
1010
{
11-
/**
12-
* @param class-string $class
13-
* @param non-empty-string $property
14-
*/
1511
public function __construct(
16-
protected readonly string $class,
17-
protected readonly string $property,
12+
/**
13+
* @var class-string
14+
*/
15+
public readonly string $class,
16+
/**
17+
* @var non-empty-string
18+
*/
19+
public readonly string $property,
1820
TypeStatement $type,
1921
string $template,
2022
int $code = 0,
@@ -44,24 +46,4 @@ class: $class,
4446
previous: $previous,
4547
);
4648
}
47-
48-
/**
49-
* @api
50-
*
51-
* @return class-string
52-
*/
53-
public function getClass(): string
54-
{
55-
return $this->class;
56-
}
57-
58-
/**
59-
* @api
60-
*
61-
* @return non-empty-string
62-
*/
63-
public function getProperty(): string
64-
{
65-
return $this->property;
66-
}
6749
}

src/Exception/Definition/Shape/ShapeFieldException.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,12 @@
1313
abstract class ShapeFieldException extends ShapeFieldsException
1414
{
1515
public function __construct(
16-
protected readonly FieldNode $field,
16+
public readonly FieldNode $field,
1717
TypeStatement $type,
1818
string $template,
1919
int $code = 0,
2020
?\Throwable $previous = null,
2121
) {
2222
parent::__construct($type, $template, $code, $previous);
2323
}
24-
25-
/**
26-
* @api
27-
*/
28-
public function getField(): FieldNode
29-
{
30-
return $this->field;
31-
}
3224
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
abstract class TemplateArgumentHintException extends TemplateArgumentHintsException
1515
{
16-
protected readonly ?Identifier $hint;
16+
public readonly ?Identifier $hint;
1717

1818
public function __construct(
1919
TemplateArgumentNode $argument,
@@ -32,12 +32,4 @@ public function __construct(
3232
previous: $previous,
3333
);
3434
}
35-
36-
/**
37-
* @api
38-
*/
39-
public function findArgumentHint(): ?Identifier
40-
{
41-
return $this->hint;
42-
}
4335
}

src/Exception/Definition/Template/InvalidTemplateArgumentException.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212
*/
1313
class InvalidTemplateArgumentException extends TemplateArgumentException
1414
{
15+
/**
16+
* Gets the type statement that causes the error.
17+
*/
18+
public TypeStatement $actual;
19+
1520
public function __construct(
16-
private readonly TypeStatement $expected,
21+
/**
22+
* Gets the type statement in which the error occurred.
23+
*/
24+
public readonly TypeStatement $expected,
1725
TemplateArgumentNode $argument,
1826
TypeStatement $type,
1927
string $template,
2028
int $code = 0,
2129
?\Throwable $previous = null,
2230
) {
31+
$this->actual = $argument->value;
32+
2333
parent::__construct(
2434
argument: $argument,
2535
type: $type,
@@ -29,26 +39,6 @@ public function __construct(
2939
);
3040
}
3141

32-
/**
33-
* Returns the type statement that causes the error.
34-
*
35-
* @api
36-
*/
37-
public function getActualType(): TypeStatement
38-
{
39-
return $this->argument->value;
40-
}
41-
42-
/**
43-
* Returns the type statement in which the error occurred.
44-
*
45-
* @api
46-
*/
47-
public function getExpectedType(): TypeStatement
48-
{
49-
return $this->expected;
50-
}
51-
5242
public static function becauseTemplateArgumentIsInvalid(
5343
TypeStatement $expected,
5444
TemplateArgumentNode $argument,

src/Exception/Definition/Template/MissingTemplateArgumentsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Occurs when a type requires more template arguments to be specified than required
1111
*/
12-
class MissingTemplateArgumentsException extends TemplateArgumentsCountException
12+
class MissingTemplateArgumentsException extends TemplateArgumentsRangeException
1313
{
1414
/**
1515
* @param int<0, max> $minSupportedArgumentsCount

src/Exception/Definition/Template/TemplateArgumentException.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ abstract class TemplateArgumentException extends TemplateArgumentsException
1616
/**
1717
* @var int<0, max>
1818
*/
19-
protected readonly int $index;
19+
public readonly int $index;
2020

2121
public function __construct(
22-
protected readonly TemplateArgumentNode $argument,
22+
public readonly TemplateArgumentNode $argument,
2323
TypeStatement $type,
2424
string $template,
2525
int $code = 0,
@@ -51,12 +51,4 @@ private static function fetchArgumentIndex(TemplateArgumentNode $argument, TypeS
5151

5252
return $index;
5353
}
54-
55-
/**
56-
* @api
57-
*/
58-
public function getArgument(): TemplateArgumentNode
59-
{
60-
return $this->argument;
61-
}
6254
}

src/Exception/Definition/Template/TemplateArgumentsCountException.php

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

src/Exception/Definition/Template/TemplateArgumentsNotSupportedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Occurs when a type does not support template arguments
1111
*/
12-
class TemplateArgumentsNotSupportedException extends TemplateArgumentsCountException
12+
class TemplateArgumentsNotSupportedException extends TemplateArgumentsRangeException
1313
{
1414
public static function becauseTemplateArgumentsNotSupported(
1515
NamedTypeNode $type,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Exception\Definition\Template;
6+
7+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
8+
9+
/**
10+
* Group of errors related to incorrect number of template arguments
11+
*/
12+
abstract class TemplateArgumentsRangeException extends TemplateArgumentsException
13+
{
14+
public function __construct(
15+
/**
16+
* @var int<0, max>
17+
*/
18+
public readonly int $passedArgumentsCount,
19+
/**
20+
* @var int<0, max>
21+
*/
22+
public readonly int $minSupportedArgumentsCount,
23+
/**
24+
* @var int<0, max>
25+
*/
26+
public readonly int $maxSupportedArgumentsCount,
27+
NamedTypeNode $type,
28+
string $template,
29+
int $code = 0,
30+
?\Throwable $previous = null
31+
) {
32+
parent::__construct(
33+
type: $type,
34+
template: $template,
35+
code: $code,
36+
previous: $previous,
37+
);
38+
}
39+
}

0 commit comments

Comments
 (0)