CoreObject   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
eloc 36
dl 0
loc 136
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setRetriever() 0 5 1
A unWrap() 0 12 2
A getUnwrapped() 0 14 5
A getObject() 0 5 2
A bindRetriever() 0 8 4
A hasRetriever() 0 3 1
A unwrapObject() 0 8 6
A setObject() 0 6 1
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
    /**
50
     * @param string|null $instance
51
     * @param string|null $exclude
52
     *
53
     * @return \Traversable|mixed
54
     */
55 8
    public function unWrap(?string $instance = null, ?string $exclude = null)
56
    {
57 8
        $object = $this->getUnwrapped($instance, $exclude);
58
59 8
        if ($this->unwrapSkipped) {
60 7
            return $object;
61
        }
62
63 4
        $this->setObject($object);
64 4
        $this->retriever = false;
65
66 4
        return $object;
67
    }
68
69
    /**
70
     * @param string|null $instance
71
     * @param string|null $exclude
72
     *
73
     * @return \Traversable|mixed
74
     */
75 8
    public function getUnwrapped(?string $instance = null, ?string $exclude = null)
76
    {
77 8
        $object = $this->getObject();
78
79 8
        $exclude = ($exclude && $object instanceof $exclude);
80
81 8
        $this->unwrapSkipped = false;
82 8
        if ($instance && $object instanceof $instance && !$exclude) {
83 7
            $this->unwrapSkipped = true;
84
85 7
            return $object;
86
        }
87
88 4
        return $this->unwrapObject($object);
89
    }
90
91
    /**
92
     * @param mixed $object
93
     *
94
     * @return mixed|ArrayIterator
95
     */
96 4
    public function unwrapObject($object)
97
    {
98 4
        $object = ($object instanceof \IteratorAggregate) ? $object->getIterator() : $object;
99 4
        $object = ($object instanceof \IteratorIterator) ? $object->getInnerIterator() : $object;
100 4
        $object = ($object instanceof \Traversable) ? iterator_to_array($object) : $object;
101 4
        $object = (method_exists($object, 'toArray')) ? $object->toArray() : $object;
102
103 4
        return (is_array($object)) ? new ArrayIterator($object) : $object;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109 8
    protected function getObject()
110
    {
111 8
        $this->bindRetriever();
112
113 8
        return ($this->hasRetriever()) ? $this->boundObject : $this->object;
114
    }
115
116
    /**
117
     * @param mixed $object
118
     *
119
     * @return CoreObject
120
     */
121 11
    public function setObject($object): self
122
    {
123 11
        $this->object      = $object;
124 11
        $this->boundObject = false;
125
126 11
        return $this;
127
    }
128
129
    /**
130
     * @param bool|null $rebind
131
     *
132
     * @return CoreObject
133
     */
134 8
    public function bindRetriever(?bool $rebind = false): self
135
    {
136 8
        if ($this->hasRetriever() && ($rebind || !$this->boundObject)) {
137 1
            $retriever         = $this->retriever;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->retriever can also be of type boolean. However, the property $retriever is declared as type callable|false. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
138 1
            $this->boundObject = $retriever($this->object);
139
        }
140
141 8
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 8
    public function hasRetriever(): bool
148
    {
149 8
        return (bool) $this->retriever;
150
    }
151
}
152