Completed
Pull Request — master (#49)
by Maxime
02:00
created

AutoDiscoveredValuesTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 23.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 19
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A values() 0 4 1
A choices() 0 13 2
B autodiscoveredValues() 5 25 6
A autodiscoveredReadables() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /** @internal */
21
    private static $guessedValues = [];
22
23
    /** @internal */
24
    private static $guessedReadables = [];
25
26
    /**
27
     * @see EnumInterface::values()
28
     *
29
     * @return int[]|string[]
30
     */
31
    public static function values(): array
32
    {
33
        return self::autodiscoveredValues();
34
    }
35
36
    /**
37
     * @see ChoiceEnumTrait::choices()
38
     */
39
    protected static function choices(): array
40
    {
41
        if (!in_array(ChoiceEnumTrait::class, class_uses(self::class, false), true)) {
42
            throw new LogicException(sprintf(
43
                'Method "%s" is only meant to be used when using the "%s" trait which is not used in "%s"',
44
                __METHOD__,
45
                ChoiceEnumTrait::class,
46
                static::class
47
            ));
48
        }
49
50
        return self::autodiscoveredReadables();
51
    }
52
53
    /**
54
     * @internal
55
     */
56
    private static function autodiscoveredValues(): array
57
    {
58
        $enumType = static::class;
59
60
        if (!isset(self::$guessedValues[$enumType])) {
61
            $r = new \ReflectionClass($enumType);
62
            $values = $r->getConstants();
63
64 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...
65
                $values = array_filter($values, function (string $k) use ($r) {
66
                    return $r->getReflectionConstant($k)->isPublic();
67
                }, ARRAY_FILTER_USE_KEY);
68
            }
69
70
            if (is_a($enumType, FlaggedEnum::class, true)) {
71
                $values = array_filter($values, function ($v) {
72
                    return is_int($v) && 0 === ($v & $v - 1) && $v > 0;
73
                });
74
            }
75
76
            self::$guessedValues[$enumType] = array_values($values);
77
        }
78
79
        return self::$guessedValues[$enumType];
80
    }
81
82
    /**
83
     * @internal
84
     */
85 View Code Duplication
    private static function autodiscoveredReadables(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
    {
87
        $enumType = static::class;
88
89
        if (!isset(self::$guessedReadables[$enumType])) {
90
            $constants = (new \ReflectionClass($enumType))->getConstants();
91
            foreach (self::autodiscoveredValues() as $value) {
92
                $constantName = array_search($value, $constants, true);
93
                self::$guessedReadables[$enumType][$value] = ucfirst(strtolower(str_replace('_', ' ', $constantName)));
94
            }
95
        }
96
97
        return self::$guessedReadables[$enumType];
98
    }
99
}
100