phplaravelphpstormpsalm-php

How to annotate Laravel Collection elements during iteration


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
        }
    }

Solution

  • 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