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 |
||
| 19 | class Agreement |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Default language of the agreement. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $_language = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The directory where backups are stored |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $_backup_dir = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The name of the file where the agreement is stored |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $_file_name = 'agreement'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The name of the directory where the backup will be saved |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $_backupdir_name = 'agreements'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The name of the log table |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $_log_table_name = '{db_prefix}log_agreement_accept'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The database object |
||
| 58 | * |
||
| 59 | * @var Object |
||
| 60 | */ |
||
| 61 | protected $_db = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Everything starts here. |
||
| 65 | * |
||
| 66 | * @param string $language the wanted language of the agreement. |
||
| 67 | * @param string $backup_dir where to store the backup of the agreements. |
||
|
|
|||
| 68 | */ |
||
| 69 | public function __construct($language, $backup_dir = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Stores a text into the agreement file. |
||
| 82 | * It stores strictly on the *language* agreement, no fallback. |
||
| 83 | * If the language passed to the class is empty, then it uses agreement.txt. |
||
| 84 | * |
||
| 85 | * @param string $text the language of the agreement we want. |
||
| 86 | * @param bool $update_backup if store a copy of the text of the agreements. |
||
| 87 | */ |
||
| 88 | public function save($text, $update_backup = false) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Retrieves the plain text version of the agreement directly from |
||
| 110 | * the file that contains it. |
||
| 111 | * |
||
| 112 | * It uses the language, but if the localized version doesn't exist |
||
| 113 | * then it may return the english version. |
||
| 114 | * |
||
| 115 | * @param boolean $fallback if fallback to the English version (default true). |
||
| 116 | */ |
||
| 117 | public function getPlainText($fallback = true) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Retrieves the BBC-parsed version of the agreement. |
||
| 138 | * |
||
| 139 | * It uses the language, but if the localized version doesn't exist |
||
| 140 | * then it may return the english version. |
||
| 141 | * |
||
| 142 | * @param boolean $fallback if fallback to the English version (default true). |
||
| 143 | */ |
||
| 144 | public function getParsedText($fallback = true) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Retrieves the BBC-parsed version of the agreement. |
||
| 153 | * |
||
| 154 | * If the language passed to the class is empty, then it uses agreement.txt. |
||
| 155 | */ |
||
| 156 | public function isWritable() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Test if the user accepted the current agreement or not. |
||
| 165 | * |
||
| 166 | * @param int $id_member The id of the member |
||
| 167 | * @param string $version The date of the agreement |
||
| 168 | */ |
||
| 169 | public function checkAccepted($id_member, $version) |
||
| 184 | |||
| 185 | View Code Duplication | public function accept($id_member, $ip, $version) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Takes care of the edge-case of the default agreement that doesn't have |
||
| 211 | * the language in the name, and the fact that the admin panels loads it |
||
| 212 | * as an empty language. |
||
| 213 | */ |
||
| 214 | protected function normalizeLanguage() |
||
| 218 | |||
| 219 | protected function _backupId() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Creates a full backup of all the agreements. |
||
| 236 | * |
||
| 237 | * @param string $backup_id the name of the directory of the backup |
||
| 238 | * @return bool true if successful, false if failes to create the directory |
||
| 239 | */ |
||
| 240 | protected function _createBackup($backup_id) |
||
| 258 | } |
||
| 259 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.