1
|
|
|
<?php |
2
|
|
|
namespace MetaHydrator\Reflection; |
3
|
|
|
|
4
|
|
|
use ArrayAccess; |
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
|
9
|
|
|
class Setter implements SetterInterface |
10
|
|
|
{ |
11
|
|
|
/** @var bool */ |
12
|
|
|
protected $throwOnFail; |
13
|
|
|
|
14
|
|
|
/** @var bool */ |
15
|
|
|
protected $ignoreProtected; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Setter constructor. |
19
|
|
|
* @param bool $throwOnFail |
20
|
|
|
* @param bool $ignoreProtected |
21
|
|
|
*/ |
22
|
|
|
public function __construct(bool $throwOnFail = true, bool $ignoreProtected = false) |
23
|
|
|
{ |
24
|
|
|
$this->throwOnFail = $throwOnFail; |
25
|
|
|
$this->ignoreProtected = $ignoreProtected; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $object |
30
|
|
|
* @param string $field |
31
|
|
|
* @param mixed $value |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function set(&$object, string $field, $value) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (is_array($object)) { |
36
|
|
|
$this->setInArray($object, $field, $value); |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (is_object($object)) { |
41
|
|
|
$this->setInObject($object, $field, $value); |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->throwOnFail) { |
46
|
|
|
throw new ReflectionException("cannot set field '$field' in non-object, non-array value"); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function setInArray(&$object, string $field, $value) |
51
|
|
|
{ |
52
|
|
|
$object[$field] = $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
protected function setInObject(&$object, string $field, $value) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$reflectionClass = new ReflectionClass($object); |
58
|
|
|
|
59
|
|
|
$setterMethod = $this->findSetterMethod($reflectionClass, $field); |
60
|
|
|
if ($setterMethod !== null) { |
61
|
|
|
$setterMethod->invoke($object, $value); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$property = $this->findProperty($reflectionClass, $field); |
66
|
|
|
if ($property !== null) { |
67
|
|
|
$property->setValue($object, $value); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($object instanceof ArrayAccess) { |
72
|
|
|
$object->offsetSet($field, $value); |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->throwOnFail) { |
77
|
|
|
throw new ReflectionException("cannot set field '$field' in object"); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ReflectionClass $reflectionClass |
83
|
|
|
* @param string $field |
84
|
|
|
* @return null|ReflectionMethod |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
protected function findSetterMethod(ReflectionClass $reflectionClass, string $field) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
foreach ($reflectionClass->getMethods($this->ignoreProtected ? ReflectionMethod::IS_PUBLIC|ReflectionMethod::IS_PROTECTED|ReflectionMethod::IS_PRIVATE : ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
89
|
|
|
if (!$reflectionMethod->isStatic() |
90
|
|
|
&& $reflectionMethod->getNumberOfRequiredParameters() == 1 |
91
|
|
|
&& strcasecmp($reflectionMethod->getName(), "set$field") == 0) { |
|
|
|
|
92
|
|
|
if (!$reflectionMethod->isPublic()) { |
93
|
|
|
$reflectionMethod->setAccessible(true); |
94
|
|
|
} |
95
|
|
|
return $reflectionMethod; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ReflectionClass $reflectionClass |
103
|
|
|
* @param string $field |
104
|
|
|
* @return null|\ReflectionProperty |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
protected function findProperty(ReflectionClass $reflectionClass, string $field) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if ($reflectionClass->hasProperty($field)) { |
109
|
|
|
$property = $reflectionClass->getProperty($field); |
110
|
|
|
if (!$property->isStatic()) { |
111
|
|
|
if ($property->isPublic()) { |
112
|
|
|
return $property; |
113
|
|
|
} else if ($this->ignoreProtected) { |
114
|
|
|
$property->setAccessible(true); |
115
|
|
|
return $property; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.