Skip to content

Fix type inference of array_sum() #2591

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

Merged
merged 12 commits into from
Aug 31, 2023

Conversation

zer0-star
Copy link
Contributor

@zer0-star zer0-star force-pushed the fix/array_sum_extension branch from 80340fa to e8e6a0d Compare August 28, 2023 03:14
@zer0-star
Copy link
Contributor Author

@ondrejmirtes Could you review this PR?

$types = $constantArray->getValueTypes();

foreach ($types as $type) {
$scalar = $type->getConstantScalarValues();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I don't really love that this PR is working with specific scalar values and calls array_sum internally. It misses out on various properties of the type system, like working with IntegerRangeType.

I'd much rather be if for ConstantArrayType objects, like [$one, $two, $three], it tried to do $scope->getType(new Plus($one, new Plus($two, $three))). Because Plus expr handling in MutatingScope already knows how to work with al possible Types, we can just call it here like that.

Because ConstantArrayType contains Type and not Expr, you can take advantage of TypeExpr virtual node to build an Expr out of Type instances.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the idea :) And we don't have to limit ourselves to a single constant array, we can union results from all constant arrays. For example when something like array{1}|array{2, 3} is passed in.

@zer0-star zer0-star marked this pull request as draft August 28, 2023 10:13
@zer0-star
Copy link
Contributor Author

Thank you very much for your review! I will continue working it in tomorrow.

@zer0-star zer0-star marked this pull request as ready for review August 29, 2023 05:32
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@zer0-star zer0-star requested a review from ondrejmirtes August 29, 2023 05:42
@zer0-star
Copy link
Contributor Author

@ondrejmirtes
Hello, can I ask you a question?
In the test case foo4, the inferred type is BenevolentUnionType, so the result becomes (float|int). Do you think it's needed to fix this?

@ondrejmirtes
Copy link
Member

@zer0-star No, that's totally fine and actually a nice side-effect of the changes :)

@zer0-star
Copy link
Contributor Author

zer0-star commented Aug 30, 2023

@ondrejmirtes
I think this PR is ready to be reviewed! I'm sorry to rush you, but could you review it for me?

@ondrejmirtes ondrejmirtes force-pushed the fix/array_sum_extension branch from 10d0388 to 6524665 Compare August 30, 2023 10:46
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I really appreciate this, especially the thorough test suite :) I wanted to write a simplification and it helped me a lot to get it right.

I also pushed a failing test case - ConstantArrayType might have an optional key. This is how to check for that:

foreach ($constantArray->getValueTypes() as $i => $type) {
    if ($constantArray->isOptionalKey($i)) { ... }
}

@zer0-star
Copy link
Contributor Author

Thanks so much for your help! I really appreciate it.
I will fix this problem later.

Is there something else to revise?

@ondrejmirtes
Copy link
Member

No, once the optional keys are supported, we are good to merge this :)

@zer0-star
Copy link
Contributor Author

I fixed it! I hope you like this change 🤗

@zer0-star zer0-star requested a review from ondrejmirtes August 31, 2023 02:00
@ondrejmirtes ondrejmirtes force-pushed the fix/array_sum_extension branch from 63ec5dc to 291bb57 Compare August 31, 2023 07:59
@ondrejmirtes
Copy link
Member

ondrejmirtes commented Aug 31, 2023

I don't really like any instanceof *Type, it's usually a code smell. Explained here: https://phpstan.org/blog/why-is-instanceof-type-wrong-and-getting-deprecated

There should be nothing special about MixedType and there were probably other types that needed a similar treatment. I was also able to get rid of other problems by doing ->toNumber() on the resulting type. See: 291bb57?w=1

I'll merge the PR if the build gets green :) Thank you very much for your cooperation!

@zer0-star
Copy link
Contributor Author

zer0-star commented Aug 31, 2023

Thanks! I was happy to contribute to PHPStan.

I learned a lot from your review!

@zer0-star zer0-star deleted the fix/array_sum_extension branch August 31, 2023 08:15
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.

None yet

3 participants