Completed
Push — master ( d2af39...7e6766 )
by Robin
12s
created

AutoDiscoveredValuesTrait::values()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) 2016 Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum;
12
13
trait AutoDiscoveredValuesTrait
14
{
15
    /** @var array */
16
    private static $guessedValues = [];
17
18
    /**
19
     * @see EnumInterface::values()
20
     *
21
     * @return int[]|string[]
22
     */
23
    public static function values(): array
24
    {
25
        $enumType = static::class;
26
27
        if (!isset(self::$guessedValues[$enumType])) {
28
            $values = (new \ReflectionClass($enumType))->getConstants();
29
30
            if (is_a($enumType, FlaggedEnum::class, true)) {
31
                $values = array_filter($values, function ($v) {
32
                    return 0 === ($v & $v - 1) && $v > 0;
33
                });
34
            }
35
36
            self::$guessedValues[$enumType] = array_values($values);
37
        }
38
39
        return self::$guessedValues[$enumType];
40
    }
41
}
42