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 admin_helper |
||
19 | { |
||
20 | /** @var \phpbb\user */ |
||
21 | protected $user; |
||
22 | |||
23 | /** @var \phpbb\language\language */ |
||
24 | protected $language; |
||
25 | |||
26 | /** @var \phpbb\template\template */ |
||
27 | protected $template; |
||
28 | |||
29 | /** @var \phpbb\log\log */ |
||
30 | protected $log; |
||
31 | |||
32 | /** @var \phpbb\ads\location\manager */ |
||
33 | protected $location_manager; |
||
34 | |||
35 | /** @var string root_path */ |
||
36 | protected $root_path; |
||
37 | |||
38 | /** @var string php_ext */ |
||
39 | protected $php_ext; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param \phpbb\user $user User object |
||
45 | * @param \phpbb\language\language $language Language object |
||
46 | * @param \phpbb\template\template $template Template object |
||
47 | * @param \phpbb\log\log $log The phpBB log system |
||
48 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
49 | * @param string $root_path phpBB root path |
||
50 | * @param string $php_ext PHP extension |
||
51 | */ |
||
52 | 20 | View Code Duplication | public function __construct(\phpbb\user $user, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext) |
62 | |||
63 | /** |
||
64 | * Assign ad data for ACP form template. |
||
65 | * |
||
66 | * @param array $data Ad data |
||
67 | * @param array $errors Validation errors |
||
68 | */ |
||
69 | 5 | public function assign_data($data, $errors) |
|
70 | { |
||
71 | 5 | $this->assign_locations($data['ad_locations']); |
|
72 | |||
73 | 5 | $errors = array_map(array($this->language, 'lang'), $errors); |
|
74 | 5 | $this->template->assign_vars(array( |
|
75 | 5 | 'S_ERROR' => (bool) count($errors), |
|
76 | 5 | 'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '', |
|
77 | |||
78 | 5 | 'AD_NAME' => $data['ad_name'], |
|
79 | 5 | 'AD_NOTE' => $data['ad_note'], |
|
80 | 5 | 'AD_CODE' => $data['ad_code'], |
|
81 | 5 | 'AD_ENABLED' => $data['ad_enabled'], |
|
82 | 5 | 'AD_END_DATE' => $this->prepare_end_date($data['ad_end_date']), |
|
83 | 5 | 'AD_PRIORITY' => $data['ad_priority'], |
|
84 | 5 | 'AD_VIEWS_LIMIT' => $data['ad_views_limit'], |
|
85 | 5 | 'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'], |
|
86 | 5 | 'AD_OWNER' => $this->prepare_ad_owner($data['ad_owner']), |
|
87 | 5 | )); |
|
88 | 5 | } |
|
89 | |||
90 | /** |
||
91 | * Assign template locations data to the template. |
||
92 | * |
||
93 | * @param mixed $ad_locations The form data or nothing. |
||
94 | * @return void |
||
95 | */ |
||
96 | 7 | public function assign_locations($ad_locations = false) |
|
97 | { |
||
98 | 7 | View Code Duplication | foreach ($this->location_manager->get_all_locations() as $location_id => $location_data) |
99 | { |
||
100 | 2 | $this->template->assign_block_vars('ad_locations', array( |
|
101 | 2 | 'LOCATION_ID' => $location_id, |
|
102 | 2 | 'LOCATION_DESC' => $location_data['desc'], |
|
103 | 2 | 'LOCATION_NAME' => $location_data['name'], |
|
104 | 2 | 'S_SELECTED' => $ad_locations ? in_array($location_id, $ad_locations) : false, |
|
105 | 2 | )); |
|
106 | 7 | } |
|
107 | 7 | } |
|
108 | |||
109 | /** |
||
110 | * Log action |
||
111 | * |
||
112 | * @param string $action Performed action in uppercase |
||
113 | * @param string $ad_name Advertisement name |
||
114 | * @return void |
||
115 | */ |
||
116 | 1 | public function log($action, $ad_name) |
|
117 | { |
||
118 | 1 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name)); |
|
119 | 1 | } |
|
120 | |||
121 | /** |
||
122 | * Get "Find username" URL to easily look for ad owner. |
||
123 | * |
||
124 | * @return string Find username URL |
||
125 | */ |
||
126 | 1 | public function get_find_username_link() |
|
127 | { |
||
128 | 1 | return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&form=acp_admanagement_add&field=ad_owner&select_single=true'); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Prepare end date for display |
||
133 | * |
||
134 | * @param mixed $end_date End date. |
||
135 | * @return string End date prepared for display. |
||
136 | */ |
||
137 | 9 | public function prepare_end_date($end_date) |
|
151 | |||
152 | /** |
||
153 | * Is an ad expired? |
||
154 | * |
||
155 | * @param array $row Advertisement data |
||
156 | * @return bool True if expired, false otherwise |
||
157 | */ |
||
158 | 7 | public function is_expired($row) |
|
177 | |||
178 | /** |
||
179 | * Prepare ad owner for display. Method takes user_id |
||
180 | * of the ad owner and returns his/her username. |
||
181 | * |
||
182 | * @param int $ad_owner User ID |
||
183 | * @return string Username belonging to $ad_owner. |
||
184 | */ |
||
185 | 5 | protected function prepare_ad_owner($ad_owner) |
|
201 | } |
||
202 |