You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Psalm static analysis fails on MapperBuilder::registerTransformer, because transformer should be of type class-string|pure-callable, but sometimes transformers isn't pure.
Examples
Example 1: format dates
(new \CuyZ\Valinor\MapperBuilder())
->registerTransformer(
fn (\DateTimeInterface$date) => $date->format('Y/m/d')
);
Psalm output
ERROR: InvalidArgument - xxx/transformer.php:20:9 - Argument 1 of CuyZ\Valinor\MapperBuilder::registerTransformer
expects class-string|pure-callable, but impure-Closure(DateTimeInterface):string provided
(see https://psalm.dev/004)
fn (\DateTimeInterface $date) => $date->format('Y/m/d')
Example 2: chained
(new \CuyZ\Valinor\MapperBuilder())
->registerTransformer(
fn (string$value, callable$next) => strtoupper($next())
);
ERROR: InvalidArgument - xxx/transformer.php:20:9 - Argument 1 of CuyZ\Valinor\MapperBuilder::registerTransformer
expects class-string|pure-callable, but impure-Closure(string, callable):string provided
(see https://psalm.dev/004)
fn (string $value, callable $next) => strtoupper($next())
If you mark $next parameter as pure-callable, then the psalm analysis will be successful, but there will be errors in runtime
PHP Fatal error: Uncaught CuyZ\Valinor\Normalizer\Exception\TransformerHasInvalidCallableParameter: Transformer's
second parameter must be a callable, `pure-callable` given for `Closure (lines 11 to 12 of /app/xxx/transformer.php)`. in
/app/vendor/cuyz/valinor/src/Normalizer/Transformer/ValueTransformersHandler.php:122
Example 3: invokable class
This is not pure transformer, cause it depends on $separator value from constructor, but i think it's still should be valid transformer
A library should probably not be type hinting a pure-callable on a closure anyway. What does the library care if my transformer has state or side effects?
I could have a transformer that logs, or increments a number each time it sees a specific value; it is not relevant.
That said, I believe the error is not just about purity.
Psalm static analysis fails on
MapperBuilder::registerTransformer
, because transformer should be of typeclass-string|pure-callable
, but sometimes transformers isn't pure.Examples
Example 1: format dates
Psalm output
Example 2: chained
If you mark $next parameter as pure-callable, then the psalm analysis will be successful, but there will be errors in runtime
Php output
Example 3: invokable class
This is not pure transformer, cause it depends on
$separator
value from constructor, but i think it's still should be valid transformerPsalm output
The text was updated successfully, but these errors were encountered: