Completed
Push — master ( f4c713...dc87ae )
by Paul
02:34
created

CoreObject::unWrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
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
    /**
26
     * CoreObject constructor.
27
     *
28
     * @param mixed $object
29
     */
30 10
    public function __construct($object)
31
    {
32 10
        $this->setObject($object);
33 10
    }
34
35
    /**
36
     * @param callable $retriever
37
     *
38
     * @return CoreObject
39
     */
40 1
    public function setRetriever(callable $retriever): self
41
    {
42 1
        $this->retriever = $retriever;
43
44 1
        return $this;
45
    }
46
47 8
    public function unWrap(?string $instance = null, ?string $exclude = null)
48
    {
49 8
        $unWrap = $this->internalUnWrap($instance, $exclude);
50 8
        $object = $unWrap['object'];
51
52 8
        if ($unWrap['skipped']) {
53 7
            return $object;
54
        }
55
56 4
        $this->setObject($object);
57 4
        $this->retriever = false;
58
59 4
        return $object;
60
    }
61
62 8
    protected function internalUnWrap(?string $instance = null, ?string $exclude = null): array
63
    {
64 8
        $object = $this->getObject();
65
66 8
        $object = ($object instanceof \IteratorAggregate) ? $object->getIterator() : $object;
67 8
        $object = ($object instanceof \IteratorIterator) ? $object->getInnerIterator() : $object;
68
69 8
        $skip    = false;
70 8
        $exclude = ($exclude && $object instanceof $exclude);
71 8
        if ($instance && $object instanceof $instance && !$exclude) {
72 7
            $skip = true;
73
        } else {
74 4
            $object = ($object instanceof \Traversable) ? iterator_to_array($object) : $object;
75 4
            $object = (method_exists($object, 'toArray')) ? $object->toArray() : $object;
76 4
            $object = (is_array($object)) ? new ArrayIterator($object) : $object;
77
        }
78
79
        return [
80 8
            'skipped' => $skip,
81 8
            'object'  => $object,
82
        ];
83
84
    }
85
86
    /**
87
     *
88
     * @return mixed
89
     */
90 8
    protected function getObject()
91
    {
92 8
        $this->bindRetriever();
93
94 8
        return ($this->hasRetriever()) ? $this->boundObject : $this->object;
95
    }
96
97
    /**
98
     * @param mixed $object
99
     *
100
     * @return CoreObject
101
     */
102 10
    public function setObject($object): self
103
    {
104 10
        $this->object      = $object;
105 10
        $this->boundObject = false;
106
107 10
        return $this;
108
    }
109
110
    /**
111
     * @param bool|null $rebind
112
     *
113
     * @return CoreObject
114
     */
115 8
    public function bindRetriever(?bool $rebind = false): self
116
    {
117 8
        if ($this->hasRetriever() && ($rebind || !$this->boundObject)) {
118 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...
119 1
            $this->boundObject = $retriever($this->object);
120
        }
121
122 8
        return $this;
123
    }
124
125 8
    public function hasRetriever(): bool
126
    {
127 8
        return (bool)$this->retriever;
128
    }
129
}
130