php-standard-library / psalm-plugin

Psalm integration for the PHP Standard Library
MIT License
24 stars 6 forks source link

Better pipe plugin #8

Closed veewee closed 2 years ago

veewee commented 2 years ago

Psalm v5 is out!

This PR contains:

More info about the dynamic storage:

-> Testcases in PSL: https://github.com/azjezz/psl/pull/372

Why is this better?

The newly introduced DynamicFunctionStorageProviderInterface makes it possible to dynamically create templates based on the provided function. This allows us to count the amount of arguments (e.g. 3) and create 4 dynamic templates that are linking up the pipe parameters like this:

The resulting closure that is coming out pipe() will then be:

In previous implementation, we frequently had to tell psalm the types were mixed, because we did not know better. By doing it this way, we rely on psalm being able to infer the templates for us!

(Thanks to the amazing work of @klimick)

Possible other use-case

Based on this principle, we can add e.g. new partial_left and partial_right functions that are checked by psalm instead of runtime. I would only add this if we were able to use the function as string (e.g. through dynamic define's as you proposed a while back. For example:

partial_right(Vec\map, Str\trim)($items) // -> closure(list<string>): list<string>

// or

partial_right(Vec\map::class, Str\trim::class)($items)

More info:

The cool thing about validating this in psalm, is that we do not require additional runtime validations. (or at least theoretically)