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

Generics restricted to array types are reporting errors #3384

Closed
stof opened this issue Jun 2, 2020 · 8 comments
Closed

Generics restricted to array types are reporting errors #3384

stof opened this issue Jun 2, 2020 · 8 comments

Comments

@stof
Copy link
Contributor

stof commented Jun 2, 2020

Bug report

I'd like to use a generic class, where the generic type is always an array of scalars, but the exact shape can be configured when using it. The typechecker already handles this fine for code using the class, but it reports errors in the class definition, for the @template annotation and for the @param annotation.

Code snippet that reproduces the problem

https://phpstan.org/r/e5cb3813-71b9-42b2-8e66-bcb65c8d2bcd

Expected output

Only the error at line 25 should be reported.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Jun 2, 2020
@phpstan-bot
Copy link
Contributor

@stof After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
  4: PHPDoc tag @template T for class HelloWorld with bound type array<bool|float|int|string|null> is not supported.
  9: Method HelloWorld::sayHello() has parameter $d with no value type specified in iterable type array.
- 9: PHPDoc tag @param for parameter $d with type T is not subtype of native type array.
-25: Parameter #1 $d of method HelloWorld<array<string, bool|string>>::sayHello() expects array('a' => string, 'b' => bool), array('a' => string, 'b' => DateTime) given.
+ 9: PHPDoc tag @param for parameter $d with type T is not subtype of native type array.
Full report
Line Error
4 `PHPDoc tag @template T for class HelloWorld with bound type array<bool
9 Method HelloWorld::sayHello() has parameter $d with no value type specified in iterable type array.
9 PHPDoc tag @param for parameter $d with type T is not subtype of native type array.

@stof
Copy link
Contributor Author

stof commented Mar 15, 2021

This now kept only the errors I considered false positives, removing the one I considered OK...

@phpstan-bot
Copy link
Contributor

@stof After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
- 4: PHPDoc tag @template T for class HelloWorld with bound type array<bool|float|int|string|null> is not supported.
- 9: Method HelloWorld::sayHello() has parameter $d with no value type specified in iterable type array.
- 9: PHPDoc tag @param for parameter $d with type T is not subtype of native type array.
+ 4: PHPDoc tag @template T for class HelloWorld with bound type null is not supported.
 25: Parameter #1 $d of method HelloWorld<array<string, bool|string>>::sayHello() expects array('a' => string, 'b' => bool), array('a' => string, 'b' => DateTime) given.
Full report
Line Error
4 PHPDoc tag @template T for class HelloWorld with bound type null is not supported.
25 `Parameter #1 $d of method HelloWorld<array<string, bool

@stof
Copy link
Contributor Author

stof commented Sep 17, 2021

@ondrejmirtes any reason to forbid null for the bound types when using a union type in the array bound type ?

@ondrejmirtes
Copy link
Member

@stof No reason, just that someone needs to step up and implement TemplateNullType similar to this commit: phpstan/phpstan-src@4e7d6c1

I believe it's really straightforward.

@phpstan-bot
Copy link
Contributor

@stof After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
- 4: PHPDoc tag @template T for class HelloWorld with bound type array<bool|float|int|string|null> is not supported.
- 9: Method HelloWorld::sayHello() has parameter $d with no value type specified in iterable type array.
- 9: PHPDoc tag @param for parameter $d with type T is not subtype of native type array.
-25: Parameter #1 $d of method HelloWorld<array<string, bool|string>>::sayHello() expects array('a' => string, 'b' => bool), array('a' => string, 'b' => DateTime) given.
+ 4: PHPDoc tag @template T for class HelloWorld with bound type null is not supported.
+25: Parameter #1 $d of method HelloWorld<array<string, bool|string>>::sayHello() expects array{a: string, b: bool}, array{a: string, b: DateTime} given.
Full report
Line Error
4 PHPDoc tag @template T for class HelloWorld with bound type null is not supported.
25 `Parameter #1 $d of method HelloWorld<array<string, bool

@ondrejmirtes
Copy link
Member

Solved the nullable union bound: phpstan/phpstan-src@1d8b2eb

@github-actions
Copy link

github-actions bot commented Mar 3, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants