Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MultilingualBehavior 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 MultilingualBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MultilingualBehavior extends Behavior |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Multilingual attributes |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | public $attributes; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Available languages |
||
| 23 | * It can be a simple array: array('fr', 'en') or an associative array: array('fr' => 'Français', 'en' => 'English') |
||
| 24 | * For associative arrays, only the keys will be used. |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | public $languages; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string the default language. |
||
| 31 | * Example: 'en'. |
||
| 32 | */ |
||
| 33 | public $defaultLanguage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string the name of the translation table |
||
| 37 | */ |
||
| 38 | public $tableName; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string the name of translation model class. |
||
| 42 | */ |
||
| 43 | public $langClassName; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string the name of the foreign key field of the translation table related to base model table. |
||
| 47 | */ |
||
| 48 | public $langForeignKey; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string the prefix of the localized attributes in the lang table. Here to avoid collisions in queries. |
||
| 52 | * In the translation table, the columns corresponding to the localized attributes have to be name like this: 'l_[name of the attribute]' |
||
| 53 | * and the id column (primary key) like this : 'l_id' |
||
| 54 | * Default to ''. |
||
| 55 | */ |
||
| 56 | public $localizedPrefix = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string the name of the lang field of the translation table. Default to 'language'. |
||
| 60 | */ |
||
| 61 | public $languageField = 'language'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var boolean if this property is set to true required validators will be applied to all translation models. |
||
| 65 | * Default to false. |
||
| 66 | */ |
||
| 67 | public $requireTranslations = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean whether to force deletion of the associated translations when a base model is deleted. |
||
| 71 | * Not needed if using foreign key with 'on delete cascade'. |
||
| 72 | * Default to true. |
||
| 73 | */ |
||
| 74 | public $forceDelete = true; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean whether to dynamically create translation model class. |
||
| 78 | * If true, the translation model class will be generated on runtime with the use of the eval() function so no additional php file is needed. |
||
| 79 | * See {@link createLangClass()} |
||
| 80 | * Default to true. |
||
| 81 | */ |
||
| 82 | public $dynamicLangClass = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean whether to abridge the language ID. |
||
| 86 | * Default to true. |
||
| 87 | */ |
||
| 88 | public $abridge = true; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string the name of the primary key field of the base model. Defaults to first value of Model::primaryKey. |
||
| 92 | */ |
||
| 93 | public $ownerPrimaryKey; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var boolean whether to check for existing translations on insert |
||
| 97 | * Default to false |
||
| 98 | */ |
||
| 99 | public $loadTranslationsOnInsert = false; |
||
| 100 | |||
| 101 | private $currentLanguage; |
||
| 102 | private $ownerClassName; |
||
| 103 | private $langClassShortName; |
||
| 104 | private $ownerClassShortName; |
||
| 105 | private $langAttributes = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array excluded validators |
||
| 109 | */ |
||
| 110 | private $excludedValidators = ['unique']; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritdoc |
||
| 114 | */ |
||
| 115 | 19 | public function events() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @inheritdoc |
||
| 128 | */ |
||
| 129 | 19 | public function attach($owner) |
|
| 229 | |||
| 230 | 19 | public function createLangClass() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Relation to model translations |
||
| 249 | * @return ActiveQuery |
||
| 250 | */ |
||
| 251 | 8 | public function getTranslations() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Relation to model translation |
||
| 258 | * @param $language |
||
| 259 | * @return ActiveQuery |
||
| 260 | */ |
||
| 261 | 8 | public function getTranslation($language = null) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Handle 'beforeValidate' event of the owner. |
||
| 270 | */ |
||
| 271 | 11 | public function beforeValidate() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Handle 'afterFind' event of the owner. |
||
| 280 | */ |
||
| 281 | 13 | public function afterFind() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Handle 'afterInsert' event of the owner. |
||
| 325 | */ |
||
| 326 | 5 | public function afterInsert() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Handle 'afterUpdate' event of the owner. |
||
| 342 | */ |
||
| 343 | 5 | public function afterUpdate() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Handle 'afterDelete' event of the owner. |
||
| 356 | */ |
||
| 357 | 2 | public function afterDelete() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param array $translations |
||
| 368 | */ |
||
| 369 | 8 | private function saveTranslations($translations = []) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritdoc |
||
| 406 | */ |
||
| 407 | 18 | public function canGetProperty($name, $checkVars = true) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @inheritdoc |
||
| 415 | */ |
||
| 416 | 10 | public function canSetProperty($name, $checkVars = true) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritdoc |
||
| 423 | */ |
||
| 424 | 17 | public function __get($name) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @inheritdoc |
||
| 438 | */ |
||
| 439 | 10 | public function __set($name, $value) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @inheritdoc |
||
| 453 | * @codeCoverageIgnore |
||
| 454 | */ |
||
| 455 | public function __isset($name) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Whether an attribute exists |
||
| 466 | * @param string $name the name of the attribute |
||
| 467 | * @return boolean |
||
| 468 | */ |
||
| 469 | 15 | public function hasLangAttribute($name) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $name the name of the attribute |
||
| 476 | * @return string the attribute value |
||
| 477 | */ |
||
| 478 | 15 | public function getLangAttribute($name) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $name the name of the attribute |
||
| 485 | * @param string $value the value of the attribute |
||
| 486 | */ |
||
| 487 | 19 | public function setLangAttribute($name, $value) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param $records |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | 6 | protected function indexByLanguage($records) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param $language |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | 19 | protected function getLanguageBaseName($language) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @param string $className |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | 19 | private function getShortClassName($className) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @return mixed|string |
||
| 526 | */ |
||
| 527 | 8 | public function getCurrentLanguage() |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @param $attribute |
||
| 534 | * @param $language |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | 19 | protected function getAttributeName($attribute, $language) |
|
| 542 | } |
||
| 543 |