Skip to content

Ability to create invalid enum #9

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

Closed
mirfilip opened this issue Jan 31, 2015 · 2 comments
Closed

Ability to create invalid enum #9

mirfilip opened this issue Jan 31, 2015 · 2 comments
Labels

Comments

@mirfilip
Copy link

Depending on how you choose consts values in your Enum class, it can have very undesirable effects. This is because the library uses in_array (constructor) and array_search (new search method) without strict checking. To get the notion of what I mean, please look at: https://ideone.com/FvouJm

The same can happen if you use many other problematic values like null, booleans, empty string etc. While majority of them are impractical to use, having value as integer 0 is. My point is, if you chose to have such a weird values, it's up to you, but the library should never allow to create enums from out of range.

You can really see the problem when you create enums from user input:

  • converting query strings params to enums
  • creating enums from arguments in php CLI apps

I think it'd be worthwhile to add that to docs too. I have a PR ready with the fix, just needs some styling changes. What do you think ?

tl;dr You can create enums from out of defined values

class MyEnum extends Enum {
    const A = 0;
    const B = 1;
}

new MyEnum('any string'); // SUCCESS but it should throw \UnexpectedValueException
@mnapoli mnapoli added the bug label Jan 31, 2015
@mnapoli
Copy link
Member

mnapoli commented Jan 31, 2015

This looks like a buggy behavior to me :/

If you have a PR feel free to submit it (especially if it can cover that with tests to avoid any future regression).

@mnapoli
Copy link
Member

mnapoli commented Feb 15, 2015

Released 1.3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants