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

Enum collapsing #7655

Merged
merged 3 commits into from
Feb 13, 2022
Merged

Enum collapsing #7655

merged 3 commits into from
Feb 13, 2022

Conversation

orklah
Copy link
Collaborator

@orklah orklah commented Feb 12, 2022

This improves the situation of #7654 but without fixing it.

It does fix another form of the same issue when using a if() instead of a coalesce assignment.

I tried to find why both forms doesn't do the same thing and it boils down to the differences between TernaryAnalyzer (which process coalesce and ternary operations) and IfElseAnalyzer(which process if block operations).

I tried to bring more consistency between the two but there are still major differences and I failed to find the solution.

Basically, there's just a missing Type::combineUnionType() somewhere, but I just couldn't find where to apply it.

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Feb 12, 2022
@orklah orklah force-pushed the enumCollapsing branch 2 times, most recently from 1386e8b to 94bc960 Compare February 12, 2022 20:59
@@ -334,6 +335,11 @@ public static function combine(
}

if ($combination->named_object_types !== null) {
foreach ($combination->value_types as $key => $atomic_type) {
if ($atomic_type instanceof TEnumCase && isset($combination->named_object_types[$atomic_type->value])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that enum case can be represented as TClassConstant when it comes from docblock, e.g.

/** @var Code::Ok|int $code */

Ideally we'd want to check if the const is actually a enum case (by calling $codebase->classlikes->enumExists($atomic->value)), but $codebase is not available during scanning phase. I tried to pass it from TypeParser, but that leads to unrelated errors like SplFileInfo not existing prior to stub files scanning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's not great :( Maybe we should have a different syntax for enum cases?

But thinking about it, it may not be too bad. The type from docblock will be collapsed at some point anyway, it may not be a problem

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should have a different syntax for enum cases?

Well, why? Enum cases are constants, so it makes sense to use the same syntax.

The type from docblock will be collapsed at some point anyway

Doesn't seem to be happening, even in your branch: https://psalm.dev/r/1698218e19

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why

Well, exactly because of what you're describing. An Enum and a property of an enum should be collapsed but this is not the case for constants

Copy link
Collaborator

Choose a reason for hiding this comment

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

That doesn't mean we should introduce a new syntax for cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure what we can do then. I'll merge this for now, it's at least a good first step

@orklah orklah merged commit 59d3d2a into vimeo:master Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants