Skip to content

Array reduce not understanding callback return with generics #8071

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

Open
BackEndTea opened this issue Sep 27, 2022 · 6 comments
Open

Array reduce not understanding callback return with generics #8071

BackEndTea opened this issue Sep 27, 2022 · 6 comments
Labels
Milestone

Comments

@BackEndTea
Copy link
Contributor

Bug report

It seems array_reduce doesn't completely understand the return from the generics.

Code snippet that reproduces the problem

https://phpstan.org/r/e5bcd1ac-83f1-485a-98cd-0d11d1c3386f

Expected output

I expected no errors here

@ondrejmirtes ondrejmirtes added this to the Generics milestone Sep 27, 2022
@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest push in 1.9.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.2 (1 error)
+PHP 8.0 – 8.2
 ==========
 
-18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
+No errors
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (1 error)
 ==========
 
-18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
 33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
Full report

PHP 8.0 – 8.2

No errors

PHP 7.1 – 7.4 (1 error)

Line Error
33 Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.

@github-actions
Copy link

github-actions bot commented Nov 6, 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 Nov 6, 2022
@ondrejmirtes
Copy link
Member

This issue was never fixed, but merely silenced...

@ondrejmirtes ondrejmirtes reopened this Jan 27, 2023
@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (9 errors)
 ==========
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
-33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: Method Inheritance::callBack() has parameter $carry with no value type specified in iterable type array.
+33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: PHPDoc tag @param for parameter $carry with type array<TKey of (int|string), TValues of bool|float|int|string|null>|null is not subtype of native type array|null.
+33: Parameter $carry of method Inheritance::callBack() has invalid type null.
+35: Strict comparison using === between array|null and null will always evaluate to false.
+39: Argument of an invalid type array|null supplied for foreach, only iterables are supported.
+44: Cannot access offset (int|string) on array|null.
+47: Method Inheritance::callBack() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array|null.
Full report

PHP 8.0 – 8.2 (1 error)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int

PHP 7.1 – 7.4 (9 errors)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int
33 Method Inheritance::callBack() has parameter $carry with no value type specified in iterable type array.
33 Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
33 `PHPDoc tag @param for parameter $carry with type array<TKey of (int
33 Parameter $carry of method Inheritance::callBack() has invalid type null.
35 `Strict comparison using === between array
39 `Argument of an invalid type array
44 `Cannot access offset (int
47 `Method Inheritance::callBack() should return array<TKey of (int

@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.2 (1 error)
+PHP 8.0 – 8.2 (2 errors)
 ==========
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
+33: Static method Inheritance::callBack() is unused.
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (10 errors)
 ==========
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
-33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: Method Inheritance::callBack() has parameter $carry with no value type specified in iterable type array.
+33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: PHPDoc tag @param for parameter $carry with type array<TKey of (int|string), TValues of bool|float|int|string|null>|null is not subtype of native type array|null.
+33: Parameter $carry of method Inheritance::callBack() has invalid type null.
+33: Static method Inheritance::callBack() is unused.
+35: Strict comparison using === between array|null and null will always evaluate to false.
+39: Argument of an invalid type array|null supplied for foreach, only iterables are supported.
+44: Cannot access offset (int|string) on array|null.
+47: Method Inheritance::callBack() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array|null.
Full report

PHP 8.0 – 8.2 (2 errors)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int
33 Static method Inheritance::callBack() is unused.

PHP 7.1 – 7.4 (10 errors)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int
33 Method Inheritance::callBack() has parameter $carry with no value type specified in iterable type array.
33 Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
33 `PHPDoc tag @param for parameter $carry with type array<TKey of (int
33 Parameter $carry of method Inheritance::callBack() has invalid type null.
33 Static method Inheritance::callBack() is unused.
35 `Strict comparison using === between array
39 `Argument of an invalid type array
44 `Cannot access offset (int
47 `Method Inheritance::callBack() should return array<TKey of (int

@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

@@ @@
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (8 errors)
 ==========
 
 18: Method Inheritance::inherit() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<string>.
-33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
+33: PHPDoc tag @param for parameter $carry with type array<TKey of (int|string), TValues of bool|float|int|string|null>|null is not subtype of native type array|null.
+33: Parameter $carry of method Inheritance::callBack() has invalid type null.
+35: Strict comparison using === between array<TKey of (int|string), TValues of bool|float|int|string|null>|null and null will always evaluate to false.
+39: Argument of an invalid type array<TKey of (int|string), TValues of bool|float|int|string|null>|null supplied for foreach, only iterables are supported.
+44: Cannot access offset TKey of (int|string) on array<TKey of (int|string), TValues of bool|float|int|string|null>|null.
+47: Method Inheritance::callBack() should return array<TKey of (int|string), TValues of bool|float|int|string|null> but returns array<TKey of (int|string), TValues of bool|float|int|string|null>|null.
Full report

PHP 8.0 – 8.2 (1 error)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int

PHP 7.1 – 7.4 (8 errors)

Line Error
18 `Method Inheritance::inherit() should return array<TKey of (int
33 Method Inheritance::callBack() uses native union types but they're supported only on PHP 8.0 and later.
33 `PHPDoc tag @param for parameter $carry with type array<TKey of (int
33 Parameter $carry of method Inheritance::callBack() has invalid type null.
35 `Strict comparison using === between array<TKey of (int
39 `Argument of an invalid type array<TKey of (int
44 `Cannot access offset TKey of (int
47 `Method Inheritance::callBack() should return array<TKey of (int

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