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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/MabeEnum/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ final private function __construct($value, $ordinal = null)
$this->value = $value;
$this->ordinal = $ordinal;
}

/**
* Returns a list of all enumeration instances of actual implementation
Copy link
Owner

Choose a reason for hiding this comment

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

Returns a list of all enumerator instances of called enumeration type

Copy link
Author

Choose a reason for hiding this comment

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

yeah!

*
* @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!

{
$enums = array();
foreach (array_keys(self::getConstants()) as $v) {
$enums[] = self::getByName($v);
}

return $enums;
}
/**
* Get the current selected constant name
*
Expand Down