SplEnumUnitTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 73
ccs 7
cts 11
cp 0.6364
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __debugInfo() 0 6 1
A __set_state() 0 10 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 mixed[]
30
     *
31
     * @phpstan-return array<string,mixed>
32
     */
33 1
    public function __serialize(): array
34
    {
35 1
        $result = parent::__serialize();
36 1
        $result['name'] = $this->name;
37
38 1
        return $result;
39
    }
40
41
    /**
42
     * Unserialize object.
43
     *
44
     * @param mixed[] $data
45
     *
46
     * @return void
47
     *
48
     * @phpstan-param array<string,mixed> $data
49
     * @phpstan-return void
50
     */
51 1
    public function __unserialize(array $data): void
52
    {
53 1
        parent::__unserialize($data);
54
55 1
        $this->name = (string) $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...
56
    }
57
58
    /**
59
     * Instanciate an exported object.
60
     *
61
     * @param mixed[] $properties
62
     *
63
     * @return static
64
     *
65
     * @phpstan-param array<string,mixed> $properties
66
     * @phpstan-return static
67
     *
68
     * @psalm-suppress UnsafeInstantiation
69
     */
70
    public static function __set_state(array $properties): SplUnitEnum
71
    {
72
        /**
73
         * @var static $object
74
         */
75
        // @phpstan-ignore-next-line
76
        $object = /** @scrutinizer ignore-call */ new static();
77
        $object->name = (string) $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...
78
79
        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...
80
    }
81
82
    /**
83
     * Dumping object.
84
     *
85
     * @return string[]
86
     *
87
     * @phpstan-return array<string,mixed>
88
     *
89
     * @codeCoverageIgnore
90
     */
91
    public function __debugInfo(): array
92
    {
93
        $result = parent::__debugInfo();
94
        $result['name'] = $this->name;
95
96
        return $result;
97
    }
98
}
99