Skip to content
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

Merged
merged 5 commits into from
May 18, 2022

Conversation

rvanvelzen
Copy link
Contributor

Fixes phpstan/phpstan#7248. When optional keys are involved it's never certain whether to values are equal.

@@ -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();
Copy link
Member

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.

Copy link
Contributor Author

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.

@rvanvelzen rvanvelzen marked this pull request as draft May 17, 2022 11:46
@ondrejmirtes
Copy link
Member

I can merge this if it's ready :)

@rvanvelzen
Copy link
Contributor Author

It didn't work for the == case, but it should cover all cases now :)

@rvanvelzen rvanvelzen marked this pull request as ready for review May 18, 2022 15:04
@ondrejmirtes ondrejmirtes merged commit 40ae66b into phpstan:1.7.x May 18, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@rvanvelzen rvanvelzen deleted the constant-array-supertype branch June 13, 2022 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive same comparison
2 participants