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 |
||
24 | class admin_overview_controller extends admin_main |
||
25 | { |
||
26 | protected $auth; |
||
27 | protected $cache; |
||
28 | protected $config; |
||
29 | protected $ppde_controller_main; |
||
30 | protected $ppde_controller_transactions; |
||
31 | protected $php_ext; |
||
32 | |||
33 | protected $ext_name; |
||
34 | protected $ext_meta = array(); |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param \phpbb\auth\auth $auth Authentication object |
||
40 | * @param \phpbb\cache\service $cache Cache object |
||
41 | * @param \phpbb\config\config $config Config object |
||
42 | * @param \phpbb\log\log $log The phpBB log system |
||
43 | * @param \skouat\ppde\controller\main_controller $ppde_controller_main Main controller object |
||
44 | * @param \skouat\ppde\controller\admin_transactions_controller $ppde_controller_transactions Admin transactions controller object |
||
45 | * @param \phpbb\request\request $request Request object |
||
46 | * @param \phpbb\template\template $template Template object |
||
47 | * @param \phpbb\user $user User object |
||
48 | * @param string $php_ext phpEx |
||
49 | * |
||
50 | * @access public |
||
51 | */ |
||
52 | public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\log\log $log, \skouat\ppde\controller\main_controller $ppde_controller_main, \skouat\ppde\controller\admin_transactions_controller $ppde_controller_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $php_ext) |
||
70 | |||
71 | /** |
||
72 | * Display the overview page |
||
73 | * |
||
74 | * @param string $action Action name |
||
75 | * |
||
76 | * @return null |
||
77 | * @access public |
||
78 | */ |
||
79 | public function display_overview($action) |
||
80 | { |
||
81 | $this->ppde_controller_main->first_start(); |
||
82 | |||
83 | $this->do_action($action); |
||
84 | |||
85 | //Load metadata for this extension |
||
86 | $this->ext_meta = $this->ppde_controller_main->load_metadata(); |
||
87 | |||
88 | // Check if a new version is available |
||
89 | $this->obtain_last_version(); |
||
90 | |||
91 | // Set output block vars for display in the template |
||
92 | $this->template->assign_vars(array( |
||
93 | 'ANONYMOUS_DONORS_COUNT' => $this->config['ppde_anonymous_donors_count'], |
||
94 | 'ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count'), |
||
95 | 'INFO_CURL' => $this->config['ppde_curl_detected'] ? $this->user->lang('INFO_CURL_VERSION', $this->config['ppde_curl_version'], $this->config['ppde_curl_ssl_version']) : $this->user->lang['INFO_NOT_DETECTED'], |
||
96 | 'INFO_FSOCKOPEN' => $this->config['ppde_fsock_detected'] ? $this->user->lang['INFO_DETECTED'] : $this->user->lang['INFO_NOT_DETECTED'], |
||
97 | 'KNOWN_DONORS_COUNT' => $this->config['ppde_known_donors_count'], |
||
98 | 'KNOWN_DONORS_PER_DAY' => $this->per_day_stats('ppde_known_donors_count'), |
||
99 | 'L_PPDE_INSTALL_DATE' => $this->user->lang('PPDE_INSTALL_DATE', $this->ext_meta['extra']['display-name']), |
||
100 | 'L_PPDE_VERSION' => $this->user->lang('PPDE_VERSION', $this->ext_meta['extra']['display-name']), |
||
101 | 'PPDE_INSTALL_DATE' => $this->user->format_date($this->config['ppde_install_date']), |
||
102 | 'PPDE_VERSION' => $this->ext_meta['version'], |
||
103 | 'S_ACTION_OPTIONS' => ($this->auth->acl_get('a_ppde_manage')) ? true : false, |
||
104 | 'S_CURL' => $this->config['ppde_curl_detected'], |
||
105 | 'S_FSOCKOPEN' => $this->config['ppde_fsock_detected'], |
||
106 | 'TRANSACTIONS_COUNT' => $this->config['ppde_transactions_count'], |
||
107 | 'TRANSACTIONS_PER_DAY' => $this->per_day_stats('ppde_transactions_count'), |
||
108 | 'U_PPDE_MORE_INFORMATION' => append_sid("index.$this->php_ext", 'i=acp_extensions&mode=main&action=details&ext_name=' . urlencode($this->ext_meta['name'])), |
||
109 | 'U_PPDE_VERSIONCHECK_FORCE' => $this->u_action . '&versioncheck_force=1', |
||
110 | 'U_ACTION' => $this->u_action, |
||
111 | )); |
||
112 | |||
113 | if ($this->ppde_controller_main->use_sandbox()) |
||
114 | { |
||
115 | // Set output block vars for display in the template |
||
116 | $this->template->assign_vars(array( |
||
117 | 'S_IPN_TEST' => true, |
||
118 | 'SANDBOX_ANONYMOUS_DONORS_COUNT' => $this->config['ppde_anonymous_donors_count_ipn'], |
||
119 | 'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'), |
||
120 | 'SANDBOX_KNOWN_DONORS_COUNT' => $this->config['ppde_known_donors_count_ipn'], |
||
121 | 'SANDBOX_KNOWN_DONORS_PER_DAY' => $this->per_day_stats('ppde_known_donors_count_ipn'), |
||
122 | 'SANDBOX_TRANSACTIONS_COUNT' => $this->config['ppde_transactions_count_ipn'], |
||
123 | 'SANDBOX_TRANSACTIONS_PER_DAY' => $this->per_day_stats('ppde_transactions_count_ipn'), |
||
124 | )); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Do action regarding the value of $action |
||
130 | * |
||
131 | * @param string $action Requested action |
||
132 | * |
||
133 | * @return null |
||
134 | * @access private |
||
135 | */ |
||
136 | private function do_action($action) |
||
137 | { |
||
138 | if ($action) |
||
139 | { |
||
140 | if (!confirm_box(true)) |
||
141 | { |
||
142 | $this->display_confirm($action); |
||
143 | } |
||
144 | else |
||
145 | { |
||
146 | $this->exec_action($action); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Display confirm box |
||
153 | * |
||
154 | * @param string $action Requested action |
||
155 | * |
||
156 | * @return null |
||
157 | * @access private |
||
158 | */ |
||
159 | private function display_confirm($action) |
||
191 | |||
192 | /** |
||
193 | * @param string $action Requested action |
||
194 | * |
||
195 | * @return null |
||
196 | * @access private |
||
197 | */ |
||
198 | private function exec_action($action) |
||
226 | |||
227 | /** |
||
228 | * Obtains the last version for this extension |
||
229 | * |
||
230 | * @return null |
||
231 | * @access private |
||
232 | */ |
||
233 | private function obtain_last_version() |
||
234 | { |
||
235 | try |
||
236 | { |
||
237 | if (!isset($this->ext_meta['extra']['version-check'])) |
||
238 | { |
||
239 | throw new \RuntimeException($this->user->lang('PPDE_NO_VERSIONCHECK'), 1); |
||
240 | } |
||
241 | |||
242 | $version_check = $this->ext_meta['extra']['version-check']; |
||
243 | |||
244 | $version_helper = new \phpbb\version_helper($this->cache, $this->config, new \phpbb\file_downloader(), $this->user); |
||
245 | $version_helper->set_current_version($this->ext_meta['version']); |
||
246 | $version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename']); |
||
247 | $version_helper->force_stability($this->config['extension_force_unstable'] ? 'unstable' : null); |
||
248 | |||
249 | $recheck = $this->request->variable('versioncheck_force', false); |
||
250 | $s_up_to_date = $version_helper->get_suggested_updates($recheck); |
||
251 | |||
252 | $this->template->assign_vars(array( |
||
253 | 'S_UP_TO_DATE' => empty($s_up_to_date), |
||
254 | 'S_VERSIONCHECK' => true, |
||
255 | 'UP_TO_DATE_MSG' => $this->user->lang('PPDE_NOT_UP_TO_DATE', $this->ext_meta['extra']['display-name']), |
||
256 | )); |
||
257 | } |
||
258 | catch (\RuntimeException $e) |
||
259 | { |
||
260 | $this->template->assign_vars(array( |
||
261 | 'S_VERSIONCHECK_STATUS' => $e->getCode(), |
||
262 | 'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $this->user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '', |
||
263 | )); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Returns the percent of items (transactions, donors) per day |
||
269 | * |
||
270 | * @param string $config_name |
||
271 | * |
||
272 | * @return string |
||
273 | * @access private |
||
274 | */ |
||
275 | private function per_day_stats($config_name) |
||
279 | |||
280 | /** |
||
281 | * Returns the number of days from the date of installation of the extension. |
||
282 | * |
||
283 | * @return float |
||
284 | * @access private |
||
285 | */ |
||
286 | private function get_install_days() |
||
290 | } |
||
291 |