psr2r-sniffer icon indicating copy to clipboard operation
psr2r-sniffer copied to clipboard

Support for phpstan generics?

Open hemberger opened this issue 2 years ago • 1 comments

I was wondering if there is any chance that support would be added for PHPStan generics (https://phpstan.org/blog/generics-by-examples). They are very useful for getting precise type validation from static analysis, but the PSR-2-R sniffs aren't happy with them.

The following is an example of a doc block for a function that takes a class string and constructor arguments, and outputs an instance of the class.

/**
 * @template T of object
 * @param class-string<T> $name The name of the class to construct
 * @param mixed ...$args The arguments to pass to the constructor
 * @return T
 */

And here are the resulting PSR-2-R violations.

>> 35:  »   ·*·@template·T·of·object
   36:  »   ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
   37:  »   ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
----------------------------------------------------------------------------------------------------------------------------------------
LINE 36: ERROR   [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 36: ERROR   [x] Possible doc block error: `class-string<T> $name The name of the class to construct` seems to be missing type
                     `string`. (Spryker.Commenting.DocBlockParamAllowDefaultValue.Typehint)
LINE 36: ERROR   [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 36: ERROR   [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
----------------------------------------------------------------------------------------------------------------------------------------
   34:  »   ·*
   35:  »   ·*·@template·T·of·object
>> 36:  »   ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
   37:  »   ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
   38:  »   ·*·@return·T
----------------------------------------------------------------------------------------------------------------------------------------
LINE 38: ERROR   [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 38: ERROR   [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 38: ERROR   [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
----------------------------------------------------------------------------------------------------------------------------------------
   36:  »   ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
   37:  »   ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
>> 38:  »   ·*·@return·T
   39:  »   ·*/

I can certainly just skip these sniffs, but I thought I'd reach out in case there was any interest in supporting this feature. Thanks!

hemberger avatar May 30 '22 01:05 hemberger