Conditions | 12 |
Paths | 14 |
Total Lines | 123 |
Code Lines | 77 |
Lines | 33 |
Ratio | 26.83 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
200 | function send($newsletter_id) { |
||
201 | $OSCOM_Db = Registry::get('Db'); |
||
202 | |||
203 | $audience = array(); |
||
204 | |||
205 | if (isset($_POST['global']) && ($_POST['global'] == 'true')) { |
||
206 | $Qproducts = $OSCOM_Db->get([ |
||
207 | 'customers c', |
||
208 | 'products_notifications pn' |
||
209 | ], [ |
||
210 | 'distinct pn.customers_id', |
||
211 | 'c.customers_firstname', |
||
212 | 'c.customers_lastname', |
||
213 | 'c.customers_email_address' |
||
214 | ], [ |
||
215 | 'c.customers_id' => [ |
||
216 | 'rel' => 'pn.customers_id' |
||
217 | ] |
||
218 | ]); |
||
219 | |||
220 | View Code Duplication | while ($Qproducts->fetch()) { |
|
221 | $audience[$Qproducts->valueInt('customers_id')] = [ |
||
222 | 'firstname' => $Qproducts->value('customers_firstname'), |
||
223 | 'lastname' => $Qproducts->value('customers_lastname'), |
||
224 | 'email_address' => $Qproducts->value('customers_email_address') |
||
225 | ]; |
||
226 | } |
||
227 | |||
228 | $Qcustomers = $OSCOM_Db->get([ |
||
229 | 'customers c', |
||
230 | 'customers_info ci' |
||
231 | ], [ |
||
232 | 'c.customers_id', |
||
233 | 'c.customers_firstname', |
||
234 | 'c.customers_lastname', |
||
235 | 'c.customers_email_address' |
||
236 | ], [ |
||
237 | 'c.customers_id' => [ |
||
238 | 'rel' => 'ci.customers_info_id' |
||
239 | ], |
||
240 | 'ci.global_product_notifications' => '1' |
||
241 | ]); |
||
242 | |||
243 | View Code Duplication | while ($Qcustomers->fetch()) { |
|
244 | $audience[$Qcustomers->valueInt('customers_id')] = [ |
||
245 | 'firstname' => $Qcustomers->value('customers_firstname'), |
||
246 | 'lastname' => $Qcustomers->value('customers_lastname'), |
||
247 | 'email_address' => $Qcustomers->value('customers_email_address') |
||
248 | ]; |
||
249 | } |
||
250 | } else { |
||
251 | $chosen = []; |
||
252 | |||
253 | View Code Duplication | foreach ($_POST['chosen'] as $id) { |
|
254 | if (is_numeric($id) && !in_array($id, $chosen)) { |
||
255 | $chosen[] = $id; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | $ids = array_map(function($k) { |
||
260 | return ':products_id_' . $k; |
||
261 | }, array_keys($chosen)); |
||
262 | |||
263 | $Qproducts = $OSCOM_Db->prepare('select distinct pn.customers_id, c.customers_firstname, c.customers_lastname, c.customers_email_address from :table_customers c, :table_products_notifications pn where c.customers_id = pn.customers_id and pn.products_id in (' . implode(', ', $ids) . ')'); |
||
264 | |||
265 | foreach ($chosen as $k => $v) { |
||
266 | $Qproducts->bindInt(':products_id_' . $k, $v); |
||
267 | } |
||
268 | |||
269 | $Qproducts->execute(); |
||
270 | |||
271 | View Code Duplication | while ($Qproducts->fetch()) { |
|
272 | $audience[$Qproducts->valueInt('customers_id')] = [ |
||
273 | 'firstname' => $Qproducts->value('customers_firstname'), |
||
274 | 'lastname' => $Qproducts->value('customers_lastname'), |
||
275 | 'email_address' => $Qproducts->value('customers_email_address') |
||
276 | ]; |
||
277 | } |
||
278 | |||
279 | $Qcustomers = $OSCOM_Db->get([ |
||
280 | 'customers c', |
||
281 | 'customers_info ci' |
||
282 | ], [ |
||
283 | 'c.customers_id', |
||
284 | 'c.customers_firstname', |
||
285 | 'c.customers_lastname', |
||
286 | 'c.customers_email_address' |
||
287 | ], [ |
||
288 | 'c.customers_id' => [ |
||
289 | 'rel' => 'ci.customers_info_id' |
||
290 | ], |
||
291 | 'ci.global_product_notifications' => '1' |
||
292 | ]); |
||
293 | |||
294 | View Code Duplication | while ($Qcustomers->fetch()) { |
|
295 | $audience[$Qcustomers->valueInt('customers_id')] = [ |
||
296 | 'firstname' => $Qcustomers->value('customers_firstname'), |
||
297 | 'lastname' => $Qcustomers->value('customers_lastname'), |
||
298 | 'email_address' => $Qcustomers->value('customers_email_address') |
||
299 | ]; |
||
300 | } |
||
301 | } |
||
302 | |||
303 | $notificationEmail = new Mail(); |
||
304 | $notificationEmail->setFrom(STORE_OWNER_EMAIL_ADDRESS, STORE_OWNER); |
||
305 | $notificationEmail->setSubject($this->title); |
||
306 | $notificationEmail->setBody($this->content); |
||
307 | |||
308 | foreach ( $audience as $key => $value ) { |
||
309 | $notificationEmail->clearTo(); |
||
310 | |||
311 | $notificationEmail->addTo($value['email_address'], $value['firstname'] . ' ' . $value['lastname']); |
||
312 | |||
313 | $notificationEmail->send(); |
||
314 | } |
||
315 | |||
316 | $OSCOM_Db->save('newsletters', [ |
||
317 | 'date_sent' => 'now()', |
||
318 | 'status' => '1' |
||
319 | ], [ |
||
320 | 'newsletters_id' => (int)$newsletter_id |
||
321 | ]); |
||
322 | } |
||
323 | } |
||
325 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.