-
Notifications
You must be signed in to change notification settings - Fork 462
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
Fix constant array comparison for optional keys #1324
Fix constant array comparison for optional keys #1324
Conversation
src/Analyser/MutatingScope.php
Outdated
@@ -2274,19 +2274,19 @@ private function resolveEqualType(Type $leftType, Type $rightType): BooleanType | |||
*/ | |||
private function resolveConstantArrayTypeComparison(ConstantArrayType $leftType, ConstantArrayType $rightType, callable $valueComparisonCallback): BooleanType | |||
{ | |||
$leftValueTypes = $leftType->getValueTypes(); | |||
if ($leftType->hasOptionalKeys() || $rightType->hasOptionalKeys()) { | |||
return new BooleanType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can tell when it's always false even when optional keys are involved. This code doesn't seem right.
if I have array{a?: int}
and array{b? int}
, it can be true because both arrays can be empty.
But if I have array{c, int, d?: int}
, array{e: int, f?: int}
, we can be sure it's always false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is already handled by the supertype check in resolveIdenticalType
. I've added these as test cases.
I can merge this if it's ready :) |
4f1079c
to
c570bc7
Compare
It didn't work for the |
Thank you! |
Fixes phpstan/phpstan#7248. When optional keys are involved it's never certain whether to values are equal.