Skip to content

Union Type + Nullable yields "Expects TEntity(A|B)|null, TEntity(A|B)|null given" #5591

Closed
phpstan/phpstan-src
#992

Description

@BusterNeece

Bug report

Hello! I'm having a counterintuitive moment after my recent PHPStan update where I'm getting an error message that reads out like:

  15     Parameter #1 $record of method App\TestClass<TEntity of App\Entity\Station|App\Entity\User>::innerMethod() expects
         (TEntity of App\Entity\Station|App\Entity\User)|null, (TEntity of App\Entity\Station|App\Entity\User)|null given.

Basically, it's saying it expected, and was given, the exact same type. It shouldn't be an error at all.

I think it's getting tripped up by the TEntity as A|B declaration, then later having TEntity|null being defined as a method parameter.

Was able to reproduce this via the web PHPStan tester. Can verify via my own local code that the same issue isn't present in 0.12.96 but is present in both 0.12.97 and 0.12.98.

Code snippet that reproduces the problem

https://phpstan.org/r/cc421f16-0749-407d-80ac-e05e7624d3f7

Expected output

No issues with the sample code.

Did PHPStan help you today? Did it make you happy in any way?

PHPStan helps me every day! It's a wonderful tool and I'm proud to be one of its supporters. <3

Activity

  1. added this to the Generics milestone on Sep 5, 2021
  2. jrmajor commented on Sep 22, 2021

    @jrmajor

    Adding my reproduction I've created for #5677 (I think it's a bit simpler):

    https://phpstan.org/r/0391a032-1553-47a8-88ad-c0f528d8dd3d

  3. mitelg commented on Oct 11, 2021

    @mitelg

    I also have some trouble with generics and null
    see this discussion 🙂 #5686

  4. alexander-schranz commented on Jan 25, 2022

    @alexander-schranz

    I think I'm running into the same issue as I'm trying to use:

    @template T of string|null
    

    https://phpstan.org/r/90499164-0265-4b6a-89de-b311ddcb82c4

    The type seems to be handled correctly. I'm just getting:

    PHPDoc tag @template T for method HelloWorld::getEffectiveDimensionAttributes() with bound type null is not supported.
    

    A more simpler usecase is: https://phpstan.org/r/9e0eebc8-a3e5-4cc9-9fbf-e182e71fdd93

    /cc @ondrejmirtes as it seems to correct analyzed can the bound type null message be removed?

  5. phpstan-bot commented on Jan 30, 2022

    @phpstan-bot
    Contributor

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

    @@ @@
    -18: PHPDoc tag @template T for method HelloWorld::getEffectiveDimensionAttributes() with bound type null is not supported.
    +No errors
  6. phpstan-bot commented on Jan 30, 2022

    @phpstan-bot
    Contributor

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

    @@ @@
    -12: PHPDoc tag @template T for method HelloWorld::getEffectiveDimensionAttributes() with bound type null is not supported.
    +No errors
  7. alexander-schranz commented on Feb 4, 2022

    @alexander-schranz

    Thank you @ondrejmirtes and @arnaud-lb 👍

  8. jrmajor commented on Feb 4, 2022

    @jrmajor

    Thank you!

  9. github-actions commented on Mar 8, 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.

  10. locked as resolved and limited conversation to collaborators on Mar 8, 2022
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