1

I was thinking about how to annotate types in PhpStorm. I believe PhpStorm is using Psalm to resolve types, but I can't find how to annotate type to get suggestions here:

enter image description here

$row in my app will always be Collection object and I want to have it marked somewhere here with annotations.

Does anyone have an idea how to accomplish that?

    /**
     * @param Collection $rows
     */
    public function collection(Collection $rows)
    {
        foreach ($rows as $row)
        {
            dump($row->); // $row is also Collection object
        }
    }
LazyOne
  • 158,824
  • 45
  • 388
  • 391
Eddy
  • 593
  • 8
  • 22
  • Check [laravel ide helper](https://github.com/barryvdh/laravel-ide-helper). – Tpojka Aug 12 '21 at 12:42
  • 1
    See https://blog.jetbrains.com/phpstorm/2021/07/phpstorm-2021-2-release/#collections_with_template -- you would need to use the generic + `@template` stuff (plus, the Psalm plugin must be enabled (even if it does not run it, it just used to support the syntax)) – LazyOne Aug 12 '21 at 13:08

2 Answers2

1

you can mark var type like this:

/**
 @var $row Collection
**/
dump($row->);

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000701884-Using-var-type-hinting-with-properties-instantiated-by-Traits

Eden Moshe
  • 1,097
  • 1
  • 6
  • 16
0

You can use generics:

    /**
     * @param Collection<Collection> $rows
     */
    public function collection(Collection $rows)
    {
        foreach ($rows as $row)
        {
            dump($row->); // $row is also Collection object
        }
    }

which are supported in most modern IDE such as PHPStorm.

Marcel
  • 186
  • 1
  • 5