简体   繁体   English

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

[英]How to configure php_codesniffer to force the PHP 7.4 property type declaration?

I'd like to configure my phpcs ruleset so it would force the PHP 7.4 added type declaration for class properties.我想配置我的 phpcs 规则集,以便强制 PHP 7.4 为 class 属性添加类型声明。 Currently I'm using the PSR-12 standard ruleset and I want to add there a sniff, which will scan this code:目前我正在使用 PSR-12 标准规则集,我想在那里添加一个嗅探,它将扫描此代码:

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

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

and show something like "Missing type declaration", so the correct code should be:并显示类似“缺少类型声明”的内容,因此正确的代码应该是:

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

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

Note that I'd like the sniff to warn me even in case of typehint being placed in the PHPDoc.请注意,即使在 PHPDoc 中放置了 typehint,我也希望嗅探器能够警告我。 It should demand the type declared before the property name.它应该要求在属性名称之前声明的类型。 Is there a rule already existing for this?是否已经存在这方面的规则? Thanks!谢谢!

Try gskema/phpcs-type-sniff :尝试gskema/phpcs-type-sniff

Custom phpcs CodeSniffer rule that:自定义 phpcs CodeSniffer 规则:

  • Enforces usage of PHP 7 type declarations (where possible)强制使用 PHP 7 类型声明(如果可能)
  • Enforces documenting array types with more specific types (eg int[])强制使用更具体的类型(例如 int[])记录数组类型
  • Checks for useless PHPDoc blocks (no repeated information)检查无用的 PHPDoc 块(无重复信息)
  • Many more other checks更多其他检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM