Completed
Push — master ( 1bc38b...e5e579 )
by Paul
05:30
created

CoreObject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 96
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isInstanceOf() 0 3 1
A getObject() 0 8 5
A isArray() 0 3 1
A __construct() 0 3 1
A setRetriever() 0 5 1
A hasRetriever() 0 3 1
A hasMethod() 0 3 1
A setObject() 0 6 1
A getClass() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mcneely\Core;
5
6
/**
7
 * Class CoreObject.
8
 *
9
 * @package Mcneely\Core
10
 */
11
class CoreObject
12
{
13
    /** @var mixed $object */
14
    protected $object;
15
16
    /** @var callable|false $retriever */
17
    protected $retriever = false;
18
19
    /** @var callable|false $boundObject */
20
    protected $boundObject = false;
21
22
    /**
23
     * CoreObject constructor.
24
     *
25
     * @param mixed $object
26
     */
27 13
    public function __construct($object)
28
    {
29 13
        $this->setObject($object);
30 13
    }
31
32
    /**
33
     * @param callable $retriever
34
     *
35
     * @return CoreObject
36
     */
37 1
    public function setRetriever(callable $retriever): self
38
    {
39 1
        $this->retriever = $retriever;
40
41 1
        return $this;
42
    }
43
44 8
    public function hasRetriever(): bool
45
    {
46 8
        return (bool) $this->retriever;
47
    }
48
49
    /**
50
     * @param bool $useRetriever
51
     *
52
     * @return mixed
53
     */
54 8
    public function getObject($useRetriever = true)
55
    {
56 8
        if (!$this->boundObject && $this->hasRetriever()) {
57 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...
58 1
            $this->boundObject =  $retriever($this->object);
59
        }
60
61 8
        return ($this->hasRetriever() && $useRetriever) ? $this->boundObject : $this->object;
62
    }
63
64
    /**
65
     * @return string
66
     */
67 1
    public function getClass(): string
68
    {
69 1
        return get_class($this->object);
70
    }
71
72
    /**
73
     * @param string $instance
74
     *
75
     * @return bool
76
     */
77 3
    public function isInstanceOf($instance): bool
78
    {
79 3
        return $this->object instanceof $instance;
80
    }
81
82
    /**
83
     * @param string $method
84
     *
85
     * @return bool
86
     */
87 4
    public function hasMethod($method): bool
88
    {
89 4
        return method_exists($this->object, $method);
90
    }
91
92 1
    public function isArray(): bool
93
    {
94 1
        return is_array($this->object);
95
    }
96
97
    /**
98
     * @param mixed $object
99
     * @return CoreObject
100
     */
101 13
    public function setObject($object): self
102
    {
103 13
        $this->object = $object;
104 13
        $this->boundObject = false;
105
106 13
        return $this;
107
    }
108
109
110
}
111