Php_codesniffer: Missing return type sniff / inspection in PHP7

Created on 25 Nov 2016  路  3Comments  路  Source: squizlabs/PHP_CodeSniffer

It is possible to create following rules in php code sniffer?

  1. Require return type for method in PHP 7 return type OR in phpdoc code block.
  2. Require type hinting in arguments list OR in phpdoc
  3. Require @var declaration for property.

For example:

Passes:
public function someMethod(SomeIntrerface $foo) : SomeIntrerface

Passes:

/**
     * @param SomeIntrerface $foo
     * @return SomeIntrerface
     */
    public function someMethod($foo)

Do not pass:
public function someMethod($foo)


Pass:

    /**
     * @var SomeInterface
     */
    private $foo;

Do not pass:
private $foo;

Question

All 3 comments

Everything is possible, please send a PR.

On the other side I'll suggest an approach that require minimal coding:

  1. the @return is already checked by PEAR.Functions.FunctionDeclaration sniff
  2. there is a feature request for sniff, checking declare(strict_types=1) presence (see #911)
  3. attempt to lint PHP file with declare(strict_types=1) in it, that doesn't have types placed would result in an error
  4. if developer then would add types to function declaration the FunctionDeclaration sniff mentioned before would check that they're also listed in PHPDoc

Probably modern IDE (e.g. PhpStorm) will already report missing types as error. If you're using build server adding a lint step would then fail the build if somebody commits such broken code.

Was this page helpful?
0 / 5 - 0 ratings