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 |
||
| 21 | class Translation extends ArrayObject implements |
||
| 22 | ArrayFetchInterface, |
||
| 23 | ArrayMergeInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Pattern of a variable |
||
| 27 | */ |
||
| 28 | const VAR_PATTERN = '{$%s}'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Prefix of the extra variables to avoid conflicts |
||
| 32 | */ |
||
| 33 | const EXTRA_VAR_PREFIX = '_'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Pattern to match variables |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $varPregPattern; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Return value in depth from multidimensional array |
||
| 44 | * |
||
| 45 | * @param string $basePath Something like: value.in.the.depth |
||
| 46 | * @return mixed Result value |
||
| 47 | */ |
||
| 48 | public function fetch($basePath) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param array $array |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function merge(array $array) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param array $keys |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function unsetKeys(array $keys) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function getVarTranslation() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $varKey |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function makeExtraVarKey($varKey) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Transform varName into {$varName}. |
||
| 135 | * |
||
| 136 | * @param string $key |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function makeVar($key) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Transform subject keys to {$var} like |
||
| 146 | * |
||
| 147 | * @param ArrayAccess $subject |
||
| 148 | * @return ArrayAccess|self |
||
| 149 | */ |
||
| 150 | public function makeVarKeys(ArrayAccess $subject = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Match {$var} regular pattern |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getVarPregPattern() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return true if {$var} is in the string |
||
| 179 | * |
||
| 180 | * @param string $string |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function containsVar($string) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove vars from string |
||
| 190 | * |
||
| 191 | * @param string $string |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | public function removeVars($string) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Replace {$var} in string with data from translation |
||
| 204 | * |
||
| 205 | * If $str = {$var} and translation has item with key {$var} = array, |
||
| 206 | * immediately return this array. |
||
| 207 | * |
||
| 208 | * @param string $str |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function translateString($str) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Replace {$var} in $subject with data from $translation |
||
| 250 | * |
||
| 251 | * @param string|array $subject |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function translate(&$subject) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param array $values |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function mergeValues(array $values) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Set translated defaults into translation |
||
| 296 | * |
||
| 297 | * @param array $defaults |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | public function setDefaults(array $defaults) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param mixed $key |
||
| 319 | * @param mixed $value |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | protected function setDefault($key, $value) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Fetch custom variables into translation |
||
| 331 | * |
||
| 332 | * example of properties: |
||
| 333 | * <pre> |
||
| 334 | * $translation = [ |
||
| 335 | * 'value' => [ |
||
| 336 | * 'in' => [ |
||
| 337 | * 'the' => [ |
||
| 338 | * 'depth' => 'valueInTheDepth', |
||
| 339 | * ], |
||
| 340 | * ], |
||
| 341 | * ], |
||
| 342 | * ]; |
||
| 343 | * </pre> |
||
| 344 | * |
||
| 345 | * example of options: |
||
| 346 | * |
||
| 347 | * <pre> |
||
| 348 | * $options = [ |
||
| 349 | * 'customVar' => 'value.in.the.depth', |
||
| 350 | * ]; |
||
| 351 | * </pre> |
||
| 352 | * |
||
| 353 | * @param array $options |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function fetchVars(array $options) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Push variables into translation |
||
| 366 | * |
||
| 367 | * @param array $options |
||
| 368 | * @param self $translation |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function pushVars(array $options, self $translation = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Explode path by dots and return those parts |
||
| 412 | * |
||
| 413 | * @param string $basePath |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | private function resolveBasePathParts($basePath) |
||
| 422 | } |
||
| 423 |