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

Support enums in metadata #639

Merged
merged 2 commits into from
Feb 3, 2022
Merged

Support enums in metadata #639

merged 2 commits into from
Feb 3, 2022

Conversation

imjoehaines
Copy link
Contributor

Goal

This PR improves our handling of enums when added as metadata

PHP has two types of enums, "pure" and "backed". A pure enum contains only cases with no values, whereas a backed enum has a string or int value for each case. For example:

enum PureEnum {
    case One;
    case Two;
}

enum StringBackedEnum: string {
    case One = 'one';
    case Two = 'two';
}

enum IntBackedEnum: int {
    case One = 1;
    case Two = 2;
}

Currently when a pure enum is added as metadata it falls into our generic object handling, which ends up converting it to null as there is no default serialisation for pure enums

This PR fixes this by converting the enum to a string in the form EnumName::CaseName, e.g. PureEnum::One using the example above

Backed enums aren't broken as they are converted into their backing values, e.g. StringBackedEnum::One is serialised as the string one. This could be improved however, given the enum name is usually more important than the raw value

This PR changes backed enums to be serialised in the form EnumName::CaseName (value), e.g. IntBackedEnum::One (1)

These changes result in the following being shown in the Bugsnag dashboard:

image

Testing

Two new PHPT tests have been added; one that tests pure enums and one that tests backed enums. PHPT tests are being used because PHP versions without enum support will fail to compile files that declare enums, making it a bit awkward to unit test

Copy link
Contributor

@kattrali kattrali left a comment

Choose a reason for hiding this comment

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

looks good! still envious of phpt tests for other platforms. lol

@imjoehaines imjoehaines merged commit d810ec3 into next Feb 3, 2022
@imjoehaines imjoehaines deleted the support-enums-in-metadata branch February 3, 2022 12:06
@imjoehaines imjoehaines mentioned this pull request Feb 4, 2022
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.

2 participants