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) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns whether the translation is disabled |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function isDisabled() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets the original string. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getOriginal() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Checks if the original string is empty or not. |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function hasOriginal() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Sets the translation string. |
||
| 141 | * |
||
| 142 | * @param string $translation |
||
| 143 | * |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | public function setTranslation($translation) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Gets the translation string. |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getTranslation() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Checks if the translation string is empty or not. |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function hasTranslation() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets the plural translation string. |
||
| 175 | * |
||
| 176 | * @param string $plural |
||
| 177 | * |
||
| 178 | * @return self |
||
| 179 | */ |
||
| 180 | public function setPlural($plural) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Gets the plural translation string. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getPlural() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Checks if the plural translation string is empty or not. |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function hasPlural() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set a new plural translation. |
||
| 209 | * |
||
| 210 | * @param array $plural |
||
| 211 | * |
||
| 212 | * @return self |
||
| 213 | */ |
||
| 214 | public function setPluralTranslations(array $plural) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Gets all plural translations. |
||
| 223 | * |
||
| 224 | * @param int $size |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getPluralTranslations($size = null) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Checks if there are any plural translation. |
||
| 249 | * |
||
| 250 | * @param bool $checkContent |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function hasPluralTranslations($checkContent = false) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Removes all plural translations. |
||
| 265 | * |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function deletePluralTranslation() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Gets the context of this translation. |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getContext() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks if the context is empty or not. |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function hasContext() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Adds a new reference for this translation. |
||
| 297 | * |
||
| 298 | * @param string $filename The file path where the translation has been found |
||
| 299 | * @param null|int $line The line number where the translation has been found |
||
| 300 | * |
||
| 301 | * @return self |
||
| 302 | */ |
||
| 303 | public function addReference($filename, $line = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks if the translation has any reference. |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function hasReferences() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Return all references for this translation. |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | public function getReferences() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Removes all references. |
||
| 333 | * |
||
| 334 | * @return self |
||
| 335 | */ |
||
| 336 | public function deleteReferences() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Adds a new comment for this translation. |
||
| 345 | * |
||
| 346 | * @param string $comment |
||
| 347 | * |
||
| 348 | * @return self |
||
| 349 | */ |
||
| 350 | public function addComment($comment) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Checks if the translation has any comment. |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function hasComments() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns all comments for this translation. |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | public function getComments() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Removes all comments. |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | public function deleteComments() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Adds a new extracted comment for this translation. |
||
| 393 | * |
||
| 394 | * @param string $comment |
||
| 395 | * |
||
| 396 | * @return self |
||
| 397 | */ |
||
| 398 | public function addExtractedComment($comment) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Checks if the translation has any extracted comment. |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function hasExtractedComments() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Returns all extracted comments for this translation. |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | public function getExtractedComments() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Removes all extracted comments. |
||
| 429 | * |
||
| 430 | * @return self |
||
| 431 | */ |
||
| 432 | public function deleteExtractedComments() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Adds a new flag for this translation. |
||
| 441 | * |
||
| 442 | * @param string $flag |
||
| 443 | * |
||
| 444 | * @return self |
||
| 445 | */ |
||
| 446 | public function addFlag($flag) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Checks if the translation has any flag. |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function hasFlags() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns all extracted flags for this translation. |
||
| 467 | * |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | public function getFlags() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Removes all flags. |
||
| 477 | * |
||
| 478 | * @return self |
||
| 479 | */ |
||
| 480 | public function deleteFlags() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Merges this translation with other translation. |
||
| 489 | * |
||
| 490 | * @param Translation $translation The translation to merge with |
||
| 491 | * @param int $options |
||
| 492 | * |
||
| 493 | * @return self |
||
| 494 | */ |
||
| 495 | public function mergeWith(Translation $translation, $options = Merge::DEFAULTS) |
||
| 505 | } |
||
| 506 |