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

AutoDiscoveredReadablesTrait::readables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 readable values from public constants.
17
 *
18
 * Meant to be used within a {@link \Elao\Enum\ReadableEnumInterface} implementation.
19
 */
20
trait AutoDiscoveredReadablesTrait
21
{
22
    use AutoDiscoveredValuesTrait {
23
        AutoDiscoveredValuesTrait::values as autodiscoveredValues;
24
    }
25
26
    /** @internal */
27
    private static $guessedReadables = [];
28
29
    /** @internal */
30
    private static $called = 0;
31
32
    /**
33
     * @see ReadableEnumInterface::readables()
34
     *
35
     * @return string[] labels indexed by enumerated value
36
     */
37
    public static function readables(): array
38
    {
39
        static::checkForChoiceEnumTraitMisuses();
40
41
        return static::autodiscoveredReadables();
42
    }
43
44
    /**
45
     * @internal
46
     */
47
    private static function autodiscoveredReadables(): array
48
    {
49
        $enumType = static::class;
50
51
        ++self::$called;
52
        /**
53
         * The {@link AutoDiscoveredReadablesTrait::$called} property and the following is required
54
         * in order to preserve BC, especially for the FlaggedEnum.
55
         * It ensures overriding the {@link \Elao\Enum\ReadableEnum::values} method is taken into account
56
         * and prevents an infinite loop otherwise.
57
         * However, it's still a BC break in case of any parent::values call...
58
         *
59
         * @see \Elao\Enum\Tests\Unit\FlaggedEnumTest::testWithSpecifiedValues
60
         */
61
        $values = self::$called > 1 ? static::autodiscoveredValues() : static::values();
0 ignored issues
show
Bug introduced by
The method autodiscoveredValues() does not exist on Elao\Enum\AutoDiscoveredReadablesTrait. Did you maybe mean values()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
        self::$called = 0;
63
64
        if (!isset(self::$guessedReadables[$enumType])) {
65
            $constants = (new \ReflectionClass($enumType))->getConstants();
66
            foreach ($values as $value) {
67
                $constantName = array_search($value, $constants, true);
68
                self::$guessedReadables[$enumType][$value] = ucfirst(strtolower(str_replace('_', ' ', $constantName)));
69
            }
70
        }
71
72
        return self::$guessedReadables[$enumType];
73
    }
74
75
    /**
76
     * @internal
77
     */
78
    private static function checkForChoiceEnumTraitMisuses()
79
    {
80
        if (!is_a(static::class, ReadableEnumInterface::class, true)) {
81
            throw new LogicException(sprintf(
82
                'The "%s" trait is meant to be used by "%s" implementations, but "%s" does not implement it.',
83
                self::class,
84
                ReadableEnumInterface::class,
85
                static::class
86
            ));
87
        }
88
    }
89
}
90