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

Squiz.Commenting.FunctionComment.IncorrectTypeHint recognizes generics a part of type hints #3589

Open
mirucon opened this issue May 8, 2022 · 1 comment

Comments

@mirucon
Copy link

mirucon commented May 8, 2022

Describe the bug
The Squiz.Commenting.FunctionComment.IncorrectTypeHint sniff gives me an error that I should include the generics part <> in type hints, which is not valid PHP code.

It works good with @return or array for @params, but it definitely has a problem with other types as in the code sample below.

As Laravel 9 and PhpStorm 2022.1 have recently supported generics, I think the sniff's problem can be a barrier for their users.

Code sample

use Illuminate\Support\Collection;

/**
 * @param array<int, string>                          $array
 * @param iterable<string>                            $iterable
 * @param int<0, 100>                                 $int
 * @param \Illuminate\Support\Collection<int, string> $collection
 *
 * @return \Illuminate\Support\Collection<int, string>
 */
function test(
    array $array,
    iterable $iterable,
    int $int,
    Collection $collection,
): Collection {
    return new Collection();
}

Custom ruleset
N/A

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
 15 | ERROR   | [ ] Expected type hint "iterable<string>"; found "iterable" for $iterable (Squiz.Commenting.FunctionComment.IncorrectTypeHint)
 15 | ERROR   | [ ] Expected type hint "int<0, 100>"; found "int" for $int (Squiz.Commenting.FunctionComment.IncorrectTypeHint)
 15 | ERROR   | [ ] Expected type hint "\Illuminate\Support\Collection<int, string>"; found "Collection" for $collection
    |         |     (Squiz.Commenting.FunctionComment.IncorrectTypeHint)

Expected behavior
Errors should not occur with generics <>, just like how it works for an array.

Versions (please complete the following information):

  • OS: Windows 10 WSL 2 Ubuntu 18.04
  • PHP: 8.0
  • PHPCS: 3.6.2
  • Standard: Squiz
@jrfnl
Copy link
Contributor

jrfnl commented May 16, 2022

I haven't ran any tests, but did see that PR #3544 is open for a similar issue. Would be interesting to see if that PR fixes this issue as well.

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

No branches or pull requests

2 participants