safe icon indicating copy to clipboard operation
safe copied to clipboard

Use psalm function stubs to add infos such as type templates and pureness

Open Kharhamel opened this issue 5 years ago • 5 comments

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?

Kharhamel avatar Jan 10 '20 13:01 Kharhamel

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)

Kharhamel avatar Jan 10 '20 13:01 Kharhamel

PHPStan reads Psalm-specific generic annotations like @psalm-template. PHPStan does not support @psalm-assert and @psalm-pure etc.

ondrejmirtes avatar Jan 10 '20 13:01 ondrejmirtes

Ok so I guess at first I can just focus on psalm annotations.

Kharhamel avatar Jan 10 '20 13:01 Kharhamel

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>).

muglug avatar Jan 11 '20 05:01 muglug

@Kharhamel Any plans to implements this?

snapshotpl avatar Jun 22 '20 23:06 snapshotpl