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 ( 778447...f7df6f )
by SignpostMarv
03:06
created

RecallDaftObjectFromData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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