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

CoreObject::hasRetriever()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
}