Skip to content

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

@mirucon

Description

@mirucon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions