Completed
Branch master (b122b1)
by Paul
10:08 queued 07:52
created

CoreObject::unwrapObject()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 5
nc 32
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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
     * @return mixed|ArrayIterator
94
     */
95 4
    public function unwrapObject($object)
96
    {
97 4
        $object = ($object instanceof \IteratorAggregate) ? $object->getIterator() : $object;
98 4
        $object = ($object instanceof \IteratorIterator) ? $object->getInnerIterator() : $object;
99 4
        $object = ($object instanceof \Traversable) ? iterator_to_array($object) : $object;
100 4
        $object = (method_exists($object, 'toArray')) ? $object->toArray() : $object;
101
102 4
        return (is_array($object)) ? new ArrayIterator($object) : $object;
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108 8
    protected function getObject()
109
    {
110 8
        $this->bindRetriever();
111
112 8
        return ($this->hasRetriever()) ? $this->boundObject : $this->object;
113
    }
114
115
    /**
116
     * @param mixed $object
117
     *
118
     * @return CoreObject
119
     */
120 11
    public function setObject($object): self
121
    {
122 11
        $this->object      = $object;
123 11
        $this->boundObject = false;
124
125 11
        return $this;
126
    }
127
128
    /**
129
     * @param bool|null $rebind
130
     *
131
     * @return CoreObject
132
     */
133 8
    public function bindRetriever(?bool $rebind = false): self
134
    {
135 8
        if ($this->hasRetriever() && ($rebind || !$this->boundObject)) {
136 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...
137 1
            $this->boundObject = $retriever($this->object);
138
        }
139
140 8
        return $this;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146 8
    public function hasRetriever(): bool
147
    {
148 8
        return (bool) $this->retriever;
149
    }
150
}
151