1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mcneely\Core; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CoreObject. |
11
|
|
|
* |
12
|
|
|
* @package Mcneely\Core |
13
|
|
|
*/ |
14
|
|
|
class CoreObject |
15
|
|
|
{ |
16
|
|
|
/** @var mixed $object */ |
17
|
|
|
protected $object; |
18
|
|
|
|
19
|
|
|
/** @var callable|false $retriever */ |
20
|
|
|
protected $retriever = false; |
21
|
|
|
|
22
|
|
|
/** @var callable|false $boundObject */ |
23
|
|
|
protected $boundObject = false; |
24
|
|
|
|
25
|
|
|
protected $unwrapSkipped = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CoreObject constructor. |
29
|
|
|
* |
30
|
|
|
* @param mixed $object |
31
|
|
|
*/ |
32
|
11 |
|
public function __construct($object) |
33
|
|
|
{ |
34
|
11 |
|
$this->setObject($object); |
35
|
11 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param callable $retriever |
39
|
|
|
* |
40
|
|
|
* @return CoreObject |
41
|
|
|
*/ |
42
|
1 |
|
public function setRetriever(callable $retriever): self |
43
|
|
|
{ |
44
|
1 |
|
$this->retriever = $retriever; |
45
|
|
|
|
46
|
1 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
public function unWrap(?string $instance = null, ?string $exclude = null) |
50
|
|
|
{ |
51
|
8 |
|
$object = $this->getUnwrapped($instance, $exclude); |
52
|
|
|
|
53
|
8 |
|
if ($this->unwrapSkipped) { |
54
|
7 |
|
return $object; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
$this->setObject($object); |
58
|
4 |
|
$this->retriever = false; |
59
|
|
|
|
60
|
4 |
|
return $object; |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
public function getUnwrapped(?string $instance = null, ?string $exclude = null) |
64
|
|
|
{ |
65
|
8 |
|
$object = $this->getObject(); |
66
|
|
|
|
67
|
8 |
|
$object = ($object instanceof \IteratorAggregate) ? $object->getIterator() : $object; |
68
|
8 |
|
$object = ($object instanceof \IteratorIterator) ? $object->getInnerIterator() : $object; |
69
|
|
|
|
70
|
8 |
|
$exclude = ($exclude && $object instanceof $exclude); |
71
|
|
|
|
72
|
8 |
|
$this->unwrapSkipped = false; |
73
|
8 |
|
if ($instance && $object instanceof $instance && !$exclude) { |
74
|
7 |
|
$this->unwrapSkipped = true; |
75
|
|
|
|
76
|
7 |
|
return $object; |
77
|
|
|
} |
78
|
4 |
|
$object = ($object instanceof \Traversable) ? iterator_to_array($object) : $object; |
79
|
4 |
|
$object = (method_exists($object, 'toArray')) ? $object->toArray() : $object; |
80
|
4 |
|
$object = (is_array($object)) ? new ArrayIterator($object) : $object; |
81
|
|
|
|
82
|
4 |
|
return $object; |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
8 |
|
protected function getObject() |
90
|
|
|
{ |
91
|
8 |
|
$this->bindRetriever(); |
92
|
|
|
|
93
|
8 |
|
return ($this->hasRetriever()) ? $this->boundObject : $this->object; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $object |
98
|
|
|
* |
99
|
|
|
* @return CoreObject |
100
|
|
|
*/ |
101
|
11 |
|
public function setObject($object): self |
102
|
|
|
{ |
103
|
11 |
|
$this->object = $object; |
104
|
11 |
|
$this->boundObject = false; |
105
|
|
|
|
106
|
11 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param bool|null $rebind |
111
|
|
|
* |
112
|
|
|
* @return CoreObject |
113
|
|
|
*/ |
114
|
8 |
|
public function bindRetriever(?bool $rebind = false): self |
115
|
|
|
{ |
116
|
8 |
|
if ($this->hasRetriever() && ($rebind || !$this->boundObject)) { |
117
|
1 |
|
$retriever = $this->retriever; |
|
|
|
|
118
|
1 |
|
$this->boundObject = $retriever($this->object); |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
8 |
|
public function hasRetriever(): bool |
125
|
|
|
{ |
126
|
8 |
|
return (bool)$this->retriever; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.