PhpStorm 如何消除 API Resources 中 $this->id 时的 Property accessed via magic method 警告

使用 PhpStorm 编辑 API Resource 类代码时,遇到一个小问题,使用 $this->id 的时候,会报警 Property accessed via magic method,虽然不影响代码运行,我还是希望消除,大家有遇到吗?除了不管或者直接关闭该项检查,有啥更加理想的办法吗?

PhpStorm 如何消除 API Resources 中 $this->id 时的 Property accessed via magic method  警告

class UploadResource extends JsonResource
{
 public function toArray(Request $request): array
  {
        // return parent::toArray($request);
        return [
            'id' => $this->id,
            'path' => $this->path,
            'file_name' => $this->file_name,
            'alt' => $this->alt,
            'mime_type' => $this->mime_type,
            'size' => $this->size,
            'disk' => $this->disk,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

我使用了 Laravel Idea 插件,但是也不能消除该警告。

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

我又调查了下,正解是 mixin DocBlock,如下:

/** @mixin \App\Models\User */
class UserResource extends JsonResource

我通过 GitHub 搜索发现这个的,然后 Google 又找到了这篇靠谱的博文: freek.dev/1482-the-mixin-php-docbl...

4个月前 评论
荭尘宝宝 4个月前
twitf 4个月前
荭尘宝宝 4个月前
wind_big 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前
讨论数量: 14

我又调查了下,正解是 mixin DocBlock,如下:

/** @mixin \App\Models\User */
class UserResource extends JsonResource

我通过 GitHub 搜索发现这个的,然后 Google 又找到了这篇靠谱的博文: freek.dev/1482-the-mixin-php-docbl...

4个月前 评论
荭尘宝宝 4个月前
twitf 4个月前
荭尘宝宝 4个月前
wind_big 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前

file
试试这样给 this 定义具体的对象名称

4个月前 评论
xuchunyang (楼主) 4个月前
xuchunyang (楼主) 4个月前

more action -> 选择第三个选项

4个月前 评论
/**
* @property string $id
* @property string $path
*/
class UploadResource extends JsonResource
{
 public function toArray(Request $request): array
  {
        // return parent::toArray($request);
        return [
            'id' => $this->id,
            'path' => $this->path,
            'file_name' => $this->file_name,
            'alt' => $this->alt,
            'mime_type' => $this->mime_type,
            'size' => $this->size,
            'disk' => $this->disk,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

试试这样

4个月前 评论

我又调查了下,正解是 mixin DocBlock,如下:

/** @mixin \App\Models\User */
class UserResource extends JsonResource

我通过 GitHub 搜索发现这个的,然后 Google 又找到了这篇靠谱的博文: freek.dev/1482-the-mixin-php-docbl...

4个月前 评论
荭尘宝宝 4个月前
twitf 4个月前
荭尘宝宝 4个月前
wind_big 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前
荭尘宝宝 4个月前
小民爱Laravel

file

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!