vscode-intelephense
vscode-intelephense copied to clipboard
Support psalm- and phpstan- prefixed param, return, var annotations
Feature description or problem with existing feature
Many popular php libraries like Doctrine
provide template types written as:
/**
* @param string $className The class name of the object to find.
* @param mixed $id The identity of the object to find.
* @psalm-param class-string<T> $className
*
* @return object|null The found object.
* @psalm-return T|null
*
* @template T of object
*/
public function find(string $className, $id);
Note that there are two param types defined for $className
and two return types, the psalm-param
is the generic one.
Params are duplicated so that tools which don't understand generics are not confused by reading normal param/return types.
Unfortunately it also means that intelephense cannot understand these generic types since it only looks at @param
and @return
, and not psalm-param
and psalm-return
.
Describe the solution you'd like
Use these advanced types to override param
and return
types, in some cases intelephense may not understand these types like int<1, max>
(which denotes integer range) so this should be a config option.
Additional context
Advanced types like generics are often prefixed with psalm-
or phpstan-
(two popular static analysis tools in php ecosystem) and intelephense is not using this information currently.
https://psalm.dev/docs/annotating_code/typing_in_psalm/ https://docs.phpdoc.org/latest/guide/guides/types.html
@bmewburn Hello.. i'm using lib https://github.com/prewk/result that using psalm annotation.. Could you make support for psalm prefix please.. i'm have been buy intelephense license, btw.. Thanks
$result = new Ok([]);
$data = $result->unwrap();
Is support for this planned?
@bmewburn same as #2081
My company is more than willing to sponsor the development of this feature.
Something we use a lot is phpstan array shapes and local types aliases imported into other files. E.g.:
<?php
/**
* @phpstan-type MyType array{
* key1: string,
* key2: array<string>,
* }
*/
class MyClass1 { /* ... */ }
<?php
/**
* @phpstan-import-type MyType from MyClass1
*/
class MyClass2 {
// here we can do @phpstan-param MyType
// or @phpstan-return MyType
// or @phpstan-var MyType
//
// but because intelephense doesn't yet support this,
// if we do @param MyType intelephense will expect a class and will show an error.
// It also doesn't show hover intelligence, etc.
}
It would be great if this were supported. I understand PHPStorm is working on this. This allows us to avoid DTO classes just for type safety and use associative arrays instead because of phpstan type checking.
This might be better as a separate issue.
1.11 will have a setting intelephense.compatibility.preferPsalmPhpstanPrefixedAnnotations
, that will make the extension use psalm and/or phpstan type annotations for @var
, @param
, @var
, @psalm-type
. It defaults to false
for now and will need to be set to true
to enable.