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

Fix psalm error 'MapperBuilder::filterExceptions expects pure-callable but impure-Closure provided'

pchelk1n opened this issue · comments

Sample code from the documentation https://valinor.cuyz.io/1.6/usage/validation-and-error-handling/#3-allow-third-party-exceptions

->filterExceptions(function (Throwable $exception) {
            if ($exception instanceof \Webmozart\Assert\InvalidArgumentException) {
                return \CuyZ\Valinor\Mapper\Tree\Message\MessageBuilder::from($exception);
            } 

            // If the exception should not be caught by this library, it
            // must be thrown again.
            throw $exception;
        })

Code from the project based on the example from the documentation:

$requestObject = (new MapperBuilder())
                ->filterExceptions(static function (Throwable $exception): ErrorMessage {
                    if ($exception instanceof InvalidArgumentException) {
                        return MessageBuilder::from($exception);
                    }

                    throw $exception;
                })
                ->mapper()
                ->map(
                    signature: $className,
                    source: new JsonSource($request->getContent()),
                );

Psalm error (errorLevel="1"):

ERROR: InvalidArgument - ApiRequestValueResolver.php:46:36 - Argument 1 of 
CuyZ\Valinor\MapperBuilder::filterExceptions 
expects pure-callable(Throwable):CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage, 
but impure-Closure(Throwable):CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage provided (see https://psalm.dev/004)

                ->filterExceptions(static function (Throwable $exception): ErrorMessage {
                    if ($exception instanceof InvalidArgumentException) {
                        return MessageBuilder::from($exception);
                    }

                    throw $exception;
                })

Thanks for the report! Should be fixed by #436. ☺️