|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var DefinesOwnIdPropertiesInterface[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $memory = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* mixed[][]. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
public function RememberDaftObject( |
|
24
|
|
|
DefinesOwnIdPropertiesInterface $object |
|
25
|
|
|
) : void { |
|
26
|
|
|
if (is_a($object, $this->type, true) === false) { |
|
27
|
|
|
throw new DaftObjectRepositoryTypeException( |
|
28
|
|
|
'Argument 1 passed to ' . |
|
|
|
|
|
|
29
|
|
|
static::class . |
|
30
|
|
|
'::' . |
|
31
|
|
|
__FUNCTION__ . |
|
32
|
|
|
'() must be an instance of ' . |
|
33
|
|
|
$this->type . |
|
34
|
|
|
', ' . |
|
35
|
|
|
get_class($object) . |
|
36
|
|
|
' given.' |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$hashId = $object::DaftObjectIdHash($object); |
|
41
|
|
|
|
|
42
|
|
|
$this->memory[$hashId] = $object; |
|
43
|
|
|
|
|
44
|
|
|
if (isset($this->data[$hashId]) === false) { |
|
45
|
|
|
$this->data[$hashId] = []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
foreach ($object::DaftObjectProperties() as $property) { |
|
49
|
|
|
$getter = 'Get' . ucfirst($property); |
|
50
|
|
|
|
|
51
|
|
|
if ( |
|
52
|
|
|
method_exists($object, $getter) === true && |
|
53
|
|
|
isset($object->$property) |
|
54
|
|
|
) { |
|
55
|
|
|
$this->data[$hashId][$property] = $object->$getter(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function ForgetDaftObjectById($id) : void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->ForgetDaftObjectByHashId($this->ObjectHashId($id)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function RemoveDaftObjectById($id) : void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->RemoveDaftObjectByHashId($this->ObjectHashId($id)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function RecallDaftObject($id) : ? DaftObject |
|
71
|
|
|
{ |
|
72
|
|
|
$hashId = $this->ObjectHashId($id); |
|
73
|
|
|
|
|
74
|
|
|
if (isset($this->memory[$hashId]) === false) { |
|
75
|
|
|
if (isset($this->data[$hashId]) === true) { |
|
76
|
|
|
$type = $this->type; |
|
77
|
|
|
|
|
78
|
|
|
return new $type($this->data[$hashId]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->memory[$hashId]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function ObjectHashId($id) : string |
|
88
|
|
|
{ |
|
89
|
|
|
$id = is_array($id) ? $id : [$id]; |
|
90
|
|
|
|
|
91
|
|
|
$type = $this->type; |
|
92
|
|
|
|
|
93
|
|
|
return $type::DaftObjectIdValuesHash($id); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function ForgetDaftObjectByHashId(string $hashId) : void |
|
97
|
|
|
{ |
|
98
|
|
|
if (isset($this->memory[$hashId]) === true) { |
|
99
|
|
|
unset($this->memory[$hashId]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function RemoveDaftObjectByHashId(string $hashId) : void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->ForgetDaftObjectByHashId($hashId); |
|
106
|
|
|
|
|
107
|
|
|
if (isset($this->data[$hashId]) === true) { |
|
108
|
|
|
unset($this->data[$hashId]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.