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 ParentEmail 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 ParentEmail, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ParentEmail extends MY_Controller |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * List of accepted params |
||
29 | * @var array |
||
30 | */ |
||
31 | public $accepted_params = [ |
||
32 | 'name', |
||
33 | 'from', |
||
34 | 'from_email', |
||
35 | 'theme', |
||
36 | 'type', |
||
37 | 'user_message', |
||
38 | 'user_message_active', |
||
39 | 'admin_message', |
||
40 | 'admin_message_active', |
||
41 | 'admin_email', |
||
42 | 'description', |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Attachment file |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $attachment; |
||
50 | |||
51 | /** |
||
52 | * Array of data |
||
53 | * @var array |
||
54 | */ |
||
55 | public $data_model = []; |
||
56 | |||
57 | /** |
||
58 | * Array of errors |
||
59 | * @var array |
||
60 | */ |
||
61 | public $errors = []; |
||
62 | |||
63 | /** |
||
64 | * Email sender name |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $from; |
||
68 | |||
69 | /** |
||
70 | * Email sender email address |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $from_email; |
||
74 | |||
75 | /** |
||
76 | * Server path to Sendmail |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $mailpath; |
||
80 | |||
81 | /** |
||
82 | * Email message |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $message; |
||
86 | |||
87 | /** |
||
88 | * Mail port |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $port; |
||
92 | |||
93 | /** |
||
94 | * Mail protocol |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $protocol; |
||
98 | |||
99 | /** |
||
100 | * Receiver email |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $send_to; |
||
104 | |||
105 | /** |
||
106 | * Email theme |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $theme; |
||
110 | |||
111 | /** |
||
112 | * Mail content type |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $type; |
||
116 | |||
117 | public function __construct() { |
||
122 | |||
123 | /** |
||
124 | * @param int $template_id |
||
125 | * @param string $variable |
||
126 | * @param string $variableValue |
||
127 | * @param string $locale |
||
128 | * @return object |
||
129 | */ |
||
130 | public function addVariable($template_id, $variable, $variableValue, $locale) { |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | * @param array $data keys from list: |
||
137 | * 'name', |
||
|
|||
138 | * 'from', |
||
139 | * 'from_email', |
||
140 | * 'theme', |
||
141 | * 'type', |
||
142 | * 'user_message', |
||
143 | * 'user_message_active', |
||
144 | * 'admin_message', |
||
145 | * 'admin_message_active', |
||
146 | * 'admin_email', |
||
147 | * 'description' |
||
148 | * @return boolean |
||
149 | */ |
||
150 | public function create($data = []) { |
||
187 | |||
188 | /** |
||
189 | * @param array $ids |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function delete(array $ids) { |
||
195 | |||
196 | /** |
||
197 | * @param int $template_id |
||
198 | * @param string $variable |
||
199 | * @param string $locale |
||
200 | * @return bool|object |
||
201 | */ |
||
202 | public function deleteVariable($template_id, $variable, $locale) { |
||
205 | |||
206 | /** |
||
207 | * |
||
208 | * @param integer $id ID of element |
||
209 | * @param array $data keys from list: |
||
210 | * 'name', |
||
211 | * 'from', |
||
212 | * 'from_email', |
||
213 | * 'theme', |
||
214 | * 'type', |
||
215 | * 'user_message', |
||
216 | * 'user_message_active', |
||
217 | * 'admin_message', |
||
218 | * 'admin_message_active', |
||
219 | * 'admin_email', |
||
220 | * 'description' |
||
221 | * @return boolean |
||
222 | */ |
||
223 | public function edit($id, $data = []) { |
||
259 | |||
260 | /** |
||
261 | * @param $emails |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | View Code Duplication | public function email_check($emails) { |
|
279 | |||
280 | /** |
||
281 | * @return array|string |
||
282 | */ |
||
283 | public function getAllTemplates() { |
||
286 | |||
287 | /** |
||
288 | * @param bool|string $locale |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getSettings($locale = FALSE) { |
||
294 | |||
295 | /** |
||
296 | * @param int $id |
||
297 | * @param string $locale |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function getTemplateById($id, $locale) { |
||
303 | |||
304 | /** |
||
305 | * @param string $template_id |
||
306 | * @param string $locale |
||
307 | * @return bool|mixed |
||
308 | */ |
||
309 | public function getTemplateVariables($template_id, $locale) { |
||
312 | |||
313 | /** |
||
314 | * test mail sending |
||
315 | * |
||
316 | * @param array $config cohfiguration options for sending email: |
||
317 | * 'protocol', |
||
318 | * 'smtp_port', |
||
319 | * 'type', |
||
320 | * 'mailpath' |
||
321 | */ |
||
322 | public function mailTest($config) { |
||
338 | |||
339 | /** |
||
340 | * send email |
||
341 | * |
||
342 | * @param string $send_to - recepient email |
||
343 | * @param string $pattern_name - email patern name |
||
344 | * @param array $variables - variables to raplase in message: |
||
345 | * $variables = array('$user$' => 'UserName') |
||
346 | * @param bool|string $attachment |
||
347 | * @return bool |
||
348 | */ |
||
349 | public function sendEmail($send_to, $pattern_name, $variables, $attachment = FALSE) { |
||
443 | |||
444 | /** |
||
445 | * set email config |
||
446 | * |
||
447 | * @param array $settings |
||
448 | */ |
||
449 | private function _set_config($settings) { |
||
465 | |||
466 | /** |
||
467 | * replace variables in patern and wrap it |
||
468 | * |
||
469 | * @param array $variables |
||
470 | * @param string $pattern |
||
471 | * @return string |
||
472 | */ |
||
473 | public function replaceVariables($pattern, $variables) { |
||
485 | |||
486 | /** |
||
487 | * send email |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | private function _sendEmail() { |
||
503 | |||
504 | /** |
||
505 | * @param array $settings |
||
506 | * @return bool |
||
507 | */ |
||
508 | public function setSettings($settings) { |
||
511 | |||
512 | /** |
||
513 | * @param int $template_id |
||
514 | * @param string $variable |
||
515 | * @param string $variableNewValue |
||
516 | * @param string $oldVariable |
||
517 | * @param string $locale |
||
518 | * @return bool|object |
||
519 | */ |
||
520 | public function updateVariable($template_id, $variable, $variableNewValue, $oldVariable, $locale) { |
||
523 | |||
524 | } |