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 |
||
23 | trait Language |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * \addtogroup Localization Localization |
||
28 | * \brief Create a localized bot. |
||
29 | * \details Using both a sql database and a redis database you can develop a localized bot with just a small impact on the performance. |
||
30 | * By default the sql database will store the language permanently in a table which name is defined in $user_table. |
||
31 | * The redis database will cache the language for the chat_id for a variable time. |
||
32 | * You can use only the sql database if a redis database is not accessible. |
||
33 | * These methods will treat groups as User so they will be stored in the table as normal user does. |
||
34 | * @{ |
||
35 | */ |
||
36 | |||
37 | /** \brief Stores the language for a multi-language bot */ |
||
38 | public $language; |
||
39 | |||
40 | /** PDO connection to the database. */ |
||
41 | public $pdo; |
||
42 | |||
43 | /** \brief Table containing bot users data into database. */ |
||
44 | public $user_table = '"User"'; |
||
45 | |||
46 | /** \brief Name of the column that represents the user ID into database */ |
||
47 | public $id_column = 'chat_id'; |
||
48 | |||
49 | /** |
||
50 | * \brief Get current user's language from the database, and set it in $language. |
||
51 | * @param string $default_language <i>Optional</i>. Default language to return in case of errors. |
||
52 | * @return string Language set for the current user, $default_language on errors. |
||
53 | */ |
||
54 | public function getLanguageDatabase(string $default_language = 'en') |
||
85 | |||
86 | /** |
||
87 | * \brief Set the current user language in database and internally. |
||
88 | * \details Save it into database first. |
||
89 | * @param string $language The language to set. |
||
90 | * @return bool On sucess, return true, throws exception otherwise. |
||
91 | */ |
||
92 | public function setLanguageDatabase(string $language) : bool |
||
116 | |||
117 | /** |
||
118 | * \brief Get current user language from Redis (as a cache) and set it in language. |
||
119 | * \details Using Redis as cache, check for the language. On failure, get the language |
||
120 | * from the database and store it (with default expiring time of one day) in Redis. |
||
121 | * |
||
122 | * It also change $language parameter of the bot to the language returned. |
||
123 | * @param string $default_language <i>Optional</i>. Default language to return in case of errors. |
||
124 | * @param int $expiring_time <i>Optional</i>. Set the expiring time for the language on |
||
125 | * redis each time it is took from the sql database. |
||
126 | * @return string Language for the current user, $default_language on errors. |
||
127 | */ |
||
128 | public function getLanguageRedis(string $default_language = 'en', int $expiring_time = 86400) : string |
||
148 | |||
149 | /** |
||
150 | * \brief Set the current user language in both Redis, database and internally. |
||
151 | * \details Save it into database first, then create the expiring key on Redis. |
||
152 | * @param string $language The language to set. |
||
153 | * @param int $expiring_time <i>Optional</i>. Time for the language key in redis to expire. |
||
154 | * @return bool On sucess, return true, throws exception otherwise. |
||
155 | */ |
||
156 | public function setLanguageRedis(string $language, int $expiring_time = 86400) : bool |
||
172 | |||
173 | /** @} */ |
||
174 | } |
||
175 |
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: