Passed
Push — master ( c81098...521ad8 )
by Paul
02:34
created

CoreObject::getUnwrapped()   B

Complexity

Conditions 10
Paths 72

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 12
nc 72
nop 2
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
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...
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