SplEnumAccessorsTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 91
ccs 0
cts 26
cp 0
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __isset() 0 14 3
A __set() 0 8 3
A __unset() 0 8 2
A __get() 0 17 3
1
<?php
2
3
/**
4
 * Part of SplTypes package.
5
 *
6
 * (c) Adrien Loyant <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ducks\Component\SplTypes;
13
14
/**
15
 * Trait used for magic accessor on SplEnum class
16
 *
17
 * @template T
18
 *
19
 * @phpstan-require-extends SplEnum
20
 *
21
 * @psalm-api
22
 */
23
trait SplEnumAccessorsTrait
24
{
25
    /**
26
     * Return the case-sensitive name of the case class itself.
27
     *
28
     * @param string $name
29
     *
30
     * @return mixed
31
     */
32
    final public function __get(string $name)
33
    {
34
        switch ($name) {
35
            case 'name':
36
                $result = \array_search($this->__default, $this->getConstList());
0 ignored issues
show
Bug introduced by
It seems like getConstList() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
                $result = \array_search($this->__default, $this->/** @scrutinizer ignore-call */ getConstList());
Loading history...
Bug Best Practice introduced by
The property __default does not exist on Ducks\Component\SplTypes\SplEnumAccessorsTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
37
                break;
38
39
            case 'value':
40
                $result = $this->__default;
41
                break;
42
43
            default:
44
                \trigger_error('Undefined property: ' . static::class . '::$' . $name, E_USER_WARNING);
45
                break;
46
        }
47
48
        return $result ?? null;
49
    }
50
51
    /**
52
     * Writing data to inaccessible (protected or private) or non-existing properties.
53
     *
54
     * @param string $name
55
     * @param mixed $value
56
     *
57
     * @return void
58
     *
59
     * @throws \Error
60
     *
61
     * @phpstan-ignore-next-line
62
     *
63
     * @psalm-suppress MissingParamType
64
     * @psalm-suppress UnusedParam
65
     */
66
    final public function __set(string $name, $value): void
67
    {
68
        // Fast return
69
        if ('name' === $name || 'value' === $name) {
70
            throw new \Error('Cannot modify readonly property ' . static::class . '::$' . $name);
71
        }
72
73
        throw new \Error('Cannot create dynamic property ' . static::class . '::$' . $name);
74
    }
75
76
    /**
77
     * Triggered by calling isset() or empty() on inaccessible or non-existing properties.
78
     *
79
     * @param string $name
80
     *
81
     * @return bool
82
     */
83
    final public function __isset(string $name): bool
84
    {
85
        switch ($name) {
86
            case 'name':
87
            case 'value':
88
                $result = true;
89
                break;
90
91
            default:
92
                $result = isset($this->$name);
93
                break;
94
        }
95
96
        return $result;
97
    }
98
99
    /**
100
     * Invoked when unset() is used on inaccessible or non-existing properties.
101
     *
102
     * @param string $name
103
     *
104
     * @return void
105
     */
106
    final public function __unset(string $name): void
107
    {
108
        // Fast return
109
        if (!\in_array($name, ['name', 'value'])) {
110
            return;
111
        }
112
113
        throw new \Error('Cannot unset readonly property ' . static::class . '::$' . $name);
114
    }
115
}
116