Passed
Push — master ( 0d7b76...076a5e )
by Randy
02:12
created

ObjectFacade::hasProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dgame\Object;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use ReflectionProperty;
8
use function Dgame\Ensurance\enforce;
9
10
/**
11
 * Class ObjectFacade
12
 * @package Dgame\Object
13
 */
14
class ObjectFacade
15
{
16
    const DEBUG_LABEL = 'Dgame_Object_Facade';
17
18
    /**
19
     * @var object
20
     */
21
    private $object;
22
    /**
23
     * @var ReflectionClass
24
     */
25
    private $reflection;
26
27
    /**
28
     * ObjectFacade constructor.
29
     *
30
     * @param object $object
31
     */
32
    public function __construct($object)
33
    {
34
        enforce(is_object($object))->orThrow('That is not a valid object');
35
36
        $this->object = $object;
37
    }
38
39
    /**
40
     * @return object
41
     */
42
    final public function getObject()
43
    {
44
        return $this->object;
45
    }
46
47
    /**
48
     * @return ReflectionClass
49
     */
50
    final public function getReflection(): ReflectionClass
51
    {
52
        if ($this->reflection === null) {
53
            $this->reflection = new ReflectionClass($this->object);
54
        }
55
56
        return $this->reflection;
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param        $value
62
     *
63
     * @return bool
64
     */
65 View Code Duplication
    final public function setValueByProperty(string $name, $value): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $property = $this->getPropertyByName($name);
68
        if ($property !== null && Validator::new($this)->validateProperty($property)) {
69
            $property->setValue($this->object, $value);
70
71
            return true;
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * @param string $name
79
     * @param        $value
80
     *
81
     * @return bool
82
     */
83 View Code Duplication
    final public function setValueByMethod(string $name, $value): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $method = $this->getSetterMethod($name);
86
        if ($method !== null && Validator::new($this)->validateSetterMethod($method, $value)) {
87
            $method->invoke($this->object, $value);
88
89
            return true;
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * @param string $name
97
     *
98
     * @return mixed|null
99
     */
100 View Code Duplication
    final public function getValueByMethod(string $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $method = $this->getGetterMethod($name);
103
        if ($method !== null && Validator::new($this)->validateGetterMethod($method)) {
104
            return $method->invoke($this->object);
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * @param string $name
112
     *
113
     * @return mixed|null
114
     */
115 View Code Duplication
    final public function getValueByProperty(string $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $property = $this->getPropertyByName($name);
118
        if ($property !== null && Validator::new($this)->validateProperty($property)) {
119
            return $property->getValue($this->object);
120
        }
121
122
        return null;
123
    }
124
125
    /**
126
     * @param string $postfix
127
     *
128
     * @return null|ReflectionMethod
129
     */
130
    final public function getSetterMethod(string $postfix)
131
    {
132
        return $this->getMethod($postfix, ['set', 'append']);
133
    }
134
135
    /**
136
     * @param string $postfix
137
     *
138
     * @return null|ReflectionMethod
139
     */
140
    final public function getGetterMethod(string $postfix)
141
    {
142
        return $this->getMethod($postfix, ['get']);
143
    }
144
145
    /**
146
     * @param string $postfix
147
     * @param array  $prefixe
148
     *
149
     * @return null|ReflectionMethod
150
     */
151
    final public function getMethod(string $postfix, array $prefixe)
152
    {
153
        foreach ($prefixe as $prefix) {
154
            $method = $this->getMethodByName($prefix . ucfirst($postfix));
155
            if ($method !== null) {
156
                return $method;
157
            }
158
        }
159
160
        return null;
161
    }
162
163
    /**
164
     * @param string $name
165
     *
166
     * @return null|ReflectionMethod
167
     */
168
    final public function getMethodByName(string $name)
169
    {
170
        return $this->hasMethod($name) ? $this->getReflection()->getMethod($name) : null;
171
    }
172
173
    /**
174
     * @param string $name
175
     *
176
     * @return null|ReflectionProperty
177
     */
178
    final public function getPropertyByName(string $name)
179
    {
180
        return $this->hasProperty($name) ? $this->getReflection()->getProperty($name) : null;
181
    }
182
183
    /**
184
     * @param string $name
185
     *
186
     * @return bool
187
     */
188
    final public function hasProperty(string $name): bool
189
    {
190
        return $this->getReflection()->hasProperty($name);
191
    }
192
193
    /**
194
     * @param string $name
195
     *
196
     * @return bool
197
     */
198
    final public function hasMethod(string $name): bool
199
    {
200
        return $this->getReflection()->hasMethod($name);
201
    }
202
}
203