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 = array(); |
||
| 15 | protected $references = array(); |
||
| 16 | protected $comments = array(); |
||
| 17 | protected $extractedComments = array(); |
||
| 18 | protected $flags = array(); |
||
| 19 | protected $translationCount; |
||
| 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 = '') |
||
| 42 | { |
||
| 43 | $this->context = (string) $context; |
||
| 44 | $this->original = (string) $original; |
||
| 45 | |||
| 46 | $this->setPlural($plural); |
||
| 47 | } |
||
| 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 | public function getClone($context = null, $original = null) |
||
| 56 | { |
||
| 57 | $new = clone $this; |
||
| 58 | |||
| 59 | if ($context !== null) { |
||
| 60 | $new->context = (string) $context; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($original !== null) { |
||
| 64 | $new->original = (string) $original; |
||
| 65 | } |
||
| 66 | |||
| 67 | return $new; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the id of this translation. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getId() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Checks whether the translation matches with the arguments. |
||
| 82 | * |
||
| 83 | * @param string $context |
||
| 84 | * @param string $original |
||
| 85 | * |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | public function is($context, $original = '') |
||
| 89 | { |
||
| 90 | return (($this->context === $context) && ($this->original === $original)) ? true : false; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets the original string. |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getOriginal() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Checks if the original string is empty or not. |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function hasOriginal() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets the translation string. |
||
| 115 | * |
||
| 116 | * @param string $translation |
||
| 117 | * |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | public function setTranslation($translation) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Gets the translation string. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getTranslation() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Checks if the translation string is empty or not. |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function hasTranslation() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Sets the plural translation string. |
||
| 149 | * |
||
| 150 | * @param string $plural |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function setPlural($plural) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the plural translation string. |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getPlural() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Checks if the plural translation string is empty or not. |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function hasPlural() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set a new plural translation. |
||
| 185 | * |
||
| 186 | * @param array $plural |
||
| 187 | * |
||
| 188 | * @return self |
||
| 189 | */ |
||
| 190 | public function setPluralTranslations(array $plural) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set a new plural translation. |
||
| 200 | * |
||
| 201 | * @param string $plural The plural string to add |
||
| 202 | * @param int $key The key of the plural translation. |
||
| 203 | * |
||
| 204 | * @return self |
||
| 205 | */ |
||
| 206 | public function setPluralTranslation($plural, $key = 0) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets one or all plural translations. |
||
| 216 | * |
||
| 217 | * @param int|null $key The key to return. If is null, return all translations |
||
| 218 | * |
||
| 219 | * @return string|array |
||
| 220 | */ |
||
| 221 | public function getPluralTranslation($key = 0) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets all plural translations. |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function getPluralTranslations() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Checks if there are any plural translation. |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function hasPluralTranslation() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Removes all plural translations. |
||
| 248 | */ |
||
| 249 | public function deletePluralTranslation() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set the number of singular + plural translations allowed. |
||
| 258 | * |
||
| 259 | * @param int $count |
||
| 260 | * |
||
| 261 | * @return self |
||
| 262 | */ |
||
| 263 | public function setTranslationCount($count) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns the number of singular + plural translations |
||
| 274 | * Returns null if this Translation is not a plural one. |
||
| 275 | * |
||
| 276 | * @return int|null |
||
| 277 | */ |
||
| 278 | public function getTranslationCount() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Normalizes the translation count. |
||
| 285 | */ |
||
| 286 | protected function normalizeTranslationCount() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Gets the context of this translation. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getContext() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Checks if the context is empty or not. |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function hasContext() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Adds a new reference for this translation. |
||
| 328 | * |
||
| 329 | * @param string $filename The file path where the translation has been found |
||
| 330 | * @param null|int $line The line number where the translation has been found |
||
| 331 | */ |
||
| 332 | public function addReference($filename, $line = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Checks if the translation has any reference. |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function hasReferences() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return all references for this translation. |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function getReferences() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Removes all references. |
||
| 360 | */ |
||
| 361 | public function deleteReferences() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Adds a new comment for this translation. |
||
| 368 | * |
||
| 369 | * @param string $comment |
||
| 370 | */ |
||
| 371 | 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 | public function deleteComments() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Adds a new extracted comment for this translation. |
||
| 406 | * |
||
| 407 | * @param string $comment |
||
| 408 | */ |
||
| 409 | public function addExtractedComment($comment) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Checks if the translation has any extracted comment. |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function hasExtractedComments() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Returns all extracted comments for this translation. |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function getExtractedComments() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Removes all extracted comments. |
||
| 436 | */ |
||
| 437 | public function deleteExtractedComments() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Adds a new flat for this translation. |
||
| 444 | * |
||
| 445 | * @param string $flag |
||
| 446 | */ |
||
| 447 | public function addFlag($flag) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Checks if the translation has any flag. |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function hasFlags() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns all extracted flags for this translation. |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getFlags() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Removes all flags. |
||
| 474 | */ |
||
| 475 | public function deleteFlags() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Merges this translation with other translation. |
||
| 482 | * |
||
| 483 | * @param Translation $translation The translation to merge with |
||
| 484 | * @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations |
||
| 485 | */ |
||
| 486 | public function mergeWith(Translation $translation, $method = null) |
||
| 516 | } |
||
| 517 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..