CuyZ / Valinor

PHP library that helps to map any input into a strongly-typed value object structure.

Home Page:https://valinor.cuyz.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with variadic parameter contains generics

jenky opened this issue · comments

commented

After upgrade from 1.7 to 1.12, my code broke even though I didn't make any changes. I was using ramsey/collection for my DTO. UnresolvableType was thrown, but there was no specific error message provided. After further debugging, I believe this is the cause of the error.

image

This is the docblock of merge method which I'm not use anyway.

/**
 * @param CollectionInterface<T> ...$collections The collections to merge.
 *
 * @return CollectionInterface<T>
 *
 * @throws CollectionMismatchException if unable to merge any of the given
 *     collections or items within the given collections due to type
 *     mismatch errors.
 */
public function merge(CollectionInterface ...$collections): CollectionInterface

Hi @jenky thanks for the report.

Could you post a more detailed example of how you use ramsey/collection in your code?

commented

This is a striped down version of it

final class MyDTO
{
    public function __construct(
        public readonly UuidInterface $uuid,
        public readonly SettingsCollection $settings,
    ) {
    }
}
final class SettingsCollection extends AbstractSet
{
    public function getType(): string
    {
        return Settings::class;
    }
}
final class Settings
{
    /**
     * @param  array<string, mixed> $settings
     */
    public function __construct(
        public readonly SettingEnum $name,
        public readonly array $settings = []
    ) {
    }
}
return (new MapperBuilder())
    ->allowPermissiveTypes()
    ->allowSuperfluousKeys()
    ->registerConstructor(
        Uuid::fromString(...),
    )
    ->mapper()
    ->map(MyDTO::class, $data);

I also try to transform the data['settings'] to Settings[] before passing to map method but it seems doesn't work also

Thanks, that helped identifying the issue!

#534 should fix it. 😉

@romm I am having the same issue. What am I doing wrong?
Also, could you provide an example using collections? Can I use the package with illuminate /collections?