Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing return type sniff / inspection in PHP7 #1222

Closed
LPodolski opened this issue Nov 25, 2016 · 3 comments
Closed

Missing return type sniff / inspection in PHP7 #1222

LPodolski opened this issue Nov 25, 2016 · 3 comments

Comments

@LPodolski
Copy link

LPodolski commented Nov 25, 2016

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;

@aik099
Copy link
Contributor

aik099 commented Nov 25, 2016

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 Add new sniff for strict _types declaration #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.

@kukulich
Copy link
Contributor

kukulich commented Jan 2, 2017

@LPodolski You can try https://github.com/slevomat/coding-standard/blob/2.0-dev/SlevomatCodingStandard/Sniffs/Typehints/TypeHintDeclarationSniff.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants