Skip to content

Array reduce not understanding callback return with generics #8071

Open

Description

@BackEndTea

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

Activity

  1. added this to the Generics milestone on Sep 27, 2022
  2. phpstan-bot commented on Oct 5, 2022

    @phpstan-bot
    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.
  3. github-actions commented on Nov 6, 2022

    @github-actions

    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.

  4. locked as resolved and limited conversation to collaborators on Nov 6, 2022
  5. ondrejmirtes commented on Jan 27, 2023

    @ondrejmirtes
    Member

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

  6. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    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
  7. phpstan-bot commented on Jan 4, 2025

    @phpstan-bot
    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
  8. phpstan-bot commented on Apr 29, 2025

    @phpstan-bot
    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
  9. phpstan-bot commented on Aug 4, 2025

    @phpstan-bot
    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>|(iterable&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
  10. phpstan-bot commented on Sep 22, 2025

    @phpstan-bot
    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)
     ==========
     
    +15: Return type array<TKey of (int|string), TValues of bool|float|int|string|null> of method Inheritance::inherit() can be narrowed to array<TKey of (int|string), string>.
     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)
     ==========
     
    +15: Return type array<TKey of (int|string), TValues of bool|float|int|string|null> of method Inheritance::inherit() can be narrowed to array<TKey of (int|string), string>.
     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 (2 errors)

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

    PHP 7.1 – 7.4 (9 errors)

    Line Error
    15 `Return type array<TKey of (int
    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions