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:
Complex classes like Give_Translations often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Translations, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Give_Translations { |
||
|
|||
13 | /** |
||
14 | * Instance. |
||
15 | * |
||
16 | * @since 2.0 |
||
17 | * @access private |
||
18 | * @var |
||
19 | */ |
||
20 | private static $instance; |
||
21 | |||
22 | /** |
||
23 | * Text config. |
||
24 | * |
||
25 | * @since 2.0 |
||
26 | * @access private |
||
27 | * @var |
||
28 | */ |
||
29 | private static $text_configs = array(); |
||
30 | |||
31 | /** |
||
32 | * Translated texts. |
||
33 | * |
||
34 | * @since 2.0 |
||
35 | * @access private |
||
36 | * @var |
||
37 | */ |
||
38 | private static $text_translations = array(); |
||
39 | |||
40 | /** |
||
41 | * Singleton pattern. |
||
42 | * |
||
43 | * @since 2.0 |
||
44 | * @access private |
||
45 | */ |
||
46 | private function __construct() { |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Get instance. |
||
52 | * |
||
53 | * @since 2.0 |
||
54 | * @access public |
||
55 | * @return static |
||
56 | */ |
||
57 | public static function get_instance() { |
||
64 | |||
65 | /** |
||
66 | * Setup |
||
67 | * |
||
68 | * @since 2.0 |
||
69 | * @access public |
||
70 | */ |
||
71 | public function setup() { |
||
74 | |||
75 | /** |
||
76 | * Setup hooks |
||
77 | * |
||
78 | * @since 2.0 |
||
79 | * @access public |
||
80 | */ |
||
81 | public function setup_hooks() { |
||
84 | |||
85 | /** |
||
86 | * Load translated texts. |
||
87 | * |
||
88 | * @since 2.0 |
||
89 | * @access public |
||
90 | */ |
||
91 | public function load_translated_texts() { |
||
102 | |||
103 | /** |
||
104 | * Add text by group ( if any ) |
||
105 | * |
||
106 | * @since 2.0 |
||
107 | * @access public |
||
108 | * |
||
109 | * @param array $args |
||
110 | * |
||
111 | * @return bool|WP_Error false on success otherwise WP_Error object |
||
112 | */ |
||
113 | public static function add_text( $args = array() ) { |
||
177 | |||
178 | /** |
||
179 | * Add label by group ( if any ) |
||
180 | * |
||
181 | * @since 2.0 |
||
182 | * @access public |
||
183 | * |
||
184 | * @param array $args |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | View Code Duplication | public static function add_label( $args = array() ) { |
|
204 | |||
205 | /** |
||
206 | * Add tooltip by group ( if any ) |
||
207 | * |
||
208 | * @since 2.0 |
||
209 | * @access public |
||
210 | * |
||
211 | * @param array $args |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | View Code Duplication | public static function add_tooltip( $args = array() ) { |
|
231 | |||
232 | /** |
||
233 | * Add translation by group ( if any ) |
||
234 | * |
||
235 | * @since 2.0 |
||
236 | * @access public |
||
237 | * |
||
238 | * @param array 4args |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public static function add_translation( $args = array() ) { |
||
263 | |||
264 | /** |
||
265 | * Get label translation by group ( if any ) |
||
266 | * |
||
267 | * @since 2.0 |
||
268 | * @access public |
||
269 | * |
||
270 | * @param string $id |
||
271 | * @param string $group |
||
272 | * @param string $text |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | View Code Duplication | public static function add_label_translation( $id, $group = '', $text = '' ) { |
|
283 | |||
284 | /** |
||
285 | * Get tooltip translation by group ( if any ) |
||
286 | * |
||
287 | * @since 2.0 |
||
288 | * @access public |
||
289 | * |
||
290 | * @param string $id |
||
291 | * @param string $group |
||
292 | * @param string $text |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | View Code Duplication | public static function add_tooltip_translation( $id, $group = '', $text = '' ) { |
|
303 | |||
304 | /** |
||
305 | * Get label by group ( if any ) |
||
306 | * |
||
307 | * @since 2.0 |
||
308 | * @access public |
||
309 | * |
||
310 | * @param string $id |
||
311 | * @param string $group |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public static function get_label( $id, $group = '' ) { |
||
322 | |||
323 | /** |
||
324 | * Get tooltip by group ( if any ) |
||
325 | * |
||
326 | * @since 2.0 |
||
327 | * @access public |
||
328 | * |
||
329 | * @param string $id |
||
330 | * @param string $group |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | public static function get_tooltip( $id, $group = '' ) { |
||
341 | |||
342 | /** |
||
343 | * Get text by group |
||
344 | * |
||
345 | * @since 2.0 |
||
346 | * @access public |
||
347 | * |
||
348 | * @param array $args |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | public static function get_text( $args = array() ) { |
||
418 | } |
||
419 | |||
422 |