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 | protected $context; |
||
11 | protected $original; |
||
12 | protected $translation = ''; |
||
13 | protected $plural; |
||
14 | protected $pluralTranslation = []; |
||
15 | protected $references = []; |
||
16 | protected $comments = []; |
||
17 | protected $extractedComments = []; |
||
18 | protected $flags = []; |
||
19 | protected $disabled = false; |
||
20 | |||
21 | /** |
||
22 | * Generates the id of a translation (context + glue + original). |
||
23 | * |
||
24 | * @param string $context |
||
25 | * @param string $original |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | public static function generateId($context, $original) |
||
33 | |||
34 | /** |
||
35 | * Construct. |
||
36 | * |
||
37 | * @param string $context The context of the translation |
||
38 | * @param string $original The original string |
||
39 | * @param string $plural The original plural string |
||
40 | */ |
||
41 | public function __construct($context, $original, $plural = '') |
||
48 | |||
49 | /** |
||
50 | * Clones this translation. |
||
51 | * |
||
52 | * @param null|string $context Optional new context |
||
53 | * @param null|string $original Optional new original |
||
54 | * |
||
55 | * @return Translation |
||
56 | */ |
||
57 | public function getClone($context = null, $original = null) |
||
71 | |||
72 | /** |
||
73 | * Returns the id of this translation. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getId() |
||
81 | |||
82 | /** |
||
83 | * Checks whether the translation matches with the arguments. |
||
84 | * |
||
85 | * @param string $context |
||
86 | * @param string $original |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function is($context, $original = '') |
||
94 | |||
95 | /** |
||
96 | * Enable or disable the translation |
||
97 | * |
||
98 | * @param bool $disabled |
||
99 | * |
||
100 | * @return self |
||
101 | */ |
||
102 | public function setDisabled($disabled) { |
||
107 | |||
108 | /** |
||
109 | * Returns whether the translation is disabled |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isDisabled() |
||
117 | |||
118 | /** |
||
119 | * Gets the original string. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getOriginal() |
||
127 | |||
128 | /** |
||
129 | * Checks if the original string is empty or not. |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function hasOriginal() |
||
137 | |||
138 | /** |
||
139 | * Sets the translation string. |
||
140 | * |
||
141 | * @param string $translation |
||
142 | * |
||
143 | * @return self |
||
144 | */ |
||
145 | public function setTranslation($translation) |
||
151 | |||
152 | /** |
||
153 | * Gets the translation string. |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getTranslation() |
||
161 | |||
162 | /** |
||
163 | * Checks if the translation string is empty or not. |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasTranslation() |
||
171 | |||
172 | /** |
||
173 | * Sets the plural translation string. |
||
174 | * |
||
175 | * @param string $plural |
||
176 | * |
||
177 | * @return self |
||
178 | */ |
||
179 | public function setPlural($plural) |
||
185 | |||
186 | /** |
||
187 | * Gets the plural translation string. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getPlural() |
||
195 | |||
196 | /** |
||
197 | * Checks if the plural translation string is empty or not. |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function hasPlural() |
||
205 | |||
206 | /** |
||
207 | * Set a new plural translation. |
||
208 | * |
||
209 | * @param array $plural |
||
210 | * |
||
211 | * @return self |
||
212 | */ |
||
213 | public function setPluralTranslations(array $plural) |
||
219 | |||
220 | /** |
||
221 | * Gets all plural translations. |
||
222 | * |
||
223 | * @param int $size |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function getPluralTranslations($size = null) |
||
245 | |||
246 | /** |
||
247 | * Checks if there are any plural translation. |
||
248 | * |
||
249 | * @param bool $checkContent |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function hasPluralTranslations($checkContent = false) |
||
261 | |||
262 | /** |
||
263 | * Removes all plural translations. |
||
264 | * |
||
265 | * @return self |
||
266 | */ |
||
267 | public function deletePluralTranslation() |
||
273 | |||
274 | /** |
||
275 | * Gets the context of this translation. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getContext() |
||
283 | |||
284 | /** |
||
285 | * Checks if the context is empty or not. |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function hasContext() |
||
293 | |||
294 | /** |
||
295 | * Adds a new reference for this translation. |
||
296 | * |
||
297 | * @param string $filename The file path where the translation has been found |
||
298 | * @param null|int $line The line number where the translation has been found |
||
299 | * |
||
300 | * @return self |
||
301 | */ |
||
302 | public function addReference($filename, $line = null) |
||
309 | |||
310 | /** |
||
311 | * Checks if the translation has any reference. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function hasReferences() |
||
319 | |||
320 | /** |
||
321 | * Return all references for this translation. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getReferences() |
||
329 | |||
330 | /** |
||
331 | * Removes all references. |
||
332 | * |
||
333 | * @return self |
||
334 | */ |
||
335 | public function deleteReferences() |
||
341 | |||
342 | /** |
||
343 | * Adds a new comment for this translation. |
||
344 | * |
||
345 | * @param string $comment |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | public function addComment($comment) |
||
357 | |||
358 | /** |
||
359 | * Checks if the translation has any comment. |
||
360 | * |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function hasComments() |
||
367 | |||
368 | /** |
||
369 | * Returns all comments for this translation. |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | public function getComments() |
||
377 | |||
378 | /** |
||
379 | * Removes all comments. |
||
380 | * |
||
381 | * @return self |
||
382 | */ |
||
383 | public function deleteComments() |
||
389 | |||
390 | /** |
||
391 | * Adds a new extracted comment for this translation. |
||
392 | * |
||
393 | * @param string $comment |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | public function addExtractedComment($comment) |
||
405 | |||
406 | /** |
||
407 | * Checks if the translation has any extracted comment. |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function hasExtractedComments() |
||
415 | |||
416 | /** |
||
417 | * Returns all extracted comments for this translation. |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | public function getExtractedComments() |
||
425 | |||
426 | /** |
||
427 | * Removes all extracted comments. |
||
428 | * |
||
429 | * @return self |
||
430 | */ |
||
431 | public function deleteExtractedComments() |
||
437 | |||
438 | /** |
||
439 | * Adds a new flag for this translation. |
||
440 | * |
||
441 | * @param string $flag |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | public function addFlag($flag) |
||
453 | |||
454 | /** |
||
455 | * Checks if the translation has any flag. |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function hasFlags() |
||
463 | |||
464 | /** |
||
465 | * Returns all extracted flags for this translation. |
||
466 | * |
||
467 | * @return array |
||
468 | */ |
||
469 | public function getFlags() |
||
473 | |||
474 | /** |
||
475 | * Removes all flags. |
||
476 | * |
||
477 | * @return self |
||
478 | */ |
||
479 | public function deleteFlags() |
||
485 | |||
486 | /** |
||
487 | * Merges this translation with other translation. |
||
488 | * |
||
489 | * @param Translation $translation The translation to merge with |
||
490 | * @param int $options |
||
491 | * |
||
492 | * @return self |
||
493 | */ |
||
494 | public function mergeWith(Translation $translation, $options = Merge::DEFAULTS) |
||
504 | } |
||
505 |