Complex classes like Translation 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 Translation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Translation |
||
9 | { |
||
10 | const MERGE_TRANSLATION_OVERRIDE = 1; |
||
11 | const MERGE_PLURAL_OVERRIDE = 2; |
||
12 | const MERGE_COMMENTS_MINES = 4; |
||
13 | const MERGE_COMMENTS_THEIRS = 8; |
||
14 | const MERGE_EXTRACTED_COMMENTS_MINES = 16; |
||
15 | const MERGE_EXTRACTED_COMMENTS_THEIRS = 32; |
||
16 | const MERGE_FLAGS_MINES = 64; |
||
17 | const MERGE_FLAGS_THEIRS = 128; |
||
18 | const MERGE_REFERENCES_MINES = 256; |
||
19 | const MERGE_REFERENCES_THEIRS = 512; |
||
20 | |||
21 | protected $context; |
||
22 | protected $original; |
||
23 | protected $translation = ''; |
||
24 | protected $plural; |
||
25 | protected $pluralTranslation = []; |
||
26 | protected $references = []; |
||
27 | protected $comments = []; |
||
28 | protected $extractedComments = []; |
||
29 | protected $flags = []; |
||
30 | |||
31 | /** |
||
32 | * Generates the id of a translation (context + glue + original). |
||
33 | * |
||
34 | * @param string $context |
||
35 | * @param string $original |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | public static function generateId($context, $original) |
||
43 | |||
44 | /** |
||
45 | * Construct. |
||
46 | * |
||
47 | * @param string $context The context of the translation |
||
48 | * @param string $original The original string |
||
49 | * @param string $plural The original plural string |
||
50 | */ |
||
51 | public function __construct($context, $original, $plural = '') |
||
58 | |||
59 | /** |
||
60 | * Clones this translation. |
||
61 | * |
||
62 | * @param null|string $context Optional new context |
||
63 | * @param null|string $original Optional new original |
||
64 | * |
||
65 | * @return Translation |
||
66 | */ |
||
67 | public function getClone($context = null, $original = null) |
||
81 | |||
82 | /** |
||
83 | * Returns the id of this translation. |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getId() |
||
91 | |||
92 | /** |
||
93 | * Checks whether the translation matches with the arguments. |
||
94 | * |
||
95 | * @param string $context |
||
96 | * @param string $original |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function is($context, $original = '') |
||
104 | |||
105 | /** |
||
106 | * Gets the original string. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getOriginal() |
||
114 | |||
115 | /** |
||
116 | * Checks if the original string is empty or not. |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function hasOriginal() |
||
124 | |||
125 | /** |
||
126 | * Sets the translation string. |
||
127 | * |
||
128 | * @param string $translation |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | public function setTranslation($translation) |
||
138 | |||
139 | /** |
||
140 | * Gets the translation string. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getTranslation() |
||
148 | |||
149 | /** |
||
150 | * Checks if the translation string is empty or not. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function hasTranslation() |
||
158 | |||
159 | /** |
||
160 | * Sets the plural translation string. |
||
161 | * |
||
162 | * @param string $plural |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | public function setPlural($plural) |
||
172 | |||
173 | /** |
||
174 | * Gets the plural translation string. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getPlural() |
||
182 | |||
183 | /** |
||
184 | * Checks if the plural translation string is empty or not. |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function hasPlural() |
||
192 | |||
193 | /** |
||
194 | * Set a new plural translation. |
||
195 | * |
||
196 | * @param array $plural |
||
197 | * |
||
198 | * @return self |
||
199 | */ |
||
200 | public function setPluralTranslations(array $plural) |
||
206 | |||
207 | /** |
||
208 | * Gets all plural translations. |
||
209 | * |
||
210 | * @param int $limit |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | public function getPluralTranslations($limit = null) |
||
232 | |||
233 | /** |
||
234 | * Checks if there are any plural translation. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function hasPluralTranslations() |
||
242 | |||
243 | /** |
||
244 | * Removes all plural translations. |
||
245 | * |
||
246 | * @return self |
||
247 | */ |
||
248 | public function deletePluralTranslation() |
||
254 | |||
255 | /** |
||
256 | * Gets the context of this translation. |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getContext() |
||
264 | |||
265 | /** |
||
266 | * Checks if the context is empty or not. |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function hasContext() |
||
274 | |||
275 | /** |
||
276 | * Adds a new reference for this translation. |
||
277 | * |
||
278 | * @param string $filename The file path where the translation has been found |
||
279 | * @param null|int $line The line number where the translation has been found |
||
280 | * |
||
281 | * @return self |
||
282 | */ |
||
283 | public function addReference($filename, $line = null) |
||
290 | |||
291 | /** |
||
292 | * Checks if the translation has any reference. |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function hasReferences() |
||
300 | |||
301 | /** |
||
302 | * Return all references for this translation. |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | public function getReferences() |
||
310 | |||
311 | /** |
||
312 | * Removes all references. |
||
313 | * |
||
314 | * @return self |
||
315 | */ |
||
316 | public function deleteReferences() |
||
322 | |||
323 | /** |
||
324 | * Adds a new comment for this translation. |
||
325 | * |
||
326 | * @param string $comment |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | public function addComment($comment) |
||
338 | |||
339 | /** |
||
340 | * Checks if the translation has any comment. |
||
341 | * |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function hasComments() |
||
348 | |||
349 | /** |
||
350 | * Returns all comments for this translation. |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function getComments() |
||
358 | |||
359 | /** |
||
360 | * Removes all comments. |
||
361 | * |
||
362 | * @return self |
||
363 | */ |
||
364 | public function deleteComments() |
||
370 | |||
371 | /** |
||
372 | * Adds a new extracted comment for this translation. |
||
373 | * |
||
374 | * @param string $comment |
||
375 | * |
||
376 | * @return self |
||
377 | */ |
||
378 | public function addExtractedComment($comment) |
||
386 | |||
387 | /** |
||
388 | * Checks if the translation has any extracted comment. |
||
389 | * |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function hasExtractedComments() |
||
396 | |||
397 | /** |
||
398 | * Returns all extracted comments for this translation. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | public function getExtractedComments() |
||
406 | |||
407 | /** |
||
408 | * Removes all extracted comments. |
||
409 | * |
||
410 | * @return self |
||
411 | */ |
||
412 | public function deleteExtractedComments() |
||
418 | |||
419 | /** |
||
420 | * Adds a new flag for this translation. |
||
421 | * |
||
422 | * @param string $flag |
||
423 | * |
||
424 | * @return self |
||
425 | */ |
||
426 | public function addFlag($flag) |
||
434 | |||
435 | /** |
||
436 | * Checks if the translation has any flag. |
||
437 | * |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function hasFlags() |
||
444 | |||
445 | /** |
||
446 | * Returns all extracted flags for this translation. |
||
447 | * |
||
448 | * @return array |
||
449 | */ |
||
450 | public function getFlags() |
||
454 | |||
455 | /** |
||
456 | * Removes all flags. |
||
457 | * |
||
458 | * @return self |
||
459 | */ |
||
460 | public function deleteFlags() |
||
466 | |||
467 | /** |
||
468 | * Merges this translation with other translation. |
||
469 | * |
||
470 | * @param Translation $translation The translation to merge with |
||
471 | * @param int $options The merging options |
||
472 | * |
||
473 | * @return self |
||
474 | */ |
||
475 | public function mergeWith(Translation $translation, $options = self::MERGE_TRANSLATION_OVERRIDE) |
||
531 | } |
||
532 |