LazyProperty   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 0
dl 0
loc 118
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getValueInitializer() 0 4 1
A getName() 0 4 1
A hasTriggers() 0 4 1
A isTriggeredBy() 0 10 3
A setInitializationCallback() 0 4 1
A hasInitializationCallback() 0 4 1
A getInitializationCallback() 0 4 1
1
<?php
2
3
namespace Isolate\LazyObjects\Proxy;
4
5
use Isolate\LazyObjects\Exception\InvalidArgumentException;
6
use Isolate\LazyObjects\Proxy\LazyProperty\InitializationCallback;
7
use Isolate\LazyObjects\Proxy\Property\Name;
8
use Isolate\LazyObjects\Proxy\Property\ValueInitializer;
9
10
/**
11
 * @api
12
 */
13
class LazyProperty
14
{
15
    /**
16
     * @var Name
17
     */
18
    private $name;
19
20
    /**
21
     * @var ValueInitializer
22
     */
23
    private $valueInitializer;
24
25
    /**
26
     * @var array|Method[] $triggers
27
     */
28
    private $triggers;
29
30
    /**
31
     * @var \Closure
32
     */
33
    private $initializationCallback;
34
35
    /**
36
     * @param Name $name
37
     * @param ValueInitializer $valueInitializer
38
     * @param array|Method[] $triggers
39
     * @throws InvalidArgumentException
40
     */
41
    public function __construct(Name $name, ValueInitializer $valueInitializer, $triggers = [])
42
    {
43
        foreach ($triggers as $trigger) {
44
            if (!$trigger instanceof Method) {
45
                throw new InvalidArgumentException("Each trigger must be an instance of Isolate\\LazyObjects\\Proxy\\Method");
46
            }
47
        }
48
49
        $this->name = $name;
50
        $this->valueInitializer = $valueInitializer;
51
        $this->triggers = $triggers;
52
    }
53
54
    /**
55
     * @return ValueInitializer
56
     * 
57
     * @api
58
     */
59
    public function getValueInitializer()
60
    {
61
        return $this->valueInitializer;
62
    }
63
64
    /**
65
     * @return string
66
     * 
67
     * @api
68
     */
69
    public function getName()
70
    {
71
        return $this->name;
72
    }
73
74
    /**
75
     * @return bool
76
     * 
77
     * @api
78
     */
79
    public function hasTriggers()
80
    {
81
        return (boolean) count($this->triggers);
82
    }
83
84
    /**
85
     * @param $methodName
86
     * @return bool
87
     * 
88
     * @api
89
     */
90
    public function isTriggeredBy($methodName)
91
    {
92
        foreach ($this->triggers as $trigger) {
93
            if ($trigger->isEqualTo($methodName)) {
94
                return true;
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * @param InitializationCallback $initializationCallback
103
     * 
104
     * @api
105
     */
106
    public function setInitializationCallback(InitializationCallback $initializationCallback)
107
    {
108
        $this->initializationCallback = $initializationCallback;
0 ignored issues
show
Documentation Bug introduced by
It seems like $initializationCallback of type object<Isolate\LazyObjec...InitializationCallback> is incompatible with the declared type object<Closure> of property $initializationCallback.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
109
    }
110
111
    /**
112
     * @return bool
113
     * 
114
     * @api
115
     */
116
    public function hasInitializationCallback()
117
    {
118
        return !is_null($this->initializationCallback);
119
    }
120
121
    /**
122
     * @return InitializationCallback
123
     * 
124
     * @api
125
     */
126
    public function getInitializationCallback()
127
    {
128
        return $this->initializationCallback;
129
    }
130
}
131