Completed
Push — master ( cc8ae8...627a43 )
by Zbigniew
02:21
created

EnumAbstract::assertIsValidValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the WrikePhpSdk package.
4
 *
5
 * (c) Zbigniew Ślązak
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zibios\WrikePhpSdk\Enums;
12
13
/**
14
 * Enum Abstract
15
 */
16
abstract class EnumAbstract
0 ignored issues
show
Coding Style introduced by
EnumAbstract does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
{
18
    /**
19
     * @var array
20
     */
21
    protected static $cache = [];
22
23
    /**
24
     * @return array
25
     */
26 163
    public static function toArray()
27
    {
28 163
        $class = get_called_class();
29 163
        if (!array_key_exists($class, static::$cache)) {
30 1
            $reflection = new \ReflectionClass($class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
31 1
            static::$cache[$class] = $reflection->getConstants();
32 1
        }
33
34 163
        return static::$cache[$class];
35
    }
36
37
    /**
38
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
39
     */
40 16
    public static function getKeys()
41
    {
42 16
        return array_keys(static::toArray());
43
    }
44
45
    /**
46
     * @param mixed $value
47
     *
48
     * @throws \InvalidArgumentException
49
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51 8
    public static function getKey($value)
52
    {
53 8
        self::assertIsValidValue($value);
54
55 8
        return array_search($value, static::toArray(), true);
56
    }
57
58
    /**
59
     * @param string $key
60
     *
61
     * @return bool
62
     */
63 40
    public static function isValidKey($key)
64
    {
65 40
        return array_key_exists($key, self::toArray());
66
    }
67
68
    /**
69
     * @param string $key
70
     *
71
     * @throws \InvalidArgumentException
72
     */
73 40
    public static function assertIsValidKey($key)
74
    {
75 40
        if (self::isValidKey($key) === false) {
76 24
            throw new \InvalidArgumentException('Wrong key.');
77
        }
78 16
    }
79
80
    /**
81
     * @param string $key
82
     *
83
     * @throws \InvalidArgumentException
84
     * @return mixed
85
     */
86 8
    public static function getValue($key)
87
    {
88 8
        self::assertIsValidKey($key);
89
90 8
        return static::toArray()[$key];
91
    }
92
93
    /**
94
     * @return array
95
     */
96 16
    public static function getValues()
97
    {
98 16
        return array_values(self::toArray());
99
    }
100
101
    /**
102
     * @param mixed $value
103
     *
104
     * @return bool
105
     */
106 99
    public static function isValidValue($value)
107
    {
108 99
        return in_array($value, static::toArray(), true);
109
    }
110
111
    /**
112
     * @throws \InvalidArgumentException
113
     *
114
     * @param mixed $value
115
     */
116 99
    public static function assertIsValidValue($value)
117
    {
118 99
        if (self::isValidValue($value) === false) {
119 24
            throw new \InvalidArgumentException('Wrong value.');
120
        }
121 75
    }
122
}
123