Passed
Push — 7.x ( 2b11c5...0a025f )
by Adrien
08:48
created

SplEnumBackedTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __serialize() 0 5 1
A __debugInfo() 0 5 1
A __set_state() 0 8 1
A __unserialize() 0 5 1
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
declare(strict_types=1);
13
14
namespace Ducks\Component\SplTypes;
15
16
/**
17
 * SplEnumBackedTrait expose magic method in order to use an SplEnum class which implements SplBackedEnum.
18
 *
19
 * @template T
20
 *
21
 * @phpstan-require-extends SplEnum
22
 * @phpstan-require-implements SplBackedEnum
23
 *
24
 * @psalm-api
25
 */
26
trait SplEnumBackedTrait
27
{
28
    use SplEnumUnitTrait {
29
        SplEnumUnitTrait::__serialize as __unitSerialize;
30
        SplEnumUnitTrait::__unserialize as __unitUnserialize;
31
        SplEnumUnitTrait::__set_state as __unitSetState;
32
    }
33
34
    /**
35
     * Serialize object.
36
     *
37
     * @return array<string,mixed>
38
     */
39
    public function __serialize(): array
40
    {
41
        $result = $this->__unitSerialize();
42
43
        return $result;
44
    }
45
46
    /**
47
     * Unserialize object.
48
     *
49
     * @param array<string,mixed> $data
50
     *
51
     * @return void
52
     */
53
    public function __unserialize(array $data): void
54
    {
55
        $this->__unitUnserialize($data);
56
57
        $this->value = &$this->__default;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
    }
59
60
    /**
61
     * Instanciate an exported object.
62
     *
63
     * @param array<string,mixed> $properties
64
     *
65
     * @return static
66
     *
67
     * @psalm-suppress UnsafeInstantiation
68
     */
69
    #[\ReturnTypeWillChange]
70
    public static function __set_state(array $properties): SplBackedEnum
71
    {
72
        /** @var static $object */
73
        $object = static::__unitSetState($properties);
74
        $object->value = $properties['value'];
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
76
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object returns the type Ducks\Component\SplTypes\SplEnumBackedTrait which is incompatible with the type-hinted return Ducks\Component\SplTypes\SplBackedEnum.
Loading history...
77
    }
78
79
    /**
80
     * Dumping object.
81
     *
82
     * @return array<string,string>
83
     *
84
     * @codeCoverageIgnore
85
     */
86
    public function __debugInfo(): array
87
    {
88
        return [
89
            'name' => $this->name,
90
            'value' => $this->value,
91
        ];
92
    }
93
}
94