Completed
Push — master ( d1ccb9...b41f5f )
by Paul
02:53
created

CoreObject   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 76
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

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