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) |
||
103 | |||
104 | /** |
||
105 | * Creates a backup of the current version of the agreement/s. |
||
106 | * |
||
107 | * @return string|bool the backup_id if successful, false if creating the backup fails |
||
108 | */ |
||
109 | public function storeBackup() |
||
119 | |||
120 | /** |
||
121 | * Retrieves the plain text version of the agreement directly from |
||
122 | * the file that contains it. |
||
123 | * |
||
124 | * It uses the language, but if the localized version doesn't exist |
||
125 | * then it may return the english version. |
||
126 | * |
||
127 | * @param boolean $fallback if fallback to the English version (default true). |
||
128 | */ |
||
129 | public function getPlainText($fallback = true) |
||
147 | |||
148 | /** |
||
149 | * Retrieves the BBC-parsed version of the agreement. |
||
150 | * |
||
151 | * It uses the language, but if the localized version doesn't exist |
||
152 | * then it may return the english version. |
||
153 | * |
||
154 | * @param boolean $fallback if fallback to the English version (default true). |
||
155 | */ |
||
156 | public function getParsedText($fallback = true) |
||
162 | |||
163 | /** |
||
164 | * Retrieves the BBC-parsed version of the agreement. |
||
165 | * |
||
166 | * If the language passed to the class is empty, then it uses agreement.txt. |
||
167 | */ |
||
168 | public function isWritable() |
||
174 | |||
175 | /** |
||
176 | * Test if the user accepted the current agreement or not. |
||
177 | * |
||
178 | * @param int $id_member The id of the member |
||
179 | * @param string $version The date of the agreement |
||
180 | */ |
||
181 | public function checkAccepted($id_member, $version) |
||
196 | |||
197 | View Code Duplication | public function accept($id_member, $ip, $version) |
|
220 | |||
221 | /** |
||
222 | * Takes care of the edge-case of the default agreement that doesn't have |
||
223 | * the language in the name, and the fact that the admin panels loads it |
||
224 | * as an empty language. |
||
225 | */ |
||
226 | protected function normalizeLanguage() |
||
230 | |||
231 | protected function _backupId() |
||
245 | |||
246 | /** |
||
247 | * Creates a full backup of all the agreements. |
||
248 | * |
||
249 | * @param string $backup_id the name of the directory of the backup |
||
250 | * @return bool true if successful, false if failes to create the directory |
||
251 | */ |
||
252 | protected function _createBackup($backup_id) |
||
270 | } |
||
271 |
This check looks for
@param
annotations 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.