Union Type + Nullable yields "Expects TEntity(A|B)|null, TEntity(A|B)|null given" #5591
Copy link
Copy link
Closed
phpstan/phpstan-src
#992Description
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
Reactions are currently unavailable
Activity
jrmajor commentedon Sep 22, 2021
Adding my reproduction I've created for #5677 (I think it's a bit simpler):
https://phpstan.org/r/0391a032-1553-47a8-88ad-c0f528d8dd3d
mitelg commentedon Oct 11, 2021
I also have some trouble with generics and
nullsee this discussion 🙂 #5686
alexander-schranz commentedon Jan 25, 2022
I think I'm running into the same issue as I'm trying to use:
https://phpstan.org/r/90499164-0265-4b6a-89de-b311ddcb82c4
The
typeseems to be handled correctly. I'm just getting: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 nullmessage be removed?phpstan-bot commentedon Jan 30, 2022
@alexander-schranz After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
phpstan-bot commentedon Jan 30, 2022
@alexander-schranz After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
alexander-schranz commentedon Feb 4, 2022
Thank you @ondrejmirtes and @arnaud-lb 👍
jrmajor commentedon Feb 4, 2022
Thank you!
github-actions commentedon Mar 8, 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.