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 | * @return string|null |
||
44 | */ |
||
45 | private static function fieldExtract($database, $field) |
||
59 | |||
60 | /** |
||
61 | * filter |
||
62 | * |
||
63 | * Parses the selection criteria, extracts the database rows that match those criteria, and |
||
64 | * returns that subset of rows. |
||
65 | * |
||
66 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
67 | * A database is a list of related data in which rows of related |
||
68 | * information are records, and columns of data are fields. The |
||
69 | * first row of the list contains labels for each column. |
||
70 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
71 | * You can use any range for the criteria argument, as long as it |
||
72 | * includes at least one column label and at least one cell below |
||
73 | * the column label in which you specify a condition for the |
||
74 | * column. |
||
75 | * @return array of mixed |
||
76 | */ |
||
77 | private static function filter($database, $criteria) |
||
131 | |||
132 | private static function getFilteredColumn($database, $field, $criteria) |
||
144 | |||
145 | /** |
||
146 | * DAVERAGE |
||
147 | * |
||
148 | * Averages the values in a column of a list or database that match conditions you specify. |
||
149 | * |
||
150 | * Excel Function: |
||
151 | * DAVERAGE(database,field,criteria) |
||
152 | * |
||
153 | * @category Database Functions |
||
154 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
155 | * A database is a list of related data in which rows of related |
||
156 | * information are records, and columns of data are fields. The |
||
157 | * first row of the list contains labels for each column. |
||
158 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
159 | * column label enclosed between double quotation marks, such as |
||
160 | * "Age" or "Yield," or a number (without quotation marks) that |
||
161 | * represents the position of the column within the list: 1 for |
||
162 | * the first column, 2 for the second column, and so on. |
||
163 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
164 | * You can use any range for the criteria argument, as long as it |
||
165 | * includes at least one column label and at least one cell below |
||
166 | * the column label in which you specify a condition for the |
||
167 | * column. |
||
168 | * @return float |
||
169 | */ |
||
170 | public static function DAVERAGE($database, $field, $criteria) |
||
182 | |||
183 | /** |
||
184 | * DCOUNT |
||
185 | * |
||
186 | * Counts the cells that contain numbers in a column of a list or database that match conditions |
||
187 | * that you specify. |
||
188 | * |
||
189 | * Excel Function: |
||
190 | * DCOUNT(database,[field],criteria) |
||
191 | * |
||
192 | * Excel Function: |
||
193 | * DAVERAGE(database,field,criteria) |
||
194 | * |
||
195 | * @category Database Functions |
||
196 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
197 | * A database is a list of related data in which rows of related |
||
198 | * information are records, and columns of data are fields. The |
||
199 | * first row of the list contains labels for each column. |
||
200 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
201 | * column label enclosed between double quotation marks, such as |
||
202 | * "Age" or "Yield," or a number (without quotation marks) that |
||
203 | * represents the position of the column within the list: 1 for |
||
204 | * the first column, 2 for the second column, and so on. |
||
205 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
206 | * You can use any range for the criteria argument, as long as it |
||
207 | * includes at least one column label and at least one cell below |
||
208 | * the column label in which you specify a condition for the |
||
209 | * column. |
||
210 | * @return int |
||
211 | * |
||
212 | * @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the |
||
213 | * database that match the criteria. |
||
214 | */ |
||
215 | View Code Duplication | public static function DCOUNT($database, $field, $criteria) |
|
227 | |||
228 | /** |
||
229 | * DCOUNTA |
||
230 | * |
||
231 | * Counts the nonblank cells in a column of a list or database that match conditions that you specify. |
||
232 | * |
||
233 | * Excel Function: |
||
234 | * DCOUNTA(database,[field],criteria) |
||
235 | * |
||
236 | * @category Database Functions |
||
237 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
238 | * A database is a list of related data in which rows of related |
||
239 | * information are records, and columns of data are fields. The |
||
240 | * first row of the list contains labels for each column. |
||
241 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
242 | * column label enclosed between double quotation marks, such as |
||
243 | * "Age" or "Yield," or a number (without quotation marks) that |
||
244 | * represents the position of the column within the list: 1 for |
||
245 | * the first column, 2 for the second column, and so on. |
||
246 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
247 | * You can use any range for the criteria argument, as long as it |
||
248 | * includes at least one column label and at least one cell below |
||
249 | * the column label in which you specify a condition for the |
||
250 | * column. |
||
251 | * @return int |
||
252 | * |
||
253 | * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the |
||
254 | * database that match the criteria. |
||
255 | */ |
||
256 | public static function DCOUNTA($database, $field, $criteria) |
||
276 | |||
277 | /** |
||
278 | * DGET |
||
279 | * |
||
280 | * Extracts a single value from a column of a list or database that matches conditions that you |
||
281 | * specify. |
||
282 | * |
||
283 | * Excel Function: |
||
284 | * DGET(database,field,criteria) |
||
285 | * |
||
286 | * @category Database Functions |
||
287 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
288 | * A database is a list of related data in which rows of related |
||
289 | * information are records, and columns of data are fields. The |
||
290 | * first row of the list contains labels for each column. |
||
291 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
292 | * column label enclosed between double quotation marks, such as |
||
293 | * "Age" or "Yield," or a number (without quotation marks) that |
||
294 | * represents the position of the column within the list: 1 for |
||
295 | * the first column, 2 for the second column, and so on. |
||
296 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
297 | * You can use any range for the criteria argument, as long as it |
||
298 | * includes at least one column label and at least one cell below |
||
299 | * the column label in which you specify a condition for the |
||
300 | * column. |
||
301 | * @return mixed |
||
302 | */ |
||
303 | public static function DGET($database, $field, $criteria) |
||
318 | |||
319 | /** |
||
320 | * DMAX |
||
321 | * |
||
322 | * Returns the largest number in a column of a list or database that matches conditions you that |
||
323 | * specify. |
||
324 | * |
||
325 | * Excel Function: |
||
326 | * DMAX(database,field,criteria) |
||
327 | * |
||
328 | * @category Database Functions |
||
329 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
330 | * A database is a list of related data in which rows of related |
||
331 | * information are records, and columns of data are fields. The |
||
332 | * first row of the list contains labels for each column. |
||
333 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
334 | * column label enclosed between double quotation marks, such as |
||
335 | * "Age" or "Yield," or a number (without quotation marks) that |
||
336 | * represents the position of the column within the list: 1 for |
||
337 | * the first column, 2 for the second column, and so on. |
||
338 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
339 | * You can use any range for the criteria argument, as long as it |
||
340 | * includes at least one column label and at least one cell below |
||
341 | * the column label in which you specify a condition for the |
||
342 | * column. |
||
343 | * @return float |
||
344 | */ |
||
345 | View Code Duplication | public static function DMAX($database, $field, $criteria) |
|
357 | |||
358 | /** |
||
359 | * DMIN |
||
360 | * |
||
361 | * Returns the smallest number in a column of a list or database that matches conditions you that |
||
362 | * specify. |
||
363 | * |
||
364 | * Excel Function: |
||
365 | * DMIN(database,field,criteria) |
||
366 | * |
||
367 | * @category Database Functions |
||
368 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
369 | * A database is a list of related data in which rows of related |
||
370 | * information are records, and columns of data are fields. The |
||
371 | * first row of the list contains labels for each column. |
||
372 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
373 | * column label enclosed between double quotation marks, such as |
||
374 | * "Age" or "Yield," or a number (without quotation marks) that |
||
375 | * represents the position of the column within the list: 1 for |
||
376 | * the first column, 2 for the second column, and so on. |
||
377 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
378 | * You can use any range for the criteria argument, as long as it |
||
379 | * includes at least one column label and at least one cell below |
||
380 | * the column label in which you specify a condition for the |
||
381 | * column. |
||
382 | * @return float |
||
383 | */ |
||
384 | View Code Duplication | public static function DMIN($database, $field, $criteria) |
|
396 | |||
397 | /** |
||
398 | * DPRODUCT |
||
399 | * |
||
400 | * Multiplies the values in a column of a list or database that match conditions that you specify. |
||
401 | * |
||
402 | * Excel Function: |
||
403 | * DPRODUCT(database,field,criteria) |
||
404 | * |
||
405 | * @category Database Functions |
||
406 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
407 | * A database is a list of related data in which rows of related |
||
408 | * information are records, and columns of data are fields. The |
||
409 | * first row of the list contains labels for each column. |
||
410 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
411 | * column label enclosed between double quotation marks, such as |
||
412 | * "Age" or "Yield," or a number (without quotation marks) that |
||
413 | * represents the position of the column within the list: 1 for |
||
414 | * the first column, 2 for the second column, and so on. |
||
415 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
416 | * You can use any range for the criteria argument, as long as it |
||
417 | * includes at least one column label and at least one cell below |
||
418 | * the column label in which you specify a condition for the |
||
419 | * column. |
||
420 | * @return float |
||
421 | */ |
||
422 | View Code Duplication | public static function DPRODUCT($database, $field, $criteria) |
|
434 | |||
435 | /** |
||
436 | * DSTDEV |
||
437 | * |
||
438 | * Estimates the standard deviation of a population based on a sample by using the numbers in a |
||
439 | * column of a list or database that match conditions that you specify. |
||
440 | * |
||
441 | * Excel Function: |
||
442 | * DSTDEV(database,field,criteria) |
||
443 | * |
||
444 | * @category Database Functions |
||
445 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
446 | * A database is a list of related data in which rows of related |
||
447 | * information are records, and columns of data are fields. The |
||
448 | * first row of the list contains labels for each column. |
||
449 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
450 | * column label enclosed between double quotation marks, such as |
||
451 | * "Age" or "Yield," or a number (without quotation marks) that |
||
452 | * represents the position of the column within the list: 1 for |
||
453 | * the first column, 2 for the second column, and so on. |
||
454 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
455 | * You can use any range for the criteria argument, as long as it |
||
456 | * includes at least one column label and at least one cell below |
||
457 | * the column label in which you specify a condition for the |
||
458 | * column. |
||
459 | * @return float |
||
460 | */ |
||
461 | public static function DSTDEV($database, $field, $criteria) |
||
473 | |||
474 | /** |
||
475 | * DSTDEVP |
||
476 | * |
||
477 | * Calculates the standard deviation of a population based on the entire population by using the |
||
478 | * numbers in a column of a list or database that match conditions that you specify. |
||
479 | * |
||
480 | * Excel Function: |
||
481 | * DSTDEVP(database,field,criteria) |
||
482 | * |
||
483 | * @category Database Functions |
||
484 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
485 | * A database is a list of related data in which rows of related |
||
486 | * information are records, and columns of data are fields. The |
||
487 | * first row of the list contains labels for each column. |
||
488 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
489 | * column label enclosed between double quotation marks, such as |
||
490 | * "Age" or "Yield," or a number (without quotation marks) that |
||
491 | * represents the position of the column within the list: 1 for |
||
492 | * the first column, 2 for the second column, and so on. |
||
493 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
494 | * You can use any range for the criteria argument, as long as it |
||
495 | * includes at least one column label and at least one cell below |
||
496 | * the column label in which you specify a condition for the |
||
497 | * column. |
||
498 | * @return float |
||
499 | */ |
||
500 | public static function DSTDEVP($database, $field, $criteria) |
||
512 | |||
513 | /** |
||
514 | * DSUM |
||
515 | * |
||
516 | * Adds the numbers in a column of a list or database that match conditions that you specify. |
||
517 | * |
||
518 | * Excel Function: |
||
519 | * DSUM(database,field,criteria) |
||
520 | * |
||
521 | * @category Database Functions |
||
522 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
523 | * A database is a list of related data in which rows of related |
||
524 | * information are records, and columns of data are fields. The |
||
525 | * first row of the list contains labels for each column. |
||
526 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
527 | * column label enclosed between double quotation marks, such as |
||
528 | * "Age" or "Yield," or a number (without quotation marks) that |
||
529 | * represents the position of the column within the list: 1 for |
||
530 | * the first column, 2 for the second column, and so on. |
||
531 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
532 | * You can use any range for the criteria argument, as long as it |
||
533 | * includes at least one column label and at least one cell below |
||
534 | * the column label in which you specify a condition for the |
||
535 | * column. |
||
536 | * @return float |
||
537 | */ |
||
538 | View Code Duplication | public static function DSUM($database, $field, $criteria) |
|
550 | |||
551 | /** |
||
552 | * DVAR |
||
553 | * |
||
554 | * Estimates the variance of a population based on a sample by using the numbers in a column |
||
555 | * of a list or database that match conditions that you specify. |
||
556 | * |
||
557 | * Excel Function: |
||
558 | * DVAR(database,field,criteria) |
||
559 | * |
||
560 | * @category Database Functions |
||
561 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
562 | * A database is a list of related data in which rows of related |
||
563 | * information are records, and columns of data are fields. The |
||
564 | * first row of the list contains labels for each column. |
||
565 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
566 | * column label enclosed between double quotation marks, such as |
||
567 | * "Age" or "Yield," or a number (without quotation marks) that |
||
568 | * represents the position of the column within the list: 1 for |
||
569 | * the first column, 2 for the second column, and so on. |
||
570 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
571 | * You can use any range for the criteria argument, as long as it |
||
572 | * includes at least one column label and at least one cell below |
||
573 | * the column label in which you specify a condition for the |
||
574 | * column. |
||
575 | * @return float |
||
576 | */ |
||
577 | public static function DVAR($database, $field, $criteria) |
||
589 | |||
590 | /** |
||
591 | * DVARP |
||
592 | * |
||
593 | * Calculates the variance of a population based on the entire population by using the numbers |
||
594 | * in a column of a list or database that match conditions that you specify. |
||
595 | * |
||
596 | * Excel Function: |
||
597 | * DVARP(database,field,criteria) |
||
598 | * |
||
599 | * @category Database Functions |
||
600 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
601 | * A database is a list of related data in which rows of related |
||
602 | * information are records, and columns of data are fields. The |
||
603 | * first row of the list contains labels for each column. |
||
604 | * @param string|int $field Indicates which column is used in the function. Enter the |
||
605 | * column label enclosed between double quotation marks, such as |
||
606 | * "Age" or "Yield," or a number (without quotation marks) that |
||
607 | * represents the position of the column within the list: 1 for |
||
608 | * the first column, 2 for the second column, and so on. |
||
609 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
610 | * You can use any range for the criteria argument, as long as it |
||
611 | * includes at least one column label and at least one cell below |
||
612 | * the column label in which you specify a condition for the |
||
613 | * column. |
||
614 | * @return float |
||
615 | */ |
||
616 | View Code Duplication | public static function DVARP($database, $field, $criteria) |
|
628 | } |
||
629 |
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.