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

SplEnumUnitTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

4 Methods

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