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 |
||
18 | class main_controller |
||
19 | { |
||
20 | /** @var \phpbb\config\config */ |
||
21 | protected $config; |
||
22 | /** @var \phpbb\controller\helper */ |
||
23 | protected $helper; |
||
24 | /** @var \phpbb\template\template */ |
||
25 | protected $template; |
||
26 | /** @var \phpbb\user */ |
||
27 | protected $user; |
||
28 | /** @var string phpBB root path */ |
||
29 | protected $root_path; |
||
30 | /** @var string phpEx */ |
||
31 | protected $php_ext; |
||
32 | |||
33 | /** @var \phpbb\request\request */ |
||
34 | private $request; |
||
35 | |||
36 | /** @var \phpbb\db\driver\driver_interface */ |
||
37 | private $db; |
||
38 | |||
39 | /** @var \phpbb\auth\auth */ |
||
40 | private $auth; |
||
41 | |||
42 | /** @var \phpbb\log\log */ |
||
43 | private $log; |
||
44 | |||
45 | /** @var \paul999\ajaxshoutbox\actions\Delete */ |
||
46 | private $delete; |
||
47 | |||
48 | /** @var string */ |
||
49 | private $table; |
||
50 | |||
51 | /** @var string */ |
||
52 | private $usertable; |
||
53 | |||
54 | /** |
||
55 | * @param \phpbb\config\config $config |
||
56 | * @param \phpbb\controller\helper $helper |
||
57 | * @param \phpbb\template\template $template |
||
58 | * @param \phpbb\user $user |
||
59 | * @param \phpbb\request\request $request |
||
60 | * @param \phpbb\db\driver\driver_interface $db |
||
61 | * @param \phpbb\auth\auth $auth |
||
62 | * @param \phpbb\log\log $log |
||
63 | * @param \paul999\ajaxshoutbox\actions\delete $delete |
||
64 | * @param string $root_path |
||
65 | * @param string $php_ext |
||
66 | * @param string $table |
||
67 | * @param string $usertable |
||
68 | */ |
||
69 | public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $helper, |
||
91 | |||
92 | /** |
||
93 | * Validate the push connection with shoutbox-app.com |
||
94 | * |
||
95 | * @param $id |
||
96 | * |
||
97 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
98 | */ |
||
99 | public function validate($id) |
||
125 | |||
126 | /** |
||
127 | * Post a new message to the shoutbox. |
||
128 | * |
||
129 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
130 | */ |
||
131 | public function post() |
||
182 | |||
183 | /** |
||
184 | * Delete a post from the client. |
||
185 | * |
||
186 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
187 | */ |
||
188 | public function delete() |
||
212 | |||
213 | /** |
||
214 | * Get the last 10 shouts |
||
215 | * |
||
216 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
217 | */ |
||
218 | public function getAll() |
||
235 | |||
236 | /** |
||
237 | * Get all shouts since a specific shout ID. |
||
238 | * |
||
239 | * @param int $id Last selected ID. |
||
240 | * |
||
241 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
242 | */ |
||
243 | View Code Duplication | public function getAfter($id) |
|
264 | |||
265 | /** |
||
266 | * Get 10 shouts before the current shout ID. |
||
267 | * |
||
268 | * @param $id |
||
269 | * |
||
270 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
271 | */ |
||
272 | View Code Duplication | public function getBefore($id) |
|
293 | |||
294 | /** |
||
295 | * Loop over a SQL result set, and generate a JSON array based on the post data. |
||
296 | * |
||
297 | * @param mixed $result return the data for the posts |
||
298 | * @param bool $reverse |
||
299 | * |
||
300 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
301 | */ |
||
302 | private function returnPosts($result, $reverse = true) |
||
314 | |||
315 | /** |
||
316 | * Generate a array with the specific post for this shout. |
||
317 | * |
||
318 | * @param array $row Input row |
||
319 | * |
||
320 | * @return array output |
||
321 | */ |
||
322 | private function getPost($row) |
||
323 | { |
||
324 | if (!defined('PHPBB_USE_BOARD_URL_PATH')) |
||
325 | { |
||
326 | define('PHPBB_USE_BOARD_URL_PATH', true); // Require full URL to smilies. |
||
327 | } |
||
328 | |||
329 | $text = generate_text_for_display( |
||
330 | $row['post_message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options'] |
||
331 | ); |
||
332 | |||
333 | $username = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']); |
||
334 | $username = str_replace('./../../', generate_board_url() . '/', $username); // Fix paths |
||
335 | $username = str_replace('./../', generate_board_url() . '/', $username); // Fix paths |
||
336 | |||
337 | $result = array( |
||
338 | 'id' => $row['shout_id'], |
||
339 | 'user' => $username, |
||
340 | 'date' => $this->user->format_date($row['post_time'], $this->user->data['user_ajaxshoutbox_format']), |
||
341 | 'message' => $text, |
||
342 | 'delete' => ($this->auth->acl_get('m_shoutbox_delete') || ($this->auth->acl_get('u_shoutbox_delete') && $row['user_id'] == $this->user->data['user_id'])), |
||
343 | ); |
||
344 | |||
345 | return array_merge($result, $this->add_form_key('ajaxshoutbox_delete_' . $row['shout_id'])); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Send a error to the user. |
||
350 | * |
||
351 | * Important: phpBB (<= 3.1.2) handles non 200 status as error. |
||
352 | * Due to the way this is implemented, phpBB will display the browser |
||
353 | * generated error, instead of the user returned error. |
||
354 | * This method will result in a 200 OK, but the correct status is in |
||
355 | * the JsonResponse.status. |
||
356 | * |
||
357 | * @param string $title |
||
358 | * @param string $message |
||
359 | * @param integer $status |
||
360 | * |
||
361 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
362 | */ |
||
363 | private function error($title, $message, $status) |
||
373 | |||
374 | /** |
||
375 | * Add a secret token and returns it as array with creation_time and form_token. |
||
376 | * |
||
377 | * Based on phpBB's add_form_key. Compatible with check_form_key. |
||
378 | * |
||
379 | * IMPORTANT: The original event is not included, because the form is build before the event, |
||
380 | * while this function returns the (Possible modified) data after the event. |
||
381 | * |
||
382 | * @param string $form_name The name of the form; has to match the name used in check_form_key, otherwise no |
||
383 | * restrictions apply |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | function add_form_key($form_name) |
||
398 | } |
||
399 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.