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:
Complex classes like donation_pages often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use donation_pages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class donation_pages extends main |
||
20 | { |
||
21 | /** |
||
22 | * Data for this entity |
||
23 | * |
||
24 | * @var array |
||
25 | * page_id |
||
26 | * page_title |
||
27 | * page_lang_id |
||
28 | * page_content |
||
29 | * page_content_bbcode_bitfield |
||
30 | * page_content_bbcode_uid |
||
31 | * page_content_bbcode_options |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected $data; |
||
35 | protected $dp_vars; |
||
36 | |||
37 | protected $config; |
||
38 | protected $donation_pages_table; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param \phpbb\config\config $config Config object |
||
44 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
45 | * @param \phpbb\user $user User object |
||
46 | * @param string $table_name Name of the table used to store data |
||
47 | * |
||
48 | * @access public |
||
49 | */ |
||
50 | View Code Duplication | public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $table_name) |
|
51 | { |
||
52 | $this->config = $config; |
||
53 | $this->db = $db; |
||
54 | $this->user = $user; |
||
55 | $this->donation_pages_table = $table_name; |
||
56 | parent::__construct( |
||
57 | $db, |
||
58 | $user, |
||
59 | 'PPDE_DP', |
||
60 | 'DONATION_PAGES', |
||
61 | $table_name, |
||
62 | array( |
||
63 | 'item_id' => array('name' => 'page_id', 'type' => 'integer'), |
||
64 | 'item_name' => array('name' => 'page_title', 'type' => 'string'), |
||
65 | 'item_lang_id' => array('name' => 'page_lang_id', 'type' => 'integer'), |
||
66 | 'item_content' => array('name' => 'page_content', 'type' => 'string'), |
||
67 | 'item_content_bbcode_bitfield' => array('name' => 'page_content_bbcode_bitfield', 'type' => 'string'), |
||
68 | 'item_content_bbcode_uid' => array('name' => 'page_content_bbcode_uid', 'type' => 'string'), |
||
69 | 'item_content_bbcode_options' => array('name' => 'page_content_bbcode_options', 'type' => 'integer'), |
||
70 | ) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get template vars |
||
76 | * |
||
77 | * @return array $this->dp_vars |
||
78 | * @access public |
||
79 | */ |
||
80 | public function get_vars() |
||
81 | { |
||
82 | $this->dp_vars = array( |
||
83 | 0 => array('var' => '{USER_ID}', |
||
84 | 'value' => $this->user->data['user_id'], |
||
85 | ), |
||
86 | 1 => array('var' => '{USERNAME}', |
||
87 | 'value' => $this->user->data['username'], |
||
88 | ), |
||
89 | 2 => array('var' => '{SITE_NAME}', |
||
90 | 'value' => $this->config['sitename'], |
||
91 | ), |
||
92 | 3 => array('var' => '{SITE_DESC}', |
||
93 | 'value' => $this->config['site_desc'], |
||
94 | ), |
||
95 | 4 => array('var' => '{BOARD_CONTACT}', |
||
96 | 'value' => $this->config['board_contact'], |
||
97 | ), |
||
98 | 5 => array('var' => '{BOARD_EMAIL}', |
||
99 | 'value' => $this->config['board_email'], |
||
100 | ), |
||
101 | 6 => array('var' => '{BOARD_SIG}', |
||
102 | 'value' => $this->config['board_email_sig'], |
||
103 | ), |
||
104 | ); |
||
105 | |||
106 | if ($this->is_in_admin()) |
||
107 | { |
||
108 | $this->add_predefined_lang_vars(); |
||
109 | } |
||
110 | |||
111 | return $this->dp_vars; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Add language key for donation pages Predefined vars |
||
116 | * |
||
117 | * @return null |
||
118 | * @access private |
||
119 | */ |
||
120 | private function add_predefined_lang_vars() |
||
121 | { |
||
122 | //Add language entries for displaying the vars |
||
123 | for ($i = 0, $size = sizeof($this->dp_vars); $i < $size; $i++) |
||
124 | { |
||
125 | $this->dp_vars[$i]['name'] = $this->user->lang[$this->lang_key_prefix . '_' . substr(substr($this->dp_vars[$i]['var'], 0, -1), 1)]; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get language id |
||
131 | * |
||
132 | * @return int Lang identifier |
||
133 | * @access public |
||
134 | */ |
||
135 | public function get_lang_id() |
||
136 | { |
||
137 | return (isset($this->data['page_lang_id'])) ? (int) $this->data['page_lang_id'] : 0; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Set Lang identifier |
||
142 | * |
||
143 | * @param int $lang |
||
144 | * |
||
145 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
146 | * @access public |
||
147 | */ |
||
148 | public function set_lang_id($lang) |
||
149 | { |
||
150 | // Set the lang_id on our data array |
||
151 | $this->data['page_lang_id'] = (int) $lang; |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Get message for edit |
||
158 | * |
||
159 | * @return string |
||
160 | * @access public |
||
161 | */ |
||
162 | public function get_message_for_edit() |
||
163 | { |
||
164 | // Use defaults if these haven't been set yet |
||
165 | $message = (isset($this->data['page_content'])) ? $this->data['page_content'] : ''; |
||
166 | $uid = (isset($this->data['page_content_bbcode_uid'])) ? $this->data['page_content_bbcode_uid'] : ''; |
||
167 | $options = (isset($this->data['page_content_bbcode_options'])) ? (int) $this->data['page_content_bbcode_options'] : 0; |
||
168 | |||
169 | // Generate for edit |
||
170 | $message_data = generate_text_for_edit($message, $uid, $options); |
||
171 | |||
172 | return $message_data['text']; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get message for display |
||
177 | * |
||
178 | * @param bool $censor_text True to censor the text (Default: true) |
||
179 | * |
||
180 | * @return string |
||
181 | * @access public |
||
182 | */ |
||
183 | public function get_message_for_display($censor_text = true) |
||
184 | { |
||
185 | // If these haven't been set yet; use defaults |
||
186 | $message = (isset($this->data['page_content'])) ? $this->data['page_content'] : ''; |
||
187 | $uid = (isset($this->data['page_content_bbcode_uid'])) ? $this->data['page_content_bbcode_uid'] : ''; |
||
188 | $bitfield = (isset($this->data['page_content_bbcode_bitfield'])) ? $this->data['page_content_bbcode_bitfield'] : ''; |
||
189 | $options = (isset($this->data['page_content_bbcode_options'])) ? (int) $this->data['page_content_bbcode_options'] : 0; |
||
190 | |||
191 | // Generate for display |
||
192 | return generate_text_for_display($message, $uid, $bitfield, $options, $censor_text); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Replace template vars in the message |
||
197 | * |
||
198 | * @param string $message |
||
199 | * |
||
200 | * @return string |
||
201 | * @access public |
||
202 | */ |
||
203 | public function replace_template_vars($message) |
||
204 | { |
||
205 | $tpl_ary = array(); |
||
206 | for ($i = 0, $size = sizeof($this->dp_vars); $i < $size; $i++) |
||
207 | { |
||
208 | $tpl_ary[$this->dp_vars[$i]['var']] = $this->dp_vars[$i]['value']; |
||
209 | } |
||
210 | |||
211 | return str_replace(array_keys($tpl_ary), array_values($tpl_ary), $message); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Enable bbcode on the message |
||
216 | * |
||
217 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
218 | * @access public |
||
219 | */ |
||
220 | public function message_enable_bbcode() |
||
221 | { |
||
222 | $this->set_message_option(OPTION_FLAG_BBCODE); |
||
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Set option helper |
||
229 | * |
||
230 | * @param int $option_value Value of the option |
||
231 | * @param bool $negate Negate (unset) option (Default: False) |
||
232 | * @param bool $reparse_message Reparse the message after setting option (Default: True) |
||
233 | * |
||
234 | * @return null |
||
235 | * @access protected |
||
236 | */ |
||
237 | protected function set_message_option($option_value, $negate = false, $reparse_message = true) |
||
238 | { |
||
239 | // Set item_text_bbcode_options to 0 if it does not yet exist |
||
240 | $this->data['page_content_bbcode_options'] = (isset($this->data['page_content_bbcode_options'])) ? $this->data['page_content_bbcode_options'] : 0; |
||
241 | |||
242 | // If we're setting the option and the option is not already set |
||
243 | if (!$negate && !($this->data['page_content_bbcode_options'] & $option_value)) |
||
244 | { |
||
245 | // Add the option to the options |
||
246 | $this->data['page_content_bbcode_options'] += $option_value; |
||
247 | } |
||
248 | |||
249 | // If we're unsetting the option and the option is already set |
||
250 | if ($negate && $this->data['page_content_bbcode_options'] & $option_value) |
||
251 | { |
||
252 | // Subtract the option from the options |
||
253 | $this->data['page_content_bbcode_options'] -= $option_value; |
||
254 | } |
||
255 | |||
256 | // Reparse the message |
||
257 | if ($reparse_message && !empty($this->data['page_content'])) |
||
258 | { |
||
259 | $message = $this->data['page_content']; |
||
260 | |||
261 | decode_message($message, $this->data['page_content_bbcode_uid']); |
||
262 | |||
263 | $this->set_message($message); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Set message |
||
269 | * |
||
270 | * @param string $message |
||
271 | * |
||
272 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
273 | * @access public |
||
274 | */ |
||
275 | public function set_message($message) |
||
276 | { |
||
277 | // Prepare the text for storage |
||
278 | $uid = $bitfield = $flags = ''; |
||
279 | generate_text_for_storage($message, $uid, $bitfield, $flags, $this->message_bbcode_enabled(), $this->message_magic_url_enabled(), $this->message_smilies_enabled()); |
||
280 | |||
281 | // Set the message to our data array |
||
282 | $this->data['page_content'] = $message; |
||
283 | $this->data['page_content_bbcode_uid'] = $uid; |
||
284 | $this->data['page_content_bbcode_bitfield'] = $bitfield; |
||
285 | |||
286 | // Flags are already set |
||
287 | |||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Check if bbcode is enabled on the message |
||
293 | * |
||
294 | * @return bool |
||
295 | * @access public |
||
296 | */ |
||
297 | public function message_bbcode_enabled() |
||
298 | { |
||
299 | return ($this->data['page_content_bbcode_options'] & OPTION_FLAG_BBCODE); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Check if magic_url is enabled on the message |
||
304 | * |
||
305 | * @return bool |
||
306 | * @access public |
||
307 | */ |
||
308 | public function message_magic_url_enabled() |
||
309 | { |
||
310 | return ($this->data['page_content_bbcode_options'] & OPTION_FLAG_LINKS); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Check if smilies are enabled on the message |
||
315 | * |
||
316 | * @return bool |
||
317 | * @access public |
||
318 | */ |
||
319 | public function message_smilies_enabled() |
||
320 | { |
||
321 | return ($this->data['page_content_bbcode_options'] & OPTION_FLAG_SMILIES); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Disable bbcode on the message |
||
326 | * |
||
327 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
328 | * @access public |
||
329 | */ |
||
330 | public function message_disable_bbcode() |
||
331 | { |
||
332 | $this->set_message_option(OPTION_FLAG_BBCODE, true); |
||
333 | |||
334 | return $this; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Enable magic url on the message |
||
339 | * |
||
340 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
341 | * @access public |
||
342 | */ |
||
343 | public function message_enable_magic_url() |
||
344 | { |
||
345 | $this->set_message_option(OPTION_FLAG_LINKS); |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Disable magic url on the message |
||
352 | * |
||
353 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
354 | * @access public |
||
355 | */ |
||
356 | public function message_disable_magic_url() |
||
357 | { |
||
358 | $this->set_message_option(OPTION_FLAG_LINKS, true); |
||
359 | |||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Enable smilies on the message |
||
365 | * |
||
366 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
367 | * @access public |
||
368 | */ |
||
369 | public function message_enable_smilies() |
||
370 | { |
||
371 | $this->set_message_option(OPTION_FLAG_SMILIES); |
||
372 | |||
373 | return $this; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Disable smilies on the message |
||
378 | * |
||
379 | * @return donation_pages $this object for chaining calls; load()->set()->save() |
||
380 | * @access public |
||
381 | */ |
||
382 | public function message_disable_smilies() |
||
383 | { |
||
384 | $this->set_message_option(OPTION_FLAG_SMILIES, true); |
||
385 | |||
386 | return $this; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * SQL Query to return the ID of selected donation page |
||
391 | * |
||
392 | * @return string |
||
393 | * @access public |
||
394 | */ |
||
395 | public function build_sql_data_exists() |
||
396 | { |
||
397 | return 'SELECT page_id |
||
398 | FROM ' . $this->donation_pages_table . " |
||
399 | WHERE page_title = '" . $this->db->sql_escape($this->data['page_title']) . "' |
||
400 | AND page_lang_id = " . (int) $this->data['page_lang_id']; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Check if required field are set |
||
405 | * |
||
406 | * @return bool |
||
407 | * @access public |
||
408 | */ |
||
409 | public function check_required_field() |
||
413 | } |
||
414 |