Skip to content

Get a list of all enumerator instances of the called enumeration type #45

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
wants to merge 2 commits into from

Conversation

olekhy
Copy link

@olekhy olekhy commented Jan 10, 2015

Added static method which helps get a list of all enumeration instances of actual implementation

Added static method which helps get a list of all enumeration instances of actual implementation
 change array notation to the old way
*
* @return Enum[]
*/
public static function getAll()
Copy link
Owner

Choose a reason for hiding this comment

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

final public static function getInstances()
{
    return array_map('self::getByName', array_keys(self::detectConstants(get_called_class()));
}

Thoughts ?

Copy link
Author

Choose a reason for hiding this comment

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

yeah!

I'll mention here:

  • using callable 'self::getByName' take execution time for 8% ca.
  • using foreach is faster as array_map not so wild (-2% - 5% on several versions of PHP and HHVM)
  • name of method maybe listEnum or getListEnumeration

Copy link
Owner

Choose a reason for hiding this comment

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

I like getInstances or getEnumerators much more as it's consistent with the name getConstants and the term list doesn't help and enum is ambiguous: enumeration type vs. enumerator element. I'm going to fix up the variable names of $enum as well.

I have benchmarked 3 versions but my version is a little bit faster :)
Tested an enumeration with 26 elements or PHP-5.5.6:

    final public static function getEnumerators1()
    {
        return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
    }

    final public static function getEnumerators2()
    {
        $enumerators = array_keys(self::detectConstants(get_called_class()));
        foreach ($enumerators as & $entry) {
            $entry = self::getByName($entry);
        }
        return $enumerators;
    }

    final public static function getEnumerators3()
    {
        $enums = array();
        foreach (array_keys(self::detectConstants(get_called_class())) as $v) {
            $enums[] = self::getByName($v);
        }
        return $enums;
    }
class MyEnum extends \MabeEnum\Enum {
    const A = 'a';
    const B = 'b';
    const C = 'c';
    const D = 'd';
    const E = 'e';
    const F = 'f';
    const G = 'g';
    const H = 'h';
    const I = 'i';
    const J = 'j';
    const K = 'k';
    const L = 'l';
    const M = 'm';
    const N = 'n';
    const O = 'o';
    const P = 'p';
    const Q = 'q';
    const R = 'r';
    const S = 's';
    const T = 't';
    const U = 'u';
    const V = 'v';
    const W = 'w';
    const X = 'x';
    const Y = 'y';
    const Z = 'z';
}

MyEnum::getConstants();

echo 'getEnumerators1(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    MyEnum::getEnumerators1();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;


echo 'getEnumerators2(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    MyEnum::getEnumerators2();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;


echo 'getEnumerators3(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    MyEnum::getEnumerators3();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
php bench.php 
getEnumerators1(): 0.53819012641907
getEnumerators2(): 0.60042691230774
getEnumerators3(): 0.60367202758789

Copy link
Author

Choose a reason for hiding this comment

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

@marc-mabe thx!

@prolic
Copy link
Collaborator

prolic commented Feb 1, 2015

Is it possible to have this in integrated until 2.0.0? Also: What's the time schedule for 2.0.0?

@marc-mabe
Copy link
Owner

@prolic I'll release the first beta of 2.0.0 the next week(s) and the first stable ~2-3 weeks later.
If you need this before 2.0.0 I can cherry-pick this commit into the 1.x branch. There will be a 1.x release the same time as 2.0.0 but I'm currently not sure if I need a beta, too.

@marc-mabe marc-mabe self-assigned this Feb 2, 2015
@prolic
Copy link
Collaborator

prolic commented Feb 2, 2015

No leaving this for 2.0.0 is okay (at least for me). But I have nothing against, of you want to release 1.x. An additional beta should not be needed. Giving it available for testing in master is sufficient (just like we do it in ZF2). I am just curious for the 2.0.0 release, as I have some classes integrating php-enum with doctrine 2, and the "real bitset" implementation would lead to some minor refactoring, making the code much easier on my side. I am planning to release it, as soon as 2.0.0 is available.

@prolic
Copy link
Collaborator

prolic commented Feb 2, 2015

By the way: My vote is for "getEnumerators" as the method name ;)

Mhh.. can I vote? lol

@marc-mabe marc-mabe added this to the 2.0.0 milestone Feb 2, 2015
@marc-mabe marc-mabe closed this in b9b545d Feb 2, 2015
@marc-mabe marc-mabe changed the title Update Enum.php Get a list of all enumerator instances of the called enumeration type Feb 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants