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

How to do simple generics #5706

Closed
szepeviktor opened this issue Sep 30, 2021 · 11 comments
Closed

How to do simple generics #5706

szepeviktor opened this issue Sep 30, 2021 · 11 comments
Labels
Milestone

Comments

@szepeviktor
Copy link
Contributor

szepeviktor commented Sep 30, 2021

Bug report

There is a function that receives a parameter of string|int and returns string or integer.

Code snippet that reproduces the problem

https://phpstan.org/r/984d90d3-9515-4250-8613-cfc69b719abf

Expected output

No error.

Please help me achieve this.
Thank you.

@ondrejmirtes
Copy link
Member

This is fine, it should work.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Sep 30, 2021
szepeviktor referenced this issue in archtechx/laravel-seo Sep 30, 2021
@phpstan-bot
Copy link
Contributor

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

@@ @@
 PHP 8.0 (1 error)
 ==========
 
-11: Function bar() should return T of int|string but returns T of int.
+ 8: Function bar() should return T of int|string but return statement is missing.
 
 PHP 7.1 – 7.4 (2 errors)
 ==========
 
- 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
-11: Function bar() should return T of int|string but returns T of int.
+ 8: Function bar() should return T of int|string but return statement is missing.
+ 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
Full report

PHP 8.0 (1 error)

Line Error
8 `Function bar() should return T of int

PHP 7.1 – 7.4 (2 errors)

Line Error
8 `Function bar() should return T of int
8 Function bar() uses native union types but they're supported only on PHP 8.0 and later.

@szepeviktor
Copy link
Contributor Author

Function bar() should return T of int|string but return statement is missing.

@ondrejmirtes The last commit made PHPStan miss the return statement.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 (1 error)
+PHP 8.0 (3 errors)
 ==========
 
+ 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
+ 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
 11: Function bar() should return T of int|string but returns T of int.
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (4 errors)
 ==========
 
  8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
+ 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
+ 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
 11: Function bar() should return T of int|string but returns T of int.
Full report

PHP 8.0 (3 errors)

Line Error
8 `PHPDoc tag @param for parameter $key with type T of int
8 `PHPDoc tag @return with type T of int
11 `Function bar() should return T of int

PHP 7.1 – 7.4 (4 errors)

Line Error
8 Function bar() uses native union types but they're supported only on PHP 8.0 and later.
8 `PHPDoc tag @param for parameter $key with type T of int
8 `PHPDoc tag @return with type T of int
11 `Function bar() should return T of int

@ondrejmirtes
Copy link
Member

@arnaud-lb Oops, we broke it! T of string|int SHOULD be a subtype of string|int: https://phpstan.org/r/094e0fad-e14c-4343-877d-403f5d7e89e0

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.1 (3 errors)
+PHP 8.0 – 8.1 (1 error)
 ==========
 
- 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
- 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
 11: Function bar() should return T of int|string but returns T of int.
 
-PHP 7.1 – 7.4 (4 errors)
+PHP 7.1 – 7.4 (2 errors)
 ==========
 
  8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
- 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
- 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
 11: Function bar() should return T of int|string but returns T of int.
Full report

PHP 8.0 – 8.1 (1 error)

Line Error
11 `Function bar() should return T of int

PHP 7.1 – 7.4 (2 errors)

Line Error
8 Function bar() uses native union types but they're supported only on PHP 8.0 and later.
11 `Function bar() should return T of int

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 (1 error)
+PHP 8.0
 ==========
 
-11: Function bar() should return T of int|string but returns T of int.
+No errors
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (1 error)
 ==========
 
- 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
-11: Function bar() should return T of int|string but returns T of int.
+ 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
Full report

PHP 8.0

No errors

PHP 7.1 – 7.4 (1 error)

Line Error
8 Function bar() uses native union types but they're supported only on PHP 8.0 and later.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.1 (3 errors)
+PHP 8.0 – 8.1
 ==========
 
- 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
- 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
-11: Function bar() should return T of int|string but returns T of int.
+No errors
 
-PHP 7.1 – 7.4 (4 errors)
+PHP 7.1 – 7.4 (1 error)
 ==========
 
- 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
- 8: PHPDoc tag @param for parameter $key with type T of int|string is not subtype of native type int|string.
- 8: PHPDoc tag @return with type T of int|string is not subtype of native type int|string.
-11: Function bar() should return T of int|string but returns T of int.
+ 8: Function bar() uses native union types but they're supported only on PHP 8.0 and later.
Full report

PHP 8.0 – 8.1

No errors

PHP 7.1 – 7.4 (1 error)

Line Error
8 Function bar() uses native union types but they're supported only on PHP 8.0 and later.

@szepeviktor
Copy link
Contributor Author

Thank you.

@szepeviktor
Copy link
Contributor Author

The PHPStan t-shirt has arrived here 😃

@github-actions
Copy link

github-actions bot commented Mar 9, 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 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants