-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly handle nested unresolvable type during mapping
This change brings two enhancements: - When mapping to a structure that contains a nested unresolvable type, for instance a doc-block type that does not match the native type, the mapper will detect it and throw a proper exception (it used to just crash on a badly designed assertion). - Unresolvable types that are not used during mapping, for instance parameters of methods that are not used by the mapper will no longer throw an exception even if not needed by the library. Instead, an unresolvable type is assigned to these invalid properties/parameters/ return types.
- Loading branch information
Showing
12 changed files
with
168 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Exception; | ||
|
||
use CuyZ\Valinor\Definition\FunctionDefinition; | ||
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType; | ||
use LogicException; | ||
|
||
/** @internal */ | ||
final class TypeErrorDuringArgumentsMapping extends LogicException | ||
{ | ||
public function __construct(FunctionDefinition $function, UnresolvableShellType $exception) | ||
{ | ||
parent::__construct( | ||
"Could not map arguments of `$function->signature`: {$exception->getMessage()}", | ||
1711534351, | ||
$exception, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Exception; | ||
|
||
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType; | ||
use CuyZ\Valinor\Type\Type; | ||
use LogicException; | ||
|
||
/** @internal */ | ||
final class TypeErrorDuringMapping extends LogicException | ||
{ | ||
public function __construct(Type $type, UnresolvableShellType $exception) | ||
{ | ||
parent::__construct( | ||
"Error while trying to map to `{$type->toString()}`: {$exception->getMessage()}", | ||
1711526329, | ||
$exception, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Tree\Exception; | ||
|
||
use CuyZ\Valinor\Type\Types\UnresolvableType; | ||
use LogicException; | ||
|
||
/** @internal */ | ||
final class UnresolvableShellType extends LogicException | ||
{ | ||
public function __construct(UnresolvableType $type) | ||
{ | ||
parent::__construct($type->message()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping; | ||
|
||
use CuyZ\Valinor\Mapper\Exception\TypeErrorDuringArgumentsMapping; | ||
use CuyZ\Valinor\Mapper\Exception\TypeErrorDuringMapping; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; | ||
|
||
final class TypeErrorDuringMappingTest extends IntegrationTestCase | ||
{ | ||
public function test_property_with_non_matching_types_throws_exception(): void | ||
{ | ||
$class = (new class () { | ||
/** | ||
* @phpstan-ignore-next-line | ||
* @var string | ||
*/ | ||
public bool $propertyWithNotMatchingTypes; | ||
})::class; | ||
|
||
$this->expectException(TypeErrorDuringMapping::class); | ||
$this->expectExceptionCode(1711526329); | ||
$this->expectExceptionMessage("Error while trying to map to `$class`: Types for property `$class::\$propertyWithNotMatchingTypes` do not match: `string` (docblock) does not accept `bool` (native)."); | ||
|
||
$this->mapperBuilder()->mapper()->map($class, ['propertyWithNotMatchingTypes' => true]); | ||
} | ||
|
||
public function test_parameter_with_non_matching_types_throws_exception(): void | ||
{ | ||
$class = (new class (true) { | ||
/** | ||
* @param string $parameterWithNotMatchingTypes | ||
* @phpstan-ignore-next-line | ||
*/ | ||
public function __construct(public bool $parameterWithNotMatchingTypes) {} | ||
})::class; | ||
|
||
$this->expectException(TypeErrorDuringMapping::class); | ||
$this->expectExceptionCode(1711526329); | ||
$this->expectExceptionMessage("Error while trying to map to `$class`: Types for parameter `$class::__construct(\$parameterWithNotMatchingTypes)` do not match: `string` (docblock) does not accept `bool` (native)."); | ||
|
||
$this->mapperBuilder()->mapper()->map($class, ['parameterWithNotMatchingTypes' => true]); | ||
} | ||
|
||
public function test_function_parameter_with_non_matching_types_throws_exception(): void | ||
{ | ||
$function = | ||
/** | ||
* @param string $parameterWithNotMatchingTypes | ||
*/ | ||
fn (bool $parameterWithNotMatchingTypes): string => 'foo'; | ||
|
||
$this->expectException(TypeErrorDuringArgumentsMapping::class); | ||
$this->expectExceptionCode(1711534351); | ||
$this->expectExceptionMessageMatches("/Could not map arguments of `.*`: Types for parameter `.*` do not match: `string` \(docblock\) does not accept `bool` \(native\)\./"); | ||
|
||
$this->mapperBuilder()->argumentsMapper()->mapArguments($function, ['parameterWithNotMatchingTypes' => true]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters