Skip to content

search() is not type safe #13

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 Feb 9, 2015 · 2 comments
Closed

search() is not type safe #13

mirfilip opened this issue Feb 9, 2015 · 2 comments

Comments

@mirfilip
Copy link

mirfilip commented Feb 9, 2015

New search() method has a contract to find enum by value and return corresponding key. Logically, this method should return boolean false when nothing found or a key, which is always a string (not integer), as internally key => value pairs are kept as associative array with string keys. Because search() uses array_search without strict param, it can find a wrong key:

class EnumFixture extends Enum
{
    const FOO = "bar";
    const PROBLEMATIC_NUMBER = 0;
    const PROBLEMATIC_NULL = null;
    const PROBLEMATIC_EMPTY_STRING = '';
    const PROBLEMATIC_BOOLEAN_FALSE = false;
}

EnumFixture::search(false); // PROBLEMATIC_NUMBER
EnumFixture::search(''); // PROBLEMATIC_NUMBER
EnumFixture::search(null); // PROBLEMATIC_NUMBER
EnumFixture::search('bar I do not exist'); // PROBLEMATIC_NUMBER

EnumFixture::search(array()); // PROBLEMATIC_NULL
EnumFixture::search(0); // FOO ***WTF***

This is the same kind of a problem as #9. The fix is to simply enable strict checking on search. Yes, you should not use such values, but the last search example shows problem even if you use proper values. I can take care of this if you want @mnapoli

@mirfilip mirfilip changed the title Search() is not type safe search() is not type safe Feb 9, 2015
mirfilip added a commit to mirfilip/php-enum that referenced this issue Feb 9, 2015
@mirfilip
Copy link
Author

mirfilip commented Feb 9, 2015

Above there is a commit on my fork. It's based on changes from #9 which are not merged yet, so I will wait with this pull request until #9 is merged.

@mnapoli
Copy link
Member

mnapoli commented Feb 10, 2015

I've merged #12, feel free to create the other pull request.

mnapoli added a commit that referenced this issue Feb 15, 2015
Fix for search() is not type safe. Fixes #13
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

No branches or pull requests

2 participants