Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types for parameter Money\Money::__construct($currency) do not match: int|string (docblock) does not accept Money\Currency (native). #494

Closed
root-aza opened this issue Mar 14, 2024 · 1 comment · Fixed by #504

Comments

@root-aza
Copy link

root-aza commented Mar 14, 2024

Hi. Can't hydrate structure using valueobject moneyphp/money 😢

Repoduce code
<?php

declare(strict_types=1);

use CuyZ\Valinor\Mapper\Source\Source;
use Money\Currency;
use Money\Money;
use Symfony\Component\Uid\Uuid;

require_once __DIR__ . '/vendor/autoload.php';


final readonly class CreateApplication
{
    /**
     * @param int<1, 400>                      $term
     * @param non-empty-list<non-empty-string> $banks
     */
    public function __construct(
        public Uuid $clientId,
        public Money $amount,
        public int $term,
        public array $banks,
    ) {}
}



$json = <<<JSON
        {
        "banks": ["test"],
        "term": 20,
        "amount": 0
    }
    JSON;

try {
    $val = (new \CuyZ\Valinor\MapperBuilder())
        ->registerConstructor(
            static fn(string $value, string $currency): Money => new Money($value, new Currency($currency))
        )
        ->mapper()
        ->map(CreateApplication::class, Source::json($json))
    ;

    var_dump($val);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
    // Handle the error…
}
Stack Trace
PHP Fatal error:  Uncaught CuyZ\Valinor\Definition\Exception\TypesDoNotMatch: Types for parameter `Money\Money::__construct($currency)` do not match: `int|string` (docblock) does not accept `Money\Currency` (native). in /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php:51
Stack trace:
#0 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionParameterDefinitionBuilder.php(28): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver->resolveType(Object(ReflectionParameter))
#1 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionMethodDefinitionBuilder.php(42): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionParameterDefinitionBuilder->for(Object(ReflectionParameter), Object(CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver))
#2 [internal function]: CuyZ\Valinor\Definition\Repository\Reflection\ReflectionMethodDefinitionBuilder->CuyZ\Valinor\Definition\Repository\Reflection\{closure}(Object(ReflectionParameter))
#3 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionMethodDefinitionBuilder.php(41): array_map(Object(Closure), Array)
#4 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(123): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionMethodDefinitionBuilder->for(Object(ReflectionMethod), Object(CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver))
#5 [internal function]: CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->CuyZ\Valinor\Definition\Repository\Reflection\{closure}(Object(ReflectionMethod))
#6 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(120): array_map(Object(Closure), Array)
#7 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(72): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->methods(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#8 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Cache/CacheClassDefinitionRepository.php(34): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->for(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#9 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/InterfaceNodeBuilder.php(47): CuyZ\Valinor\Definition\Repository\Cache\CacheClassDefinitionRepository->for(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#10 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#11 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/IterableNodeBuilder.php(26): CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#12 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/StrictNodeBuilder.php(36): CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#13 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php(33): CuyZ\Valinor\Mapper\Tree\Builder\StrictNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#14 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/RootNodeBuilder.php(16): CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#15 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ObjectNodeBuilder.php(56): CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell))
#16 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ObjectNodeBuilder.php(23): CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder->children(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Object\ArgumentsValues), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#17 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/NativeClassNodeBuilder.php(39): CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Object\FilteredObjectBuilder), Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#18 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\NativeClassNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#19 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/UnionNodeBuilder.php(37): CuyZ\Valinor\Mapper\Tree\Builder\CasterNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#20 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/InterfaceNodeBuilder.php(51): CuyZ\Valinor\Mapper\Tree\Builder\UnionNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#21 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#22 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/IterableNodeBuilder.php(26): CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#23 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/StrictNodeBuilder.php(36): CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#24 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php(33): CuyZ\Valinor\Mapper\Tree\Builder\StrictNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#25 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/RootNodeBuilder.php(16): CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#26 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php(44): CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell))
#27 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php(25): CuyZ\Valinor\Mapper\TypeTreeMapper->node('CreateApplicati...', Object(CuyZ\Valinor\Mapper\Source\Source))
#28 /Users/aza/PhpstormProjects/cash-backend/app.php(44): CuyZ\Valinor\Mapper\TypeTreeMapper->map('CreateApplicati...', Object(CuyZ\Valinor\Mapper\Source\Source))
#29 {main}
  thrown in /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php on line 51

Fatal error: Uncaught CuyZ\Valinor\Definition\Exception\TypesDoNotMatch: Types for parameter `Money\Money::__construct($currency)` do not match: `int|string` (docblock) does not accept `Money\Currency` (native). in /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php:51
Stack trace:
#0 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionParameterDefinitionBuilder.php(28): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver->resolveType(Object(ReflectionParameter))
#1 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionMethodDefinitionBuilder.php(42): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionParameterDefinitionBuilder->for(Object(ReflectionParameter), Object(CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver))
#2 [internal function]: CuyZ\Valinor\Definition\Repository\Reflection\ReflectionMethodDefinitionBuilder->CuyZ\Valinor\Definition\Repository\Reflection\{closure}(Object(ReflectionParameter))
#3 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionMethodDefinitionBuilder.php(41): array_map(Object(Closure), Array)
#4 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(123): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionMethodDefinitionBuilder->for(Object(ReflectionMethod), Object(CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver))
#5 [internal function]: CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->CuyZ\Valinor\Definition\Repository\Reflection\{closure}(Object(ReflectionMethod))
#6 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(120): array_map(Object(Closure), Array)
#7 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php(72): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->methods(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#8 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Cache/CacheClassDefinitionRepository.php(34): CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->for(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#9 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/InterfaceNodeBuilder.php(47): CuyZ\Valinor\Definition\Repository\Cache\CacheClassDefinitionRepository->for(Object(CuyZ\Valinor\Type\Types\NativeClassType))
#10 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#11 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/IterableNodeBuilder.php(26): CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#12 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/StrictNodeBuilder.php(36): CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#13 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php(33): CuyZ\Valinor\Mapper\Tree\Builder\StrictNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#14 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/RootNodeBuilder.php(16): CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#15 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ObjectNodeBuilder.php(56): CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell))
#16 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ObjectNodeBuilder.php(23): CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder->children(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Object\ArgumentsValues), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#17 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/NativeClassNodeBuilder.php(39): CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Object\FilteredObjectBuilder), Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#18 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\NativeClassNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#19 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/UnionNodeBuilder.php(37): CuyZ\Valinor\Mapper\Tree\Builder\CasterNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#20 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/InterfaceNodeBuilder.php(51): CuyZ\Valinor\Mapper\Tree\Builder\UnionNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#21 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php(24): CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#22 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/IterableNodeBuilder.php(26): CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#23 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/StrictNodeBuilder.php(36): CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#24 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php(33): CuyZ\Valinor\Mapper\Tree\Builder\StrictNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#25 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/Tree/Builder/RootNodeBuilder.php(16): CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell), Object(CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder))
#26 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php(44): CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder->build(Object(CuyZ\Valinor\Mapper\Tree\Shell))
#27 /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php(25): CuyZ\Valinor\Mapper\TypeTreeMapper->node('CreateApplicati...', Object(CuyZ\Valinor\Mapper\Source\Source))
#28 /Users/aza/PhpstormProjects/cash-backend/app.php(44): CuyZ\Valinor\Mapper\TypeTreeMapper->map('CreateApplicati...', Object(CuyZ\Valinor\Mapper\Source\Source))
#29 {main}
  thrown in /Users/aza/PhpstormProjects/cash-backend/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php on line 51
@romm
Copy link
Member

romm commented Mar 27, 2024

Hey there, thanks for the reproducible code, helped a lot!

This should be fixed by #504. 😉

@romm romm closed this as completed in #504 Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants