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 $id; |
||
| 11 | protected $context; |
||
| 12 | protected $original; |
||
| 13 | protected $translation = ''; |
||
| 14 | protected $plural; |
||
| 15 | protected $pluralTranslation = []; |
||
| 16 | protected $references = []; |
||
| 17 | protected $comments = []; |
||
| 18 | protected $extractedComments = []; |
||
| 19 | protected $flags = []; |
||
| 20 | protected $disabled = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Generates the id of a translation (context + glue + original). |
||
| 24 | * |
||
| 25 | * @param string $context |
||
| 26 | * @param string $original |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public static function generateId($context, $original) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Construct. |
||
| 37 | * |
||
| 38 | * @param string $context The context of the translation |
||
| 39 | * @param string $original The original string |
||
| 40 | * @param string $plural The original plural string |
||
| 41 | */ |
||
| 42 | public function __construct($context, $original, $plural = '') |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Clones this translation. |
||
| 52 | * |
||
| 53 | * @param null|string $context Optional new context |
||
| 54 | * @param null|string $original Optional new original |
||
| 55 | * |
||
| 56 | * @return Translation |
||
| 57 | */ |
||
| 58 | public function getClone($context = null, $original = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Sets the id of this translation. |
||
| 75 | * @warning The use of this function to set a custom ID will prevent |
||
| 76 | * Translations::find from matching this translation. |
||
| 77 | * |
||
| 78 | * @param string $id |
||
| 79 | */ |
||
| 80 | public function setId($id) |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns the id of this translation. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getId() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Checks whether the translation matches with the arguments. |
||
| 101 | * |
||
| 102 | * @param string $context |
||
| 103 | * @param string $original |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function is($context, $original = '') |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Enable or disable the translation |
||
| 114 | * |
||
| 115 | * @param bool $disabled |
||
| 116 | * |
||
| 117 | * @return self |
||
| 118 | */ |
||
| 119 | public function setDisabled($disabled) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns whether the translation is disabled |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function isDisabled() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the original string. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getOriginal() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Checks if the original string is empty or not. |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function hasOriginal() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets the translation string. |
||
| 158 | * |
||
| 159 | * @param string $translation |
||
| 160 | * |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | public function setTranslation($translation) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Gets the translation string. |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getTranslation() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Checks if the translation string is empty or not. |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function hasTranslation() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the plural translation string. |
||
| 192 | * |
||
| 193 | * @param string $plural |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | public function setPlural($plural) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the plural translation string. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getPlural() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Checks if the plural translation string is empty or not. |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function hasPlural() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set a new plural translation. |
||
| 226 | * |
||
| 227 | * @param array $plural |
||
| 228 | * |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function setPluralTranslations(array $plural) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gets all plural translations. |
||
| 240 | * |
||
| 241 | * @param int $size |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getPluralTranslations($size = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Checks if there are any plural translation. |
||
| 266 | * |
||
| 267 | * @param bool $checkContent |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function hasPluralTranslations($checkContent = false) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Removes all plural translations. |
||
| 282 | * |
||
| 283 | * @return self |
||
| 284 | */ |
||
| 285 | public function deletePluralTranslation() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Gets the context of this translation. |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getContext() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Checks if the context is empty or not. |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function hasContext() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds a new reference for this translation. |
||
| 314 | * |
||
| 315 | * @param string $filename The file path where the translation has been found |
||
| 316 | * @param null|int $line The line number where the translation has been found |
||
| 317 | * |
||
| 318 | * @return self |
||
| 319 | */ |
||
| 320 | public function addReference($filename, $line = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Checks if the translation has any reference. |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function hasReferences() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return all references for this translation. |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function getReferences() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Removes all references. |
||
| 350 | * |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | public function deleteReferences() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Adds a new comment for this translation. |
||
| 362 | * |
||
| 363 | * @param string $comment |
||
| 364 | * |
||
| 365 | * @return self |
||
| 366 | */ |
||
| 367 | public function addComment($comment) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Checks if the translation has any comment. |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function hasComments() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns all comments for this translation. |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function getComments() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Removes all comments. |
||
| 398 | * |
||
| 399 | * @return self |
||
| 400 | */ |
||
| 401 | public function deleteComments() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Adds a new extracted comment for this translation. |
||
| 410 | * |
||
| 411 | * @param string $comment |
||
| 412 | * |
||
| 413 | * @return self |
||
| 414 | */ |
||
| 415 | public function addExtractedComment($comment) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Checks if the translation has any extracted comment. |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function hasExtractedComments() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Returns all extracted comments for this translation. |
||
| 436 | * |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | public function getExtractedComments() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Removes all extracted comments. |
||
| 446 | * |
||
| 447 | * @return self |
||
| 448 | */ |
||
| 449 | public function deleteExtractedComments() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Adds a new flag for this translation. |
||
| 458 | * |
||
| 459 | * @param string $flag |
||
| 460 | * |
||
| 461 | * @return self |
||
| 462 | */ |
||
| 463 | public function addFlag($flag) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Checks if the translation has any flag. |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function hasFlags() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns all extracted flags for this translation. |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getFlags() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Removes all flags. |
||
| 494 | * |
||
| 495 | * @return self |
||
| 496 | */ |
||
| 497 | public function deleteFlags() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Merges this translation with other translation. |
||
| 506 | * |
||
| 507 | * @param Translation $translation The translation to merge with |
||
| 508 | * @param int $options |
||
| 509 | * |
||
| 510 | * @return self |
||
| 511 | */ |
||
| 512 | public function mergeWith(Translation $translation, $options = Merge::DEFAULTS) |
||
| 522 | } |
||
| 523 |