phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Property $id accessed via magic method.

Open rabol opened this issue 8 months ago • 4 comments

Hi

I'm not really sure why I get this error for all all the $model->id:

Property $id accessed via magic method.

Laravel Code

protected static function boot(): void
    {
        parent::boot();

        static::creating(function (self $model): void {
            $model->setDefaultSettings();
        });
        static::created(function (self $model): void {
            $model->makeUserDirectories();

        });

        static::deleted(function (self $model): void {
            $model->deleteUserDirectories();

            UserNotification::whereUserId($model->id)->delete();
            UserWebhook::whereUserId($model->id)->delete();
            UserSignature::whereUserId($model->id);
            UserDocTemplate::whereUserId($model->id)->whereNull('team_id')->delete();
            UserDoc::whereUserId($model->id)->whereNull('team_id')->delete();
            UserEmailTemplate::whereUserId($model->id)->whereNull('team_id')->delete();
        });
    }

rabol avatar Apr 02 '25 07:04 rabol

This low-severity message warns about a dynamic property that is not defined while other dynamic properties are defined with @property tag. (https://docs.devsense.com/vs/code%20validation/diagnostics/#hint:~:text=or%20simpler%20form.-,PHP6602,-MagicMethodField)

Recommended actions are:

Once the other model attributes are defined with @property tag, the editor expects that user knows what he does :)

jakubmisek avatar Apr 02 '25 09:04 jakubmisek

Not absolutely correct.

The issue here is that it is not all properties but only some that trigger this warning / error.

I saw that i for some reason had this:

/**
 * @property string $current_team_id
 * @property datetime $email_verified_at
 */

on the model, when that is removed, then the 'warning' goes away

rabol avatar Apr 02 '25 09:04 rabol

Yes, that is what the error says: "Some properties are defined using @property", so if you use ->id which is not defined using @property, but other properties are, it is possibly an error.

jakubmisek avatar Apr 02 '25 10:04 jakubmisek

In short - either define all dynamic properties using @property or none of them.

If you defined "just some", the editor assumes you forgot.

jakubmisek avatar Apr 02 '25 10:04 jakubmisek