简体   繁体   中英

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. Currently I'm using the PSR-12 standard ruleset and I want to add there a sniff, which will scan this code:

/** @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. It should demand the type declared before the property name. Is there a rule already existing for this? Thanks!

Try 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 (eg int[])
  • Checks for useless PHPDoc blocks (no repeated information)
  • Many more other checks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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