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 |
||
| 5 | trait Localization { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * \addtogroup Multilanguage Multilanguage |
||
| 9 | * \brief Methods to create a localized bot. |
||
| 10 | * @{ |
||
| 11 | */ |
||
| 12 | |||
| 13 | /** \brief Store the language for a multi-language bot */ |
||
| 14 | public $language; |
||
| 15 | |||
| 16 | /** \brief Store localization data */ |
||
| 17 | public $local; |
||
| 18 | |||
| 19 | /** \brief Table contaning bot users data in the sql database. */ |
||
| 20 | public $user_table = '"User"'; |
||
| 21 | |||
| 22 | /** \brief Name of the column that represents the user id in the sql database */ |
||
| 23 | public $id_column = 'chat_id'; |
||
| 24 | |||
| 25 | /** @} */ |
||
| 26 | |||
| 27 | /** |
||
| 28 | * \brief Get current user language from the database, and set it in $language. |
||
| 29 | * @param $default_language <i>Optional</i>. Default language to return in case of errors. |
||
| 30 | * @return Language set for the current user, $default_language on errors. |
||
| 31 | */ |
||
| 32 | public function getLanguageDatabase($default_language = 'en') { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * \brief Get current user language from redis, as a cache, and set it in language. |
||
| 84 | * \details Using redis database as cache, seeks the language in it, if there isn't |
||
| 85 | * then get the language from the sql database and store it (with default expiring of one day) in redis. |
||
| 86 | * It also change $language parameter of the bot to the language returned. |
||
| 87 | * @param $default_language <i>Optional</i>. Default language to return in case of errors. |
||
| 88 | * @param $expiring_time <i>Optional</i>. Set the expiring time for the language on redis each time it is took from the sql database. |
||
| 89 | * @return Language for the current user, $default_language on errors. |
||
| 90 | */ |
||
| 91 | public function getLanguageRedis($default_language = 'en', $expiring_time = '86400') : string { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * \brief Set the current user language in both redis, sql database and $language. |
||
| 120 | * \details Save it on database first, then create the expiring key on redis. |
||
| 121 | * @param $language The new language to set. |
||
| 122 | * @param $expiring_time <i>Optional</i>. Time for the language key in redis to expire. |
||
| 123 | * @return On sucess, return true, throw exception otherwise. |
||
| 124 | */ |
||
| 125 | public function setLanguageRedis($language, $expiring_time = '86400') { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * \brief Load localization files (JSON-serialized) from a folder and set them in $local variable. |
||
| 159 | * \details Save all localization files, saved as json format, from a directory and put the contents in $local variable. |
||
| 160 | * Each file will be saved into $local with the first two letters of the filename as the index. |
||
| 161 | * Access the english data as $this->local["en"]["Your key"]. |
||
| 162 | * File <code>./localization/en.json</code>: |
||
| 163 | * |
||
| 164 | * {"Hello_Msg": "Hello"} |
||
| 165 | * |
||
| 166 | * File <code>./localization/it.json</code>: |
||
| 167 | * |
||
| 168 | * {"Hello_Msg": "Ciao"} |
||
| 169 | * |
||
| 170 | * Usage in <code>processMessage()</code>: |
||
| 171 | * |
||
| 172 | * $sendMessage($this->local[$this->language]["Hello_Msg"]); |
||
| 173 | * |
||
| 174 | * @param $dir Directory where the localization files are saved. |
||
| 175 | */ |
||
| 176 | public function loadLocalization($dir = './localization') { |
||
| 206 | |||
| 207 | /** @} */ |
||
| 208 | } |
||
| 209 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: