php - 如何配置 php_codesniffer 以强制 PHP 7.4 属性类型声明?

标签 php types php-7.4 phpcs phpcodesniffer

我想配置我的 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,我也希望嗅探器能够警告我。它应该要求在属性名称之前声明类型。 对此是否已有规则?谢谢!

最佳答案

尝试gskema/phpcs-type-sniff :

Custom phpcs CodeSniffer rule that:

  • Enforces usage of PHP 7 type declarations (where possible)
  • Enforces documenting array types with more specific types (e.g. int[])
  • Checks for useless PHPDoc blocks (no repeated information)
  • Many more other checks

关于php - 如何配置 php_codesniffer 以强制 PHP 7.4 属性类型声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65071984/

相关文章:

go - 自定义类型作为参数传递给函数

types - Elasticsearch,类型属性与类型映射有什么区别?

javascript - 数据表搜索,Orderby 不起作用

php - 注意 : Use of undefined constant self - assumed 'self' , 当把 property_exists 作为第一个参数时

php - 查询生成器 laravel

javascript - 如何在 TypeScript 中使用动态对象属性

php - 将变量声明为字符串,然后将其用作数组?

PHP7.4 Redis上的Session存储问题

php - 如何在 DSN 中指定用户名和密码?

php - 如何根据通过查询字符串接收到的值执行 jQuery 代码?