Skip to content

search() is not type safe #13

Closed
Closed
@mirfilip

Description

@mirfilip

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions