GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( f06180...d17a44 )
by SignpostMarv
06:19
created

RecallDaftObjectFromData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
/**
12
* @template T as DefinesOwnIdPropertiesInterface&DaftObjectCreatedByArray
13
*
14
* @template-extends AbstractDaftObjectRepository<T>
15
*/
16
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository
17
{
18
    const BOOL_DEFAULT_ASSUMEDOESNOTEXIST = false;
19
20
    /**
21
    * @var DefinesOwnIdPropertiesInterface[]
22
    *
23
    * @psalm-var T[]
24
    */
25
    protected $memory = [];
26
27
    /**
28
    * @var mixed[][]
29
    */
30
    protected $data = [];
31
32
    /**
33
    * {@inheritdoc}
34
    *
35
    * @psalm-param T $object
36
    */
37 6
    public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void
38
    {
39 6
        TypeParanoia::ThrowIfNotDaftObjectIdPropertiesType(
40 6
            $object,
41 6
            1,
42 6
            static::class,
43 6
            __FUNCTION__,
44 6
            $this->type
45
        );
46
47 4
        $hashId = $object::DaftObjectIdHash($object);
48
49 4
        $this->memory[$hashId] = $object;
50
51 4
        $this->RememberDaftObjectData($object);
52 4
    }
53
54 4
    public function ForgetDaftObjectById($id) : void
55
    {
56 4
        $this->ForgetDaftObjectByHashId($this->type::DaftObjectIdValuesHash(
57 4
            TypeParanoia::ForceArgumentAsArray($id)
58
        ));
59 4
    }
60
61 4
    public function RemoveDaftObjectById($id) : void
62
    {
63 4
        $this->RemoveDaftObjectByHashId($this->type::DaftObjectIdValuesHash(
64 4
            TypeParanoia::ForceArgumentAsArray($id)
65
        ));
66 4
    }
67
68
    /**
69
    * {@inheritdoc}
70
    *
71
    * @psalm-return T|null
72
    */
73 4
    public function RecallDaftObject($id) : ? DefinesOwnIdPropertiesInterface
74
    {
75 4
        $hashId = $this->type::DaftObjectIdValuesHash(
76 4
            TypeParanoia::ForceArgumentAsArray($id)
77
        );
78
79 4
        return $this->memory[$hashId] ?? $this->RecallDaftObjectFromData($id);
80
    }
81
82
    /**
83
    * {@inheritdoc}
84
    *
85
    * @psalm-param T $object
86
    */
87 4
    public function RememberDaftObjectData(
88
        DefinesOwnIdPropertiesInterface $object,
89
        bool $assumeDoesNotExist = self::BOOL_DEFAULT_ASSUMEDOESNOTEXIST
90
    ) : void {
91 4
        $hashId = $object::DaftObjectIdHash($object);
92
93 4
        $this->data[$hashId] = [];
94
95 4
        foreach ($object::DaftObjectPublicGetters() as $property) {
96 4
            $this->data[$hashId][$property] = $object->__get($property);
97
        }
98 4
    }
99
100
    /**
101
    * Recalls the corresponding daft object instance from cached data.
102
    *
103
    * @param scalar|(scalar|array|object|null)[] $id
104
    *
105
    * @psalm-return T|null
106
    */
107 8
    protected function RecallDaftObjectFromData($id) : ? DefinesOwnIdPropertiesInterface
108
    {
109 8
        $hashId = $this->type::DaftObjectIdValuesHash(
110 8
            TypeParanoia::ForceArgumentAsArray($id)
111
        );
112
113 8
        if (isset($this->data[$hashId])) {
114 4
            $type = $this->type;
115
116 4
            $out = new $type($this->data[$hashId]);
117
118 4
            return $out;
119
        }
120
121 8
        return null;
122
    }
123
124 4
    private function ForgetDaftObjectByHashId(string $hashId) : void
125
    {
126 4
        if (true === isset($this->memory[$hashId])) {
127 4
            unset($this->memory[$hashId]);
128
        }
129 4
    }
130
131 4
    private function RemoveDaftObjectByHashId(string $hashId) : void
132
    {
133 4
        $this->ForgetDaftObjectByHashId($hashId);
134
135 4
        if (true === isset($this->data[$hashId])) {
136 4
            unset($this->data[$hashId]);
137
        }
138 4
    }
139
}
140