1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DependencyInjection\Internal; |
4
|
|
|
|
5
|
|
|
class ReflectionPropertyFactory implements ReflectionFactoryInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \ReflectionProperty |
9
|
|
|
*/ |
10
|
|
|
private $reflectionProperty; |
11
|
|
|
|
12
|
|
View Code Duplication |
public function __construct($class, $name) |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
if (!is_string($class) && !class_exists($class)) { |
15
|
|
|
throw Exception\ReflectionExceptionFactory::invalidArgument( |
16
|
|
|
sprintf("Parameter 1 of %s must be either string and valid class name.", __METHOD__) |
17
|
|
|
); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$this->reflectionProperty = new \ReflectionProperty($class, $name); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function create($class, $name) |
24
|
|
|
{ |
25
|
|
|
return new static($class, $name); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function export($class, $name, $return = false) |
29
|
|
|
{ |
30
|
|
|
return \ReflectionProperty::export($class, $name, $return); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getDeclaringClass() |
34
|
|
|
{ |
35
|
|
|
$decl = $this->reflectionProperty->getDeclaringClass(); |
36
|
|
|
|
37
|
|
|
return ($decl instanceof \ReflectionClass ? $decl : null); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getDocComment() |
41
|
|
|
{ |
42
|
|
|
return $this->reflectionProperty->getDocComment(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getModifiers() |
46
|
|
|
{ |
47
|
|
|
return $this->reflectionProperty->getModifiers(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getName() |
51
|
|
|
{ |
52
|
|
|
return $this->reflectionProperty->getName(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getValue($object = null) |
56
|
|
|
{ |
57
|
|
|
if (is_null($object) && !$this->isStatic()) { |
58
|
|
|
throw Exception\ReflectionExceptionFactory::logic( |
59
|
|
|
sprintf( |
60
|
|
|
"Parameter 1 of %s must be a string because the current property being analyze is not static.", |
61
|
|
|
__METHOD__ |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$object = (is_string($object) && class_exists($object) |
67
|
|
|
? ReflectionClassFactory::create($object)->newInstance() |
68
|
|
|
: (is_object($object) |
69
|
|
|
? $object |
70
|
|
|
: null)); |
71
|
|
|
|
72
|
|
|
return ($this->isStatic() ? $this->reflectionProperty->getValue() |
73
|
|
|
: (is_null($object) ? null : $this->reflectionProperty->getValue($object))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isDefault() |
77
|
|
|
{ |
78
|
|
|
return $this->reflectionProperty->isDefault(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function isPrivate() |
82
|
|
|
{ |
83
|
|
|
return $this->reflectionProperty->isPrivate(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isProtected() |
87
|
|
|
{ |
88
|
|
|
return $this->reflectionProperty->isProtected(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isPublic() |
92
|
|
|
{ |
93
|
|
|
return $this->reflectionProperty->isPublic(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isStatic() |
97
|
|
|
{ |
98
|
|
|
return $this->reflectionProperty->isStatic(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setAccessible($accessible = false) |
102
|
|
|
{ |
103
|
|
|
$this->reflectionProperty->setAccessible($accessible); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function setValue($object, $value) |
107
|
|
|
{ |
108
|
|
|
if (is_null($object) && !$this->isStatic()) { |
109
|
|
|
throw Exception\ReflectionExceptionFactory::logic( |
110
|
|
|
sprintf( |
111
|
|
|
"Parameter 1 of %s must be a string because the current property being analyze is not static.", |
112
|
|
|
__METHOD__ |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$object = (is_string($object) && class_exists($object) |
118
|
|
|
? ReflectionClassFactory::create($object)->newInstance() |
119
|
|
|
: (is_object($object) |
120
|
|
|
? $object |
121
|
|
|
: null)); |
122
|
|
|
|
123
|
|
|
if ($this->isStatic()) { |
124
|
|
|
$this->reflectionProperty->setValue($value); |
125
|
|
|
} else { |
126
|
|
|
if (!is_null($object)) { |
127
|
|
|
$this->reflectionProperty->setValue($object, $value); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function __toString() |
133
|
|
|
{ |
134
|
|
|
return (string)$this->reflectionProperty; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getReflector() |
138
|
|
|
{ |
139
|
|
|
return $this->reflectionProperty; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.