【问题标题】:How to ignore PHP_CodeSniffer warnings如何忽略 PHP_CodeSniffer 警告
【发布时间】:2020-01-30 01:29:53
【问题描述】:

我想知道是否有某种方法可以忽略 PHP_CodeSniffer 生成的涉及 Eloquent 映射的警告。

例如:

/**
 * @param User $user
 * @param string $message
 * @param string $type
 * @return Comment
 * @throws Exception
 */
public function createComment(User $user, $message, $type)
{
    $comment = new Comment();
    $comment->creator()->associate($user);
    $comment->Message = $message;          //PHPCS warning: Property accessed via magic method         
    $comment->AddedDate = new Carbon();    
    $comment->Type = $type;
    $comment->save();
    return $comment;
}

P.S:我不想排除与模型无关的警告(保留它们以供其他类提示),最好排除每个属性的 setter 和 getter

【问题讨论】:

  • 搜索 laravel 的 ide-helper。这是一个为此字段生成 phpdoc 并修复此警告的包。
  • 或者使用@property 记录模型 因为它正在检索数据库值,所以除了 Eloquent 中的属性设置器和获取器之外,您不能严格键入它。但是您可以通过这种phpDoc方式满足检查。

标签: php laravel static-analysis phpcs


【解决方案1】:

如果“Comment”是您创建的模型,请添加类 phpDoc cmets 以提示 IDE 有关可用属性的信息。

/**
 * Class Comment
 * @property int id
 * @property string Message
 */
class Comment extends Model {

这也适用于自动完成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多