requiring type hint and return type
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 ?
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
Yes, i want same configuration but no way
@BeycanDeveloper Please take a moment to read; https://github.com/squizlabs/PHP_CodeSniffer/issues/3932
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