safe
safe copied to clipboard
Use psalm function stubs to add infos such as type templates and pureness
In order to more easily solve #175 and #141, it may be easier to simply extract the needed infos from psalm's functions stubs instead of trying to recalculate them. This way we don't have to duplicate psalm's logic.
This is a bit redundant with our utilization of phpstan's functionMap to generate types but we could just use psalm as our type reference as well. @moufmouf, @muglug what you think of this? Does this look raisonable?
Maybe we could do the same thing for the annotation @template from phpstan. Or is it redundant with @psalm-template? More generally, if I want to remain compatible with both tools, will I have to duplicate every annotations or is there some kind of overlapp between the two? (@ondrejmirtes)
PHPStan reads Psalm-specific generic annotations like @psalm-template
. PHPStan does not support @psalm-assert
and @psalm-pure
etc.
Ok so I guess at first I can just focus on psalm annotations.
Psalm has three sources of types:
- CallMap.php (virtually identical to PHPStan's functionMap as they're based on the same file in Phan's source code)
- CoreGenericFunctions.php/CoreGenericClasses.php and other files in
src/Psalm/Internal/Stubs
- Custom type provider plugins (that you won't be able to easily repurpose)
I designed the annotations to provide as much information as possible about the behaviour of the function within the function docblock, so people wouldn't have to write plugins (where the barrier to entry is much higher).
The stubs have the template annotations that can be repurposed, including @param-out
which defines the type of a byref variable after the function has executed if different from input (e.g. the sort
function which turns array<string, int>
into a list<int>
).
@Kharhamel Any plans to implements this?