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

624 views Asked by At

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!

1

There are 1 answers

0
jokkedk On

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