Completed
Pull Request — master (#49)
by Maxime
01:42
created

AutoDiscoveredValuesTrait::autodiscoveredValues()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 5
Ratio 20 %

Importance

Changes 0
Metric Value
dl 5
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 5
nop 0
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum;
12
13
use Elao\Enum\Exception\LogicException;
14
15
/**
16
 * Auto-discover enumerated values from public constants.
17
 */
18
trait AutoDiscoveredValuesTrait
19
{
20
    /** @var array */
21
    private static $guessedValues = [];
22
23
    /**
24
     * @see EnumInterface::values()
25
     *
26
     * @return int[]|string[]
27
     */
28
    public static function values(): array
29
    {
30
        return static::autodiscoveredValues();
31
    }
32
33
    /**
34
     * @see ChoiceEnumTrait::choices()
35
     */
36
    protected static function choices(): array
37
    {
38
        if (!in_array(ChoiceEnumTrait::class, class_uses(self::class, false), true)) {
39
            throw new LogicException(sprintf(
40
                'Method "%s" is only meant to be used when using the "%s" trait which is not used in "%s"',
41
                __METHOD__,
42
                ChoiceEnumTrait::class,
43
                static::class
44
            ));
45
        }
46
47
        return array_combine(static::autodiscoveredValues(), static::autodiscoveredValues());
48
    }
49
50
    /**
51
     * @internal
52
     */
53
    private static function autodiscoveredValues(): array
54
    {
55
        $enumType = static::class;
56
57
        if (!isset(self::$guessedValues[$enumType])) {
58
            $r = new \ReflectionClass($enumType);
59
            $values = $r->getConstants();
60
61 View Code Duplication
            if (PHP_VERSION_ID >= 70100) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                $values = array_filter($values, function (string $k) use ($r) {
63
                    return $r->getReflectionConstant($k)->isPublic();
64
                }, ARRAY_FILTER_USE_KEY);
65
            }
66
67
            if (is_a($enumType, FlaggedEnum::class, true)) {
68
                $values = array_filter($values, function ($v) {
69
                    return is_int($v) && 0 === ($v & $v - 1) && $v > 0;
70
                });
71
            }
72
73
            self::$guessedValues[$enumType] = array_values($values);
74
        }
75
76
        return self::$guessedValues[$enumType];
77
    }
78
}
79