0

I'm trying to use PhpDoc to hint all the deprecated magic properties set on a Laravel Model using the mixin described in this answer.

I am using JetBrains PhpStorm 2022.1.1, PHP 8.1.6 and Psalm 4.23.0

I have a trait, defined in app/Models/Traits/PhpDoc/ProductPhpDocTrait.php:

<?php

namespace App\Models\Traits\PhpDoc;

/**
 * @property int id
 * @property int type
*/
trait ProductPhpDocTrait
{

    /**
     * @deprecated
     * @var string $display_status ;
     */
    public string $display_status;

}

And I'm using this trait on my model in app/Models/Product.php:

<?php

namespace App\Models;

/**
 * @mixin App\Models\Traits\PhpDoc\ProductPhpDocTrait
 */
class Product extends Model implements HasLinkableTexts
{
    //...
}

Autocomplete is now working for the properties documented in the trait, and those which are deprecated are now marked with strikethrough.

But Psalm is throwing the following error:

ERROR: UndefinedDocblockClass - app/Models/Product.php:21:7 - Docblock-defined class, interface or enum named App\Models\Traits\PhpDoc\ProductPhpDocTrait does not exist (see https://psalm.dev/200)
class Product extends Model implements HasLinkableTexts

(the line & character reference the opening class definition)

It seems that the namespace is fine up until two levels above where the file resides:

Image showing PhpStorm issues

If I move the trait up a level (in the filesystem and the namespace) to app/Models/Traits/ProductPhpDocTrait.php, PhpStorm still flags the error as two levels up the namespace:

enter image description here

If I move the trait into the Models folder (and update the namespace), I still get the same Psalm error that the trait does not exist.

How can I resolve this, such that I can hint deprecated magic properties with the code passing static analysis?

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • 1
    Since the namespace is already `App\Models`, it makes sense to try using either `Traits\PhpDoc\ProductPhpDocTrait` or `\App\Models\Traits\PhpDoc\ProductPhpDocTrait`. – Eugene Morozov Jun 22 '22 at 11:52
  • Thanks @EugeneMorozov - neither of those worked though. I've tried letting PhpStorm add the use statement using the 'import' context action too, but no joy. – Adam Hopkinson Jun 22 '22 at 16:33
  • `class, interface or enum` - looks like Psalm does not expect a trait there. Have you tried converting the trait to a class instead? – weirdan Jun 22 '22 at 17:32

0 Answers0