【问题标题】:How to configure php_codesniffer to force the PHP 7.4 property type declaration?如何配置 php_codesniffer 强制 PHP 7.4 属性类型声明?
【发布时间】:2020-11-30 10:39:32
【问题描述】:

我想配置我的 phpcs 规则集,以便强制 PHP 7.4 为类属性添加类型声明。目前我正在使用 PSR-12 标准规则集,我想在那里添加一个嗅探器,它将扫描此代码:

/** @var string $timeFormat*/
protected $timeFormat = 'H:i';

protected $dateFormat = 'Y-m-d';

并显示“缺少类型声明”之类的内容,因此正确的代码应该是:

/** @var string $timeFormat*/
protected string $timeFormat = 'H:i';

protected string $dateFormat = 'Y-m-d';

请注意,即使在 PHPDoc 中放置了 typehint,我也希望嗅探器能够警告我。它应该要求在属性名称之前声明的类型。 是否已经存在这方面的规则?谢谢!

【问题讨论】:

    标签: php types php-7.4 phpcs phpcodesniffer


    【解决方案1】:

    试试gskema/phpcs-type-sniff:

    自定义phpcs CodeSniffer规则:

    • 强制使用 PHP 7 类型声明(如果可能)
    • 强制使用更具体的类型(例如 int[])记录数组类型
    • 检查无用的 PHPDoc 块(无重复信息)
    • 更多其他检查

    【讨论】:

      猜你喜欢
      • 2018-02-19
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多