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

Support for phpstan generics? #29

Open
hemberger opened this issue May 30, 2022 · 1 comment
Open

Support for phpstan generics? #29

hemberger opened this issue May 30, 2022 · 1 comment

Comments

@hemberger
Copy link
Contributor

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!

@dereuromark
Copy link
Contributor

Feel free to PR any needed change.
So far normal generic Syntax is already supported.
But not these templates yet I guess

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

No branches or pull requests

2 participants