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:
| 1 | <?php namespace XoopsModules\Smartobject; |
||
| 27 | class MlObject extends Smartobject\BaseSmartObject |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * SmartMlObject constructor. |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * If object is not new, change the control of the not-multilanguage fields |
||
| 40 | * |
||
| 41 | * We need to intercept this function from SmartObject because if the object is not new... |
||
| 42 | */ |
||
| 43 | // function getForm() { |
||
| 44 | |||
| 45 | //} |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Strip Multilanguage Fields |
||
| 49 | * |
||
| 50 | * Get rid of all the multilanguage fields to have an object with only global fields. |
||
| 51 | * This will be usefull when creating the ML object for the first time. Then we will be able |
||
| 52 | * to create translations. |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function stripMultilanguageFields() |
|
| 65 | |||
| 66 | View Code Duplication | public function stripNonMultilanguageFields() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Make non multilanguage fields read only |
||
| 80 | * |
||
| 81 | * This is used when we are creating/editing a translation. |
||
| 82 | * We only want to edit the multilanguag fields, not the global one. |
||
| 83 | */ |
||
| 84 | public function makeNonMLFieldReadOnly() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param bool $onlyUrl |
||
| 96 | * @param bool $withimage |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function getEditLanguageLink($onlyUrl = false, $withimage = true) |
||
| 105 | } |
||
| 106 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.