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 MathTrig 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 MathTrig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class MathTrig |
||
28 | { |
||
29 | // |
||
30 | // Private method to return an array of the factors of the input value |
||
31 | // |
||
32 | 35 | private static function factors($value) |
|
33 | { |
||
34 | 35 | $startVal = floor(sqrt($value)); |
|
35 | |||
36 | 35 | $factorArray = []; |
|
37 | 35 | for ($i = $startVal; $i > 1; --$i) { |
|
38 | 33 | if (($value % $i) == 0) { |
|
39 | 23 | $factorArray = array_merge($factorArray, self::factors($value / $i)); |
|
40 | 23 | $factorArray = array_merge($factorArray, self::factors($i)); |
|
41 | 23 | if ($i <= sqrt($value)) { |
|
42 | 23 | break; |
|
43 | } |
||
44 | } |
||
45 | } |
||
46 | 35 | if (!empty($factorArray)) { |
|
47 | 23 | rsort($factorArray); |
|
48 | 23 | ||
49 | return $factorArray; |
||
50 | 35 | } else { |
|
51 | return [(integer) $value]; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | 5 | private static function romanCut($num, $n) |
|
59 | |||
60 | /** |
||
61 | * ATAN2 |
||
62 | * |
||
63 | * This function calculates the arc tangent of the two variables x and y. It is similar to |
||
64 | * calculating the arc tangent of y ÷ x, except that the signs of both arguments are used |
||
65 | * to determine the quadrant of the result. |
||
66 | * The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a |
||
67 | * point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between |
||
68 | * -pi and pi, excluding -pi. |
||
69 | * |
||
70 | * Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard |
||
71 | * PHP atan2() function, so we need to reverse them here before calling the PHP atan() function. |
||
72 | * |
||
73 | * Excel Function: |
||
74 | * ATAN2(xCoordinate,yCoordinate) |
||
75 | * |
||
76 | * @category Mathematical and Trigonometric Functions |
||
77 | * @param float $xCoordinate The x-coordinate of the point. |
||
78 | * @param float $yCoordinate The y-coordinate of the point. |
||
79 | * @return float The inverse tangent of the specified x- and y-coordinates. |
||
80 | */ |
||
81 | public static function ATAN2($xCoordinate = null, $yCoordinate = null) |
||
103 | |||
104 | /** |
||
105 | * CEILING |
||
106 | * |
||
107 | * Returns number rounded up, away from zero, to the nearest multiple of significance. |
||
108 | * For example, if you want to avoid using pennies in your prices and your product is |
||
109 | * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the |
||
110 | * nearest nickel. |
||
111 | * |
||
112 | * Excel Function: |
||
113 | * CEILING(number[,significance]) |
||
114 | * |
||
115 | * @category Mathematical and Trigonometric Functions |
||
116 | * @param float $number The number you want to round. |
||
117 | * @param float $significance The multiple to which you want to round. |
||
118 | * @return float Rounded Number |
||
119 | */ |
||
120 | View Code Duplication | public static function CEILING($number, $significance = null) |
|
142 | 2 | ||
143 | /** |
||
144 | * COMBIN |
||
145 | * |
||
146 | * Returns the number of combinations for a given number of items. Use COMBIN to |
||
147 | * determine the total possible number of groups for a given number of items. |
||
148 | * |
||
149 | * Excel Function: |
||
150 | * COMBIN(numObjs,numInSet) |
||
151 | * |
||
152 | * @category Mathematical and Trigonometric Functions |
||
153 | * @param int $numObjs Number of different objects |
||
154 | * @param int $numInSet Number of objects in each combination |
||
155 | * @return int Number of combinations |
||
156 | */ |
||
157 | public static function COMBIN($numObjs, $numInSet) |
||
174 | 1 | ||
175 | /** |
||
176 | * EVEN |
||
177 | * |
||
178 | * Returns number rounded up to the nearest even integer. |
||
179 | * You can use this function for processing items that come in twos. For example, |
||
180 | * a packing crate accepts rows of one or two items. The crate is full when |
||
181 | * the number of items, rounded up to the nearest two, matches the crate's |
||
182 | * capacity. |
||
183 | * |
||
184 | * Excel Function: |
||
185 | * EVEN(number) |
||
186 | * |
||
187 | * @category Mathematical and Trigonometric Functions |
||
188 | * @param float $number Number to round |
||
189 | * @return int Rounded Number |
||
190 | */ |
||
191 | public static function EVEN($number) |
||
209 | 1 | ||
210 | /** |
||
211 | * FACT |
||
212 | * |
||
213 | * Returns the factorial of a number. |
||
214 | * The factorial of a number is equal to 1*2*3*...* number. |
||
215 | * |
||
216 | * Excel Function: |
||
217 | * FACT(factVal) |
||
218 | * |
||
219 | * @category Mathematical and Trigonometric Functions |
||
220 | * @param float $factVal Factorial Value |
||
221 | * @return int Factorial |
||
222 | */ |
||
223 | public static function FACT($factVal) |
||
248 | 1 | ||
249 | /** |
||
250 | * FACTDOUBLE |
||
251 | * |
||
252 | * Returns the double factorial of a number. |
||
253 | * |
||
254 | * Excel Function: |
||
255 | * FACTDOUBLE(factVal) |
||
256 | * |
||
257 | * @category Mathematical and Trigonometric Functions |
||
258 | * @param float $factVal Factorial Value |
||
259 | * @return int Double Factorial |
||
260 | */ |
||
261 | public static function FACTDOUBLE($factVal) |
||
281 | 1 | ||
282 | /** |
||
283 | * FLOOR |
||
284 | * |
||
285 | * Rounds number down, toward zero, to the nearest multiple of significance. |
||
286 | * |
||
287 | * Excel Function: |
||
288 | * FLOOR(number[,significance]) |
||
289 | * |
||
290 | * @category Mathematical and Trigonometric Functions |
||
291 | * @param float $number Number to round |
||
292 | * @param float $significance Significance |
||
293 | * @return float Rounded Number |
||
294 | */ |
||
295 | View Code Duplication | public static function FLOOR($number, $significance = null) |
|
319 | |||
320 | /** |
||
321 | 2 | * GCD |
|
322 | * |
||
323 | * Returns the greatest common divisor of a series of numbers. |
||
324 | * The greatest common divisor is the largest integer that divides both |
||
325 | * number1 and number2 without a remainder. |
||
326 | * |
||
327 | * Excel Function: |
||
328 | * GCD(number1[,number2[, ...]]) |
||
329 | * |
||
330 | * @category Mathematical and Trigonometric Functions |
||
331 | * @param mixed $arg,... Data values |
||
332 | * @return int Greatest Common Divisor |
||
333 | */ |
||
334 | public static function GCD() |
||
396 | |||
397 | 12 | /** |
|
398 | * INT |
||
399 | * |
||
400 | * Casts a floating point value to an integer |
||
401 | * |
||
402 | * Excel Function: |
||
403 | * INT(number) |
||
404 | * |
||
405 | * @category Mathematical and Trigonometric Functions |
||
406 | * @param float $number Number to cast to an integer |
||
407 | * @return int Integer value |
||
408 | */ |
||
409 | View Code Duplication | public static function INT($number) |
|
424 | 16 | ||
425 | 15 | /** |
|
426 | * LCM |
||
427 | 1 | * |
|
428 | * Returns the lowest common multiplier of a series of numbers |
||
429 | * The least common multiple is the smallest positive integer that is a multiple |
||
430 | * of all integer arguments number1, number2, and so on. Use LCM to add fractions |
||
431 | * with different denominators. |
||
432 | * |
||
433 | * Excel Function: |
||
434 | * LCM(number1[,number2[, ...]]) |
||
435 | * |
||
436 | * @category Mathematical and Trigonometric Functions |
||
437 | * @param mixed $arg,... Data values |
||
438 | * @return int Lowest Common Multiplier |
||
439 | */ |
||
440 | public static function LCM() |
||
476 | |||
477 | 9 | /** |
|
478 | 9 | * LOG_BASE |
|
479 | * |
||
480 | 9 | * Returns the logarithm of a number to a specified base. The default base is 10. |
|
481 | * |
||
482 | * Excel Function: |
||
483 | * LOG(number[,base]) |
||
484 | * |
||
485 | * @category Mathematical and Trigonometric Functions |
||
486 | * @param float $number The positive real number for which you want the logarithm |
||
487 | * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10. |
||
488 | * @return float |
||
489 | */ |
||
490 | public static function logBase($number = null, $base = 10) |
||
504 | 2 | ||
505 | /** |
||
506 | 67 | * MDETERM |
|
507 | 18 | * |
|
508 | * Returns the matrix determinant of an array. |
||
509 | 49 | * |
|
510 | * Excel Function: |
||
511 | * MDETERM(array) |
||
512 | * |
||
513 | * @category Mathematical and Trigonometric Functions |
||
514 | * @param array $matrixValues A matrix of values |
||
515 | * @return float |
||
516 | */ |
||
517 | View Code Duplication | public static function MDETERM($matrixValues) |
|
554 | |||
555 | /** |
||
556 | * MINVERSE |
||
557 | * |
||
558 | * Returns the inverse matrix for the matrix stored in an array. |
||
559 | * |
||
560 | * Excel Function: |
||
561 | * MINVERSE(array) |
||
562 | * |
||
563 | * @category Mathematical and Trigonometric Functions |
||
564 | * @param array $matrixValues A matrix of values |
||
565 | * @return array |
||
566 | */ |
||
567 | View Code Duplication | public static function MINVERSE($matrixValues) |
|
606 | |||
607 | /** |
||
608 | * MMULT |
||
609 | * |
||
610 | * @param array $matrixData1 A matrix of values |
||
611 | * @param array $matrixData2 A matrix of values |
||
612 | * @return array |
||
613 | */ |
||
614 | public static function MMULT($matrixData1, $matrixData2) |
||
669 | |||
670 | /** |
||
671 | * MOD |
||
672 | * |
||
673 | * @param int $a Dividend |
||
674 | * @param int $b Divisor |
||
675 | * @return int Remainder |
||
676 | */ |
||
677 | public static function MOD($a = 1, $b = 1) |
||
692 | 9 | ||
693 | 1 | /** |
|
694 | 8 | * MROUND |
|
695 | 1 | * |
|
696 | 7 | * Rounds a number to the nearest multiple of a specified value |
|
697 | 2 | * |
|
698 | * @param float $number Number to round |
||
699 | * @param int $multiple Multiple to which you want to round $number |
||
700 | 5 | * @return float Rounded Number |
|
701 | */ |
||
702 | public static function MROUND($number, $multiple) |
||
722 | 10 | ||
723 | 9 | /** |
|
724 | 9 | * MULTINOMIAL |
|
725 | * |
||
726 | 1 | * Returns the ratio of the factorial of a sum of values to the product of factorials. |
|
727 | * |
||
728 | 2 | * @param array of mixed Data Series |
|
729 | * @return float |
||
730 | */ |
||
731 | public static function MULTINOMIAL() |
||
758 | |||
759 | 2 | /** |
|
760 | 2 | * ODD |
|
761 | 2 | * |
|
762 | * Returns number rounded up to the nearest odd integer. |
||
763 | * |
||
764 | * @param float $number Number to round |
||
765 | * @return int Rounded Number |
||
766 | */ |
||
767 | public static function ODD($number) |
||
791 | 5 | ||
792 | /** |
||
793 | * POWER |
||
794 | 8 | * |
|
795 | * Computes x raised to the power y. |
||
796 | 1 | * |
|
797 | * @param float $x |
||
798 | * @param float $y |
||
799 | * @return float |
||
800 | */ |
||
801 | public static function POWER($x = 0, $y = 2) |
||
818 | 2 | ||
819 | /** |
||
820 | * PRODUCT |
||
821 | * |
||
822 | 78 | * PRODUCT returns the product of all the values and cells referenced in the argument list. |
|
823 | 78 | * |
|
824 | * Excel Function: |
||
825 | * PRODUCT(value1[,value2[, ...]]) |
||
826 | * |
||
827 | * @category Mathematical and Trigonometric Functions |
||
828 | * @param mixed $arg,... Data values |
||
829 | * @return float |
||
830 | */ |
||
831 | View Code Duplication | public static function PRODUCT() |
|
855 | |||
856 | /** |
||
857 | * QUOTIENT |
||
858 | 7 | * |
|
859 | * QUOTIENT function returns the integer portion of a division. Numerator is the divided number |
||
860 | * and denominator is the divisor. |
||
861 | 7 | * |
|
862 | * Excel Function: |
||
863 | * QUOTIENT(value1[,value2[, ...]]) |
||
864 | * |
||
865 | * @category Mathematical and Trigonometric Functions |
||
866 | * @param mixed $arg,... Data values |
||
867 | * @return float |
||
868 | */ |
||
869 | public static function QUOTIENT() |
||
893 | |||
894 | 6 | /** |
|
895 | * RAND |
||
896 | * |
||
897 | * @param int $min Minimal value |
||
898 | * @param int $max Maximal value |
||
899 | * @return int Random number |
||
900 | */ |
||
901 | 6 | public static function RAND($min = 0, $max = 0) |
|
912 | |||
913 | public static function ROMAN($aValue, $style = 0) |
||
944 | |||
945 | /** |
||
946 | * ROUNDUP |
||
947 | 5 | * |
|
948 | 5 | * Rounds a number up to a specified number of decimal places |
|
949 | 5 | * |
|
950 | 5 | * @param float $number Number to round |
|
951 | 5 | * @param int $digits Number of digits to which you want to round $number |
|
952 | 5 | * @return float Rounded Number |
|
953 | */ |
||
954 | 5 | View Code Duplication | public static function ROUNDUP($number, $digits) |
970 | 14 | ||
971 | /** |
||
972 | 14 | * ROUNDDOWN |
|
973 | 12 | * |
|
974 | 12 | * Rounds a number down to a specified number of decimal places |
|
975 | 2 | * |
|
976 | * @param float $number Number to round |
||
977 | 10 | * @param int $digits Number of digits to which you want to round $number |
|
978 | * @return float Rounded Number |
||
979 | */ |
||
980 | 2 | View Code Duplication | public static function ROUNDDOWN($number, $digits) |
996 | 14 | ||
997 | /** |
||
998 | 14 | * SERIESSUM |
|
999 | 12 | * |
|
1000 | 12 | * Returns the sum of a power series |
|
1001 | 2 | * |
|
1002 | * @param float $x Input value to the power series |
||
1003 | 10 | * @param float $n Initial power to which you want to raise $x |
|
1004 | * @param float $m Step by which to increase $n for each term in the series |
||
1005 | * @param array of mixed Data Series |
||
1006 | 2 | * @return float |
|
1007 | */ |
||
1008 | public static function SERIESSUM() |
||
1036 | |||
1037 | 2 | /** |
|
1038 | 2 | * SIGN |
|
1039 | * |
||
1040 | 2 | * Determines the sign of a number. Returns 1 if the number is positive, zero (0) |
|
1041 | * if the number is 0, and -1 if the number is negative. |
||
1042 | * |
||
1043 | 2 | * @param float $number Number to round |
|
1044 | * @return int sign value |
||
1045 | */ |
||
1046 | View Code Duplication | public static function SIGN($number) |
|
1063 | 2 | ||
1064 | /** |
||
1065 | 70 | * SQRTPI |
|
1066 | 69 | * |
|
1067 | 4 | * Returns the square root of (number * pi). |
|
1068 | * |
||
1069 | 65 | * @param float $number Number |
|
1070 | * @return float Square Root of Number * Pi |
||
1071 | 1 | */ |
|
1072 | View Code Duplication | public static function SQRTPI($number) |
|
1086 | |||
1087 | 15 | /** |
|
1088 | 14 | * SUBTOTAL |
|
1089 | 3 | * |
|
1090 | * Returns a subtotal in a list or database. |
||
1091 | 11 | * |
|
1092 | * @param int the number 1 to 11 that specifies which function to |
||
1093 | 1 | * use in calculating subtotals within a list. |
|
1094 | * @param array of mixed Data Series |
||
1095 | * @return float |
||
1096 | */ |
||
1097 | public static function SUBTOTAL() |
||
1133 | |||
1134 | /** |
||
1135 | * SUM |
||
1136 | * |
||
1137 | * SUM computes the sum of all the values and cells referenced in the argument list. |
||
1138 | * |
||
1139 | * Excel Function: |
||
1140 | * SUM(value1[,value2[, ...]]) |
||
1141 | * |
||
1142 | * @category Mathematical and Trigonometric Functions |
||
1143 | * @param mixed $arg,... Data values |
||
1144 | * @return float |
||
1145 | */ |
||
1146 | View Code Duplication | public static function SUM() |
|
1160 | |||
1161 | /** |
||
1162 | * SUMIF |
||
1163 | * |
||
1164 | * Counts the number of cells that contain numbers within the list of arguments |
||
1165 | * |
||
1166 | * Excel Function: |
||
1167 | * SUMIF(value1[,value2[, ...]],condition) |
||
1168 | * |
||
1169 | * @category Mathematical and Trigonometric Functions |
||
1170 | * @param mixed $arg,... Data values |
||
1171 | * @param string $condition The criteria that defines which cells will be summed. |
||
1172 | * @return float |
||
1173 | */ |
||
1174 | public static function SUMIF($aArgs, $condition, $sumArgs = []) |
||
1200 | 4 | ||
1201 | 4 | /** |
|
1202 | * SUMIFS |
||
1203 | * |
||
1204 | 5 | * Counts the number of cells that contain numbers within the list of arguments |
|
1205 | 5 | * |
|
1206 | * Excel Function: |
||
1207 | 5 | * SUMIFS(value1[,value2[, ...]],condition) |
|
1208 | * |
||
1209 | * @category Mathematical and Trigonometric Functions |
||
1210 | * @param mixed $arg,... Data values |
||
1211 | 5 | * @param string $condition The criteria that defines which cells will be summed. |
|
1212 | * @return float |
||
1213 | */ |
||
1214 | public static function SUMIFS() |
||
1248 | |||
1249 | /** |
||
1250 | * SUMPRODUCT |
||
1251 | * |
||
1252 | * Excel Function: |
||
1253 | * SUMPRODUCT(value1[,value2[, ...]]) |
||
1254 | * |
||
1255 | * @category Mathematical and Trigonometric Functions |
||
1256 | * @param mixed $arg,... Data values |
||
1257 | * @return float |
||
1258 | */ |
||
1259 | public static function SUMPRODUCT() |
||
1289 | |||
1290 | /** |
||
1291 | * SUMSQ |
||
1292 | * |
||
1293 | * SUMSQ returns the sum of the squares of the arguments |
||
1294 | * |
||
1295 | * Excel Function: |
||
1296 | * SUMSQ(value1[,value2[, ...]]) |
||
1297 | * |
||
1298 | * @category Mathematical and Trigonometric Functions |
||
1299 | * @param mixed $arg,... Data values |
||
1300 | * @return float |
||
1301 | */ |
||
1302 | View Code Duplication | public static function SUMSQ() |
|
1316 | |||
1317 | /** |
||
1318 | * SUMX2MY2 |
||
1319 | 7 | * |
|
1320 | * @param mixed[] $matrixData1 Matrix #1 |
||
1321 | 7 | * @param mixed[] $matrixData2 Matrix #2 |
|
1322 | * @return float |
||
1323 | */ |
||
1324 | 7 | View Code Duplication | public static function SUMX2MY2($matrixData1, $matrixData2) |
1340 | |||
1341 | /** |
||
1342 | * SUMX2PY2 |
||
1343 | * |
||
1344 | * @param mixed[] $matrixData1 Matrix #1 |
||
1345 | * @param mixed[] $matrixData2 Matrix #2 |
||
1346 | * @return float |
||
1347 | */ |
||
1348 | View Code Duplication | public static function SUMX2PY2($matrixData1, $matrixData2) |
|
1364 | |||
1365 | /** |
||
1366 | * SUMXMY2 |
||
1367 | * |
||
1368 | * @param mixed[] $matrixData1 Matrix #1 |
||
1369 | * @param mixed[] $matrixData2 Matrix #2 |
||
1370 | * @return float |
||
1371 | */ |
||
1372 | View Code Duplication | public static function SUMXMY2($matrixData1, $matrixData2) |
|
1388 | |||
1389 | /** |
||
1390 | * TRUNC |
||
1391 | * |
||
1392 | * Truncates value to the number of fractional digits by number_digits. |
||
1393 | * |
||
1394 | * @param float $value |
||
1395 | * @param int $digits |
||
1396 | * @return float Truncated value |
||
1397 | */ |
||
1398 | public static function TRUNC($value = 0, $digits = 0) |
||
1418 | } |
||
1419 |
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.