Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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) |
||
38 | |||
39 | /** |
||
40 | * @return object |
||
41 | */ |
||
42 | final public function getObject() |
||
46 | |||
47 | /** |
||
48 | * @return ReflectionClass |
||
49 | */ |
||
50 | final public function getReflection(): ReflectionClass |
||
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 |
|
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 |
|
94 | |||
95 | /** |
||
96 | * @param string $name |
||
97 | * |
||
98 | * @return mixed|null |
||
99 | */ |
||
100 | View Code Duplication | final public function getValueByMethod(string $name) |
|
109 | |||
110 | /** |
||
111 | * @param string $name |
||
112 | * |
||
113 | * @return mixed|null |
||
114 | */ |
||
115 | View Code Duplication | final public function getValueByProperty(string $name) |
|
124 | |||
125 | /** |
||
126 | * @param string $postfix |
||
127 | * |
||
128 | * @return null|ReflectionMethod |
||
129 | */ |
||
130 | final public function getSetterMethod(string $postfix) |
||
134 | |||
135 | /** |
||
136 | * @param string $postfix |
||
137 | * |
||
138 | * @return null|ReflectionMethod |
||
139 | */ |
||
140 | final public function getGetterMethod(string $postfix) |
||
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) |
||
162 | |||
163 | /** |
||
164 | * @param string $name |
||
165 | * |
||
166 | * @return null|ReflectionMethod |
||
167 | */ |
||
168 | final public function getMethodByName(string $name) |
||
172 | |||
173 | /** |
||
174 | * @param string $name |
||
175 | * |
||
176 | * @return null|ReflectionProperty |
||
177 | */ |
||
178 | final public function getPropertyByName(string $name) |
||
182 | |||
183 | /** |
||
184 | * @param string $name |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | final public function hasProperty(string $name): bool |
||
192 | |||
193 | /** |
||
194 | * @param string $name |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | final public function hasMethod(string $name): bool |
||
202 | } |
||
203 |
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.