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

Infer ::from() and ::tryFrom() return types on backed enums #7011

Merged
merged 1 commit into from Nov 28, 2021

Conversation

weirdan
Copy link
Collaborator

@weirdan weirdan commented Nov 28, 2021

Fixes #6429

@weirdan weirdan added the release:feature The PR will be included in 'Features' section of the release notes label Nov 28, 2021
@weirdan weirdan added this to the PHP 8.1 milestone Nov 28, 2021
@weirdan weirdan requested a review from orklah November 28, 2021 07:39
foreach ($original_class_storage->enum_cases as $case_name => $case_storage) {
if (UnionTypeComparator::isContainedBy(
$source_analyzer->getCodebase(),
\is_int($case_storage->value) ?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to use a constant to initialize a case? If it happens, is_int() will fail. Can't we access the type at this moment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's possible, and it fails. But not here.

https://3v4l.org/WHXIV#v8.1rc3
https://psalm.dev/r/3547ca91d3

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Built-in constants work.
User-defined constants don't: https://3v4l.org/nhGRA#v8.1rc3
Class constants work if the class is defined before the enum: https://3v4l.org/bcYPN#v8.1rc3.
Class constants from autoloaded classes do not work: https://3v4l.org/AjOCi#v8.1rc3

3v4l doesn't have proper 8.1 yet, so it's based on RC3. Some of the above could have been fixed before final release.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some of the above could have been fixed before final release.

I've just built 8.1 branch, and there are no changes for those cases.

@weirdan weirdan merged commit cd2abc7 into vimeo:master Nov 28, 2021
@weirdan weirdan deleted the backed-enum-from branch November 28, 2021 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enums: Implement callmap Enum::from, Enum::tryFrom and Enum::cases
2 participants