Complex classes like Translations 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 Translations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 96 | class Translations extends ArrayObject |
||
| 97 | { |
||
| 98 | const HEADER_LANGUAGE = 'Language'; |
||
| 99 | const HEADER_PLURAL = 'Plural-Forms'; |
||
| 100 | const HEADER_DOMAIN = 'X-Domain'; |
||
| 101 | |||
| 102 | public static $options = [ |
||
| 103 | 'defaultHeaders' => [ |
||
| 104 | 'Project-Id-Version' => '', |
||
| 105 | 'Report-Msgid-Bugs-To' => '', |
||
| 106 | 'Last-Translator' => '', |
||
| 107 | 'Language-Team' => '', |
||
| 108 | 'MIME-Version' => '1.0', |
||
| 109 | 'Content-Type' => 'text/plain; charset=UTF-8', |
||
| 110 | 'Content-Transfer-Encoding' => '8bit', |
||
| 111 | ], |
||
| 112 | 'headersSorting' => false, |
||
| 113 | 'defaultDateHeaders' => [ |
||
| 114 | 'POT-Creation-Date', |
||
| 115 | 'PO-Revision-Date', |
||
| 116 | ], |
||
| 117 | ]; |
||
| 118 | |||
| 119 | private $headers; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @see ArrayObject::__construct() |
||
| 123 | */ |
||
| 124 | public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator') |
||
| 125 | { |
||
| 126 | $this->headers = static::$options['defaultHeaders']; |
||
| 127 | |||
| 128 | foreach (static::$options['defaultDateHeaders'] as $header) { |
||
| 129 | $this->headers[$header] = date('c'); |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->headers[self::HEADER_LANGUAGE] = ''; |
||
| 133 | |||
| 134 | parent::__construct($input, $flags, $iterator_class); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Magic method to create new instances using extractors |
||
| 139 | * For example: Translations::fromMoFile($filename, $options);. |
||
| 140 | * |
||
| 141 | * @return Translations |
||
| 142 | */ |
||
| 143 | public static function __callStatic($name, $arguments) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Magic method to import/export the translations to a specific format |
||
| 154 | * For example: $translations->toMoFile($filename, $options); |
||
| 155 | * For example: $translations->addFromMoFile($filename, $options);. |
||
| 156 | * |
||
| 157 | * @return self|bool |
||
| 158 | */ |
||
| 159 | public function __call($name, $arguments) |
||
| 160 | { |
||
| 161 | if (!preg_match('/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches)) { |
||
| 162 | throw new BadMethodCallException("The method $name does not exists"); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($matches[1] === 'addFrom') { |
||
| 166 | $extractor = 'Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3]; |
||
| 167 | $source = array_shift($arguments); |
||
| 168 | $options = array_shift($arguments) ?: []; |
||
| 169 | |||
| 170 | call_user_func($extractor, $source, $this, $options); |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | $generator = 'Gettext\\Generators\\'.$matches[2].'::to'.$matches[3]; |
||
| 176 | |||
| 177 | array_unshift($arguments, $this); |
||
| 178 | |||
| 179 | return call_user_func_array($generator, $arguments); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Magic method to clone each translation on clone the translations object. |
||
| 184 | */ |
||
| 185 | public function __clone() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Control the new translations added. |
||
| 198 | * |
||
| 199 | * @param mixed $index |
||
| 200 | * @param Translation $value |
||
| 201 | * |
||
| 202 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
| 203 | * |
||
| 204 | * @return Translation |
||
| 205 | */ |
||
| 206 | public function offsetSet($index, $value) |
||
| 207 | { |
||
| 208 | if (!($value instanceof Translation)) { |
||
| 209 | throw new InvalidArgumentException('Only instances of Gettext\\Translation must be added to a Gettext\\Translations'); |
||
| 210 | } |
||
| 211 | |||
| 212 | $id = $value->getId(); |
||
| 213 | |||
| 214 | if ($this->offsetExists($id)) { |
||
| 215 | $this[$id]->mergeWith($value); |
||
| 216 | |||
| 217 | return $this[$id]; |
||
| 218 | } |
||
| 219 | |||
| 220 | parent::offsetSet($id, $value); |
||
| 221 | |||
| 222 | return $value; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the plural definition. |
||
| 227 | * |
||
| 228 | * @param int $count |
||
| 229 | * @param string $rule |
||
| 230 | * |
||
| 231 | * @return self |
||
| 232 | */ |
||
| 233 | public function setPluralForms($count, $rule) |
||
| 234 | { |
||
| 235 | $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};"); |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the parsed plural definition. |
||
| 242 | * |
||
| 243 | * @param null|array [count, rule] |
||
| 244 | */ |
||
| 245 | public function getPluralForms() |
||
| 246 | { |
||
| 247 | $header = $this->getHeader(self::HEADER_PLURAL); |
||
| 248 | |||
| 249 | if (!empty($header) && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches)) { |
||
| 250 | return [intval($matches[1]), $matches[2]]; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set a new header. |
||
| 256 | * |
||
| 257 | * @param string $name |
||
| 258 | * @param string $value |
||
| 259 | * |
||
| 260 | * @return self |
||
| 261 | */ |
||
| 262 | public function setHeader($name, $value) |
||
| 263 | { |
||
| 264 | $name = trim($name); |
||
| 265 | $this->headers[$name] = trim($value); |
||
| 266 | |||
| 267 | return $this; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns a header value. |
||
| 272 | * |
||
| 273 | * @param string $name |
||
| 274 | * |
||
| 275 | * @return null|string |
||
| 276 | */ |
||
| 277 | public function getHeader($name) |
||
| 278 | { |
||
| 279 | return isset($this->headers[$name]) ? $this->headers[$name] : null; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns all header for this translations (in alphabetic order). |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getHeaders() |
||
| 288 | { |
||
| 289 | if (static::$options['headersSorting']) { |
||
| 290 | ksort($this->headers); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $this->headers; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Removes all headers. |
||
| 298 | * |
||
| 299 | * @return self |
||
| 300 | */ |
||
| 301 | public function deleteHeaders() |
||
| 302 | { |
||
| 303 | $this->headers = []; |
||
| 304 | |||
| 305 | return $this; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Removes one header. |
||
| 310 | * |
||
| 311 | * @param string $name |
||
| 312 | * |
||
| 313 | * @return self |
||
| 314 | */ |
||
| 315 | public function deleteHeader($name) |
||
| 316 | { |
||
| 317 | unset($this->headers[$name]); |
||
| 318 | |||
| 319 | return $this; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the language value. |
||
| 324 | * |
||
| 325 | * @return string $language |
||
| 326 | */ |
||
| 327 | public function getLanguage() |
||
| 328 | { |
||
| 329 | return $this->getHeader(self::HEADER_LANGUAGE); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Sets the language and the plural forms. |
||
| 334 | * |
||
| 335 | * @param string $language |
||
| 336 | * |
||
| 337 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
| 338 | * |
||
| 339 | * @return self |
||
| 340 | */ |
||
| 341 | public function setLanguage($language) |
||
| 342 | { |
||
| 343 | $this->setHeader(self::HEADER_LANGUAGE, trim($language)); |
||
| 344 | |||
| 345 | if (($info = Language::getById($language))) { |
||
| 346 | return $this->setPluralForms(count($info->categories), $info->formula); |
||
| 347 | } |
||
| 348 | |||
| 349 | throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language)); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Checks whether the language is empty or not. |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | public function hasLanguage() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set a new domain for this translations. |
||
| 366 | * |
||
| 367 | * @param string $domain |
||
| 368 | * |
||
| 369 | * @return self |
||
| 370 | */ |
||
| 371 | public function setDomain($domain) |
||
| 372 | { |
||
| 373 | $this->setHeader(self::HEADER_DOMAIN, trim($domain)); |
||
| 374 | |||
| 375 | return $this; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns the domain. |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getDomain() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Checks whether the domain is empty or not. |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function hasDomain() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Search for a specific translation. |
||
| 402 | * |
||
| 403 | * @param string|Translation $context The context of the translation or a translation instance |
||
| 404 | * @param string $original The original string |
||
| 405 | * |
||
| 406 | * @return Translation|false |
||
| 407 | */ |
||
| 408 | public function find($context, $original = '') |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Count all elements translated |
||
| 421 | * |
||
| 422 | * @return integer |
||
| 423 | */ |
||
| 424 | public function countTranslated() |
||
| 425 | { |
||
| 426 | $callback = function(Translation $v) |
||
| 427 | { |
||
| 428 | return ($v->hasTranslation()) ? $v->getTranslation() : null; |
||
| 429 | }; |
||
| 430 | |||
| 431 | return count(array_filter(get_object_vars($this), $callback)); |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Creates and insert/merges a new translation. |
||
| 436 | * |
||
| 437 | * @param string $context The translation context |
||
| 438 | * @param string $original The translation original string |
||
| 439 | * @param string $plural The translation original plural string |
||
| 440 | * |
||
| 441 | * @return Translation The translation created |
||
| 442 | */ |
||
| 443 | public function insert($context, $original, $plural = '') |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Merges this translations with other translations. |
||
| 450 | * |
||
| 451 | * @param Translations $translations The translations instance to merge with |
||
| 452 | * @param int $options |
||
| 453 | * |
||
| 454 | * @return self |
||
| 455 | */ |
||
| 456 | public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
||
| 463 | } |
||
| 464 |