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