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 Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Database |
||
28 | { |
||
29 | /** |
||
30 | * fieldExtract. |
||
31 | * |
||
32 | * Extracts the column ID to use for the data field. |
||
33 | * |
||
34 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
35 | * A database is a list of related data in which rows of related |
||
36 | * information are records, and columns of data are fields. The |
||
37 | * first row of the list contains labels for each column. |
||
38 | * @param mixed $field Indicates which column is used in the function. Enter the |
||
39 | * column label enclosed between double quotation marks, such as |
||
40 | * "Age" or "Yield," or a number (without quotation marks) that |
||
41 | * represents the position of the column within the list: 1 for |
||
42 | * the first column, 2 for the second column, and so on. |
||
43 | * |
||
44 | * @return string|null |
||
45 | */ |
||
46 | private static function fieldExtract($database, $field) |
||
60 | |||
61 | /** |
||
62 | * filter. |
||
63 | * |
||
64 | * Parses the selection criteria, extracts the database rows that match those criteria, and |
||
65 | * returns that subset of rows. |
||
66 | * |
||
67 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
68 | * A database is a list of related data in which rows of related |
||
69 | * information are records, and columns of data are fields. The |
||
70 | * first row of the list contains labels for each column. |
||
71 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
72 | * You can use any range for the criteria argument, as long as it |
||
73 | * includes at least one column label and at least one cell below |
||
74 | * the column label in which you specify a condition for the |
||
75 | * column. |
||
76 | * |
||
77 | * @return array of mixed |
||
78 | */ |
||
79 | private static function filter($database, $criteria) |
||
133 | |||
134 | private static function getFilteredColumn($database, $field, $criteria) |
||
146 | |||
147 | /** |
||
148 | * DAVERAGE. |
||
149 | * |
||
150 | * Averages the values in a column of a list or database that match conditions you specify. |
||
151 | * |
||
152 | * Excel Function: |
||
153 | * DAVERAGE(database,field,criteria) |
||
154 | * |
||
155 | * @category Database Functions |
||
156 | * |
||
157 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
158 | * A database is a list of related data in which rows of related |
||
159 | * information are records, and columns of data are fields. The |
||
160 | * first row of the list contains labels for each column. |
||
161 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
162 | * column label enclosed between double quotation marks, such as |
||
163 | * "Age" or "Yield," or a number (without quotation marks) that |
||
164 | * represents the position of the column within the list: 1 for |
||
165 | * the first column, 2 for the second column, and so on. |
||
166 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
167 | * You can use any range for the criteria argument, as long as it |
||
168 | * includes at least one column label and at least one cell below |
||
169 | * the column label in which you specify a condition for the |
||
170 | * column. |
||
171 | * |
||
172 | * @return float |
||
173 | */ |
||
174 | public static function DAVERAGE($database, $field, $criteria) |
||
186 | |||
187 | /** |
||
188 | * DCOUNT. |
||
189 | * |
||
190 | * Counts the cells that contain numbers in a column of a list or database that match conditions |
||
191 | * that you specify. |
||
192 | * |
||
193 | * Excel Function: |
||
194 | * DCOUNT(database,[field],criteria) |
||
195 | * |
||
196 | * Excel Function: |
||
197 | * DAVERAGE(database,field,criteria) |
||
198 | * |
||
199 | * @category Database Functions |
||
200 | * |
||
201 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
202 | * A database is a list of related data in which rows of related |
||
203 | * information are records, and columns of data are fields. The |
||
204 | * first row of the list contains labels for each column. |
||
205 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
206 | * column label enclosed between double quotation marks, such as |
||
207 | * "Age" or "Yield," or a number (without quotation marks) that |
||
208 | * represents the position of the column within the list: 1 for |
||
209 | * the first column, 2 for the second column, and so on. |
||
210 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
211 | * You can use any range for the criteria argument, as long as it |
||
212 | * includes at least one column label and at least one cell below |
||
213 | * the column label in which you specify a condition for the |
||
214 | * column. |
||
215 | * |
||
216 | * @return int |
||
217 | * |
||
218 | * @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the |
||
219 | * database that match the criteria. |
||
220 | */ |
||
221 | View Code Duplication | public static function DCOUNT($database, $field, $criteria) |
|
233 | |||
234 | /** |
||
235 | * DCOUNTA. |
||
236 | * |
||
237 | * Counts the nonblank cells in a column of a list or database that match conditions that you specify. |
||
238 | * |
||
239 | * Excel Function: |
||
240 | * DCOUNTA(database,[field],criteria) |
||
241 | * |
||
242 | * @category Database Functions |
||
243 | * |
||
244 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
245 | * A database is a list of related data in which rows of related |
||
246 | * information are records, and columns of data are fields. The |
||
247 | * first row of the list contains labels for each column. |
||
248 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
249 | * column label enclosed between double quotation marks, such as |
||
250 | * "Age" or "Yield," or a number (without quotation marks) that |
||
251 | * represents the position of the column within the list: 1 for |
||
252 | * the first column, 2 for the second column, and so on. |
||
253 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
254 | * You can use any range for the criteria argument, as long as it |
||
255 | * includes at least one column label and at least one cell below |
||
256 | * the column label in which you specify a condition for the |
||
257 | * column. |
||
258 | * |
||
259 | * @return int |
||
260 | * |
||
261 | * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the |
||
262 | * database that match the criteria. |
||
263 | */ |
||
264 | public static function DCOUNTA($database, $field, $criteria) |
||
284 | |||
285 | /** |
||
286 | * DGET. |
||
287 | * |
||
288 | * Extracts a single value from a column of a list or database that matches conditions that you |
||
289 | * specify. |
||
290 | * |
||
291 | * Excel Function: |
||
292 | * DGET(database,field,criteria) |
||
293 | * |
||
294 | * @category Database Functions |
||
295 | * |
||
296 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
297 | * A database is a list of related data in which rows of related |
||
298 | * information are records, and columns of data are fields. The |
||
299 | * first row of the list contains labels for each column. |
||
300 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
301 | * column label enclosed between double quotation marks, such as |
||
302 | * "Age" or "Yield," or a number (without quotation marks) that |
||
303 | * represents the position of the column within the list: 1 for |
||
304 | * the first column, 2 for the second column, and so on. |
||
305 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
306 | * You can use any range for the criteria argument, as long as it |
||
307 | * includes at least one column label and at least one cell below |
||
308 | * the column label in which you specify a condition for the |
||
309 | * column. |
||
310 | * |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public static function DGET($database, $field, $criteria) |
||
328 | |||
329 | /** |
||
330 | * DMAX. |
||
331 | * |
||
332 | * Returns the largest number in a column of a list or database that matches conditions you that |
||
333 | * specify. |
||
334 | * |
||
335 | * Excel Function: |
||
336 | * DMAX(database,field,criteria) |
||
337 | * |
||
338 | * @category Database Functions |
||
339 | * |
||
340 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
341 | * A database is a list of related data in which rows of related |
||
342 | * information are records, and columns of data are fields. The |
||
343 | * first row of the list contains labels for each column. |
||
344 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
345 | * column label enclosed between double quotation marks, such as |
||
346 | * "Age" or "Yield," or a number (without quotation marks) that |
||
347 | * represents the position of the column within the list: 1 for |
||
348 | * the first column, 2 for the second column, and so on. |
||
349 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
350 | * You can use any range for the criteria argument, as long as it |
||
351 | * includes at least one column label and at least one cell below |
||
352 | * the column label in which you specify a condition for the |
||
353 | * column. |
||
354 | * |
||
355 | * @return float |
||
356 | */ |
||
357 | View Code Duplication | public static function DMAX($database, $field, $criteria) |
|
369 | |||
370 | /** |
||
371 | * DMIN. |
||
372 | * |
||
373 | * Returns the smallest number in a column of a list or database that matches conditions you that |
||
374 | * specify. |
||
375 | * |
||
376 | * Excel Function: |
||
377 | * DMIN(database,field,criteria) |
||
378 | * |
||
379 | * @category Database Functions |
||
380 | * |
||
381 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
382 | * A database is a list of related data in which rows of related |
||
383 | * information are records, and columns of data are fields. The |
||
384 | * first row of the list contains labels for each column. |
||
385 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
386 | * column label enclosed between double quotation marks, such as |
||
387 | * "Age" or "Yield," or a number (without quotation marks) that |
||
388 | * represents the position of the column within the list: 1 for |
||
389 | * the first column, 2 for the second column, and so on. |
||
390 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
391 | * You can use any range for the criteria argument, as long as it |
||
392 | * includes at least one column label and at least one cell below |
||
393 | * the column label in which you specify a condition for the |
||
394 | * column. |
||
395 | * |
||
396 | * @return float |
||
397 | */ |
||
398 | View Code Duplication | public static function DMIN($database, $field, $criteria) |
|
410 | |||
411 | /** |
||
412 | * DPRODUCT. |
||
413 | * |
||
414 | * Multiplies the values in a column of a list or database that match conditions that you specify. |
||
415 | * |
||
416 | * Excel Function: |
||
417 | * DPRODUCT(database,field,criteria) |
||
418 | * |
||
419 | * @category Database Functions |
||
420 | * |
||
421 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
422 | * A database is a list of related data in which rows of related |
||
423 | * information are records, and columns of data are fields. The |
||
424 | * first row of the list contains labels for each column. |
||
425 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
426 | * column label enclosed between double quotation marks, such as |
||
427 | * "Age" or "Yield," or a number (without quotation marks) that |
||
428 | * represents the position of the column within the list: 1 for |
||
429 | * the first column, 2 for the second column, and so on. |
||
430 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
431 | * You can use any range for the criteria argument, as long as it |
||
432 | * includes at least one column label and at least one cell below |
||
433 | * the column label in which you specify a condition for the |
||
434 | * column. |
||
435 | * |
||
436 | * @return float |
||
437 | */ |
||
438 | View Code Duplication | public static function DPRODUCT($database, $field, $criteria) |
|
450 | |||
451 | /** |
||
452 | * DSTDEV. |
||
453 | * |
||
454 | * Estimates the standard deviation of a population based on a sample by using the numbers in a |
||
455 | * column of a list or database that match conditions that you specify. |
||
456 | * |
||
457 | * Excel Function: |
||
458 | * DSTDEV(database,field,criteria) |
||
459 | * |
||
460 | * @category Database Functions |
||
461 | * |
||
462 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
463 | * A database is a list of related data in which rows of related |
||
464 | * information are records, and columns of data are fields. The |
||
465 | * first row of the list contains labels for each column. |
||
466 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
467 | * column label enclosed between double quotation marks, such as |
||
468 | * "Age" or "Yield," or a number (without quotation marks) that |
||
469 | * represents the position of the column within the list: 1 for |
||
470 | * the first column, 2 for the second column, and so on. |
||
471 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
472 | * You can use any range for the criteria argument, as long as it |
||
473 | * includes at least one column label and at least one cell below |
||
474 | * the column label in which you specify a condition for the |
||
475 | * column. |
||
476 | * |
||
477 | * @return float |
||
478 | */ |
||
479 | public static function DSTDEV($database, $field, $criteria) |
||
491 | |||
492 | /** |
||
493 | * DSTDEVP. |
||
494 | * |
||
495 | * Calculates the standard deviation of a population based on the entire population by using the |
||
496 | * numbers in a column of a list or database that match conditions that you specify. |
||
497 | * |
||
498 | * Excel Function: |
||
499 | * DSTDEVP(database,field,criteria) |
||
500 | * |
||
501 | * @category Database Functions |
||
502 | * |
||
503 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
504 | * A database is a list of related data in which rows of related |
||
505 | * information are records, and columns of data are fields. The |
||
506 | * first row of the list contains labels for each column. |
||
507 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
508 | * column label enclosed between double quotation marks, such as |
||
509 | * "Age" or "Yield," or a number (without quotation marks) that |
||
510 | * represents the position of the column within the list: 1 for |
||
511 | * the first column, 2 for the second column, and so on. |
||
512 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
513 | * You can use any range for the criteria argument, as long as it |
||
514 | * includes at least one column label and at least one cell below |
||
515 | * the column label in which you specify a condition for the |
||
516 | * column. |
||
517 | * |
||
518 | * @return float |
||
519 | */ |
||
520 | public static function DSTDEVP($database, $field, $criteria) |
||
532 | |||
533 | /** |
||
534 | * DSUM. |
||
535 | * |
||
536 | * Adds the numbers in a column of a list or database that match conditions that you specify. |
||
537 | * |
||
538 | * Excel Function: |
||
539 | * DSUM(database,field,criteria) |
||
540 | * |
||
541 | * @category Database Functions |
||
542 | * |
||
543 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
544 | * A database is a list of related data in which rows of related |
||
545 | * information are records, and columns of data are fields. The |
||
546 | * first row of the list contains labels for each column. |
||
547 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
548 | * column label enclosed between double quotation marks, such as |
||
549 | * "Age" or "Yield," or a number (without quotation marks) that |
||
550 | * represents the position of the column within the list: 1 for |
||
551 | * the first column, 2 for the second column, and so on. |
||
552 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
553 | * You can use any range for the criteria argument, as long as it |
||
554 | * includes at least one column label and at least one cell below |
||
555 | * the column label in which you specify a condition for the |
||
556 | * column. |
||
557 | * |
||
558 | * @return float |
||
559 | */ |
||
560 | View Code Duplication | public static function DSUM($database, $field, $criteria) |
|
572 | |||
573 | /** |
||
574 | * DVAR. |
||
575 | * |
||
576 | * Estimates the variance of a population based on a sample by using the numbers in a column |
||
577 | * of a list or database that match conditions that you specify. |
||
578 | * |
||
579 | * Excel Function: |
||
580 | * DVAR(database,field,criteria) |
||
581 | * |
||
582 | * @category Database Functions |
||
583 | * |
||
584 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
585 | * A database is a list of related data in which rows of related |
||
586 | * information are records, and columns of data are fields. The |
||
587 | * first row of the list contains labels for each column. |
||
588 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
589 | * column label enclosed between double quotation marks, such as |
||
590 | * "Age" or "Yield," or a number (without quotation marks) that |
||
591 | * represents the position of the column within the list: 1 for |
||
592 | * the first column, 2 for the second column, and so on. |
||
593 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
594 | * You can use any range for the criteria argument, as long as it |
||
595 | * includes at least one column label and at least one cell below |
||
596 | * the column label in which you specify a condition for the |
||
597 | * column. |
||
598 | * |
||
599 | * @return float |
||
600 | */ |
||
601 | public static function DVAR($database, $field, $criteria) |
||
613 | |||
614 | /** |
||
615 | * DVARP. |
||
616 | * |
||
617 | * Calculates the variance of a population based on the entire population by using the numbers |
||
618 | * in a column of a list or database that match conditions that you specify. |
||
619 | * |
||
620 | * Excel Function: |
||
621 | * DVARP(database,field,criteria) |
||
622 | * |
||
623 | * @category Database Functions |
||
624 | * |
||
625 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
626 | * A database is a list of related data in which rows of related |
||
627 | * information are records, and columns of data are fields. The |
||
628 | * first row of the list contains labels for each column. |
||
629 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
630 | * column label enclosed between double quotation marks, such as |
||
631 | * "Age" or "Yield," or a number (without quotation marks) that |
||
632 | * represents the position of the column within the list: 1 for |
||
633 | * the first column, 2 for the second column, and so on. |
||
634 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
635 | * You can use any range for the criteria argument, as long as it |
||
636 | * includes at least one column label and at least one cell below |
||
637 | * the column label in which you specify a condition for the |
||
638 | * column. |
||
639 | * |
||
640 | * @return float |
||
641 | */ |
||
642 | View Code Duplication | public static function DVARP($database, $field, $criteria) |
|
654 | } |
||
655 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.