PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

requiring type hint and return type

Open mathroc opened this issue 7 years ago • 8 comments

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 ?

mathroc avatar Jun 15 '18 09:06 mathroc

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.

gsherwood avatar Jun 18 '18 00:06 gsherwood

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

mathroc avatar Jun 18 '18 08:06 mathroc

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

mathroc avatar Aug 21 '18 07:08 mathroc

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.

adaamz avatar Oct 20 '18 21:10 adaamz

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

TomasVotruba avatar Dec 13 '18 10:12 TomasVotruba

tly without having to repeat

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

lifesboy avatar Dec 14 '20 10:12 lifesboy

Yes, i want same configuration but no way

0xBeycan avatar Dec 28 '23 10:12 0xBeycan

@BeycanDeveloper Please take a moment to read; https://github.com/squizlabs/PHP_CodeSniffer/issues/3932

ldebrouwer avatar Dec 28 '23 11:12 ldebrouwer

Since all static analysis tools (phpstan, psalm) check for the type and report an error if it's missing (neither phpdoc nor type hint exist), implementing this doesn't really make sense anymore.

Probably the existing phpdoc only errors can be deprecated eventually

*I know this repo is not in use anymore, but opening a new issue in the new repo for this comment didn't make sense

kkmuffme avatar Aug 10 '25 19:08 kkmuffme