Php_codesniffer: requiring type hint and return type

Created on 15 Jun 2018  Â·  6Comments  Â·  Source: squizlabs/PHP_CodeSniffer

Hi,

before php 7 I used to have those rules:

    <rule ref="Squiz.Commenting.FunctionComment.Missing"/>
    <rule ref="Squiz.Commenting.FunctionComment.MissingParamTag"/>
    <rule ref="Squiz.Commenting.FunctionComment.MissingParamName"/>
    <rule ref="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
    <rule ref="Squiz.Commenting.FunctionComment.IncorrectTypeHint"/>
    <rule ref="Squiz.Commenting.FunctionComment.MissingReturn"/>

to make sure that every function had its parameters and return type specified somewhere. now with php 7 I'd like to do specify the types directly without having to repeat that, eg:

// before:
/**
 * @param string $bar
 * @param Option $o
 * @param int[] $ids
 * @return string
 */
function foo($bar, Option $o, array $ids) {
    // …
}

// after:
/**
 * @param int[] $ids
 */
function foo(string $bar, Option $o, array $ids): string {
    // …
}

So that's probably multiple sniffs, note that those should fail:

function foo($bar): string {
    // $bar should be type hinted
}
/**
 * @param int $bar
 */
function foo(string $bar): string {
    // $bar: string does not include int
}
/**
 * @param int[] $ids
 */
function foo($ids): string {
    // $ids should be `array` type hinted
}
/**
 * @return string[]
 */
function foo() {
    // function return type should be type hinted `array`
}
function foo() {
    // function return type should be type hinted
}

for that I propose those sniffs:

    <rule ref="Generic.Function.MissingParamTypeHint"/>
    <rule ref="Generic.Function.IncorrectParamTypeHint"/>
    <rule ref="Generic.Function.MissingReturnTypeHint"/>
    <rule ref="Generic.Function.IncorrectReturnTypeHint"/>

what do you think, would that be useful ?

Enhancement

Most helpful comment

To even automate migration from docblocks to typehint you can use Rector, that changes the code and check for conflicts with parent classes (in and out the `/vendor):
https://www.tomasvotruba.cz/blog/2018/12/10/rocket-science-behind-migration-of-docblock-types-to-php-typehints/#give-your-code-a-typehint-facelift

All 6 comments

A sniff to ensure that function params and return values are type hinted sounds good, although it couldn't tell you if the type hint is accurate.

Another sniff (or a change to the Squiz.Commenting.FunctionComment sniff) could be useful for ensuring your documentation (if any) matches what the code says - probably using the code as the authoritative source.

So yes, I think some new sniffs could be useful here.

great

A sniff to ensure that function params and return values are type hinted sounds good, although it couldn't tell you if the type hint is accurate.

With IncorrectParamTypeHint I don't expect phpcs to look if I actually return something valid. I was only thinking of accuracy regarding the comment type hint. but maybe it's best to leave it in Squiz.Commenting.FunctionComment

this projects provides smilar sniffs for phpcs : https://github.com/slevomat/coding-standard

For typehints for function parameters it is better to use more complex solution like phpstan (https://github.com/phpstan/phpstan-strict-rules). Problem of phpcs is that it looks only on 1 one/class at once. You get errors/warning if parent class has no typehints, if you add typehint you get php error.

To even automate migration from docblocks to typehint you can use Rector, that changes the code and check for conflicts with parent classes (in and out the `/vendor):
https://www.tomasvotruba.cz/blog/2018/12/10/rocket-science-behind-migration-of-docblock-types-to-php-typehints/#give-your-code-a-typehint-facelift

tly without having to repeat

I get this error:
ERROR: Referenced sniff "Generic.Function.MissingParamTypeHint" does not exist

Was this page helpful?
0 / 5 - 0 ratings