vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Support psalm- and phpstan- prefixed param, return, var annotations

Open aszenz opened this issue 2 years ago • 4 comments

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

aszenz avatar Jan 13 '23 19:01 aszenz

@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();

erlangparasu avatar Apr 27 '23 22:04 erlangparasu

Is support for this planned?

Xesau avatar Jul 12 '23 10:07 Xesau

@bmewburn same as #2081

My company is more than willing to sponsor the development of this feature.

seebeen avatar Jan 22 '24 23:01 seebeen

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.

mikedfunk avatar Feb 08 '24 18:02 mikedfunk

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.

bmewburn avatar Jun 28 '24 03:06 bmewburn