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:
1 | <?php |
||
5 | class Modifier |
||
6 | { |
||
7 | /** |
||
8 | * Set value to null if value is empty |
||
9 | * @param mixed $variable This should be the variable you are checking if it is empty |
||
10 | * @return mixed Returns either NULL or the original variable |
||
11 | */ |
||
12 | public static function setNullOnEmpty($variable) |
||
19 | |||
20 | /** |
||
21 | * Set value to 0 if value is empty |
||
22 | * @param mixed $variable This should be the variable you are checking if it is empty |
||
23 | * @return mixed Returns either 0 or the original variable |
||
24 | */ |
||
25 | public static function setZeroOnEmpty($variable) |
||
32 | |||
33 | /** |
||
34 | * Checks to see if required filed have been filled in and are not empty |
||
35 | * @param mixed $variable This should be the variable you are checking |
||
36 | * @return boolean Will return true if not empty and contains at least minimum number of characters else returns false |
||
37 | */ |
||
38 | public static function isRequiredString($variable, $minStringLength = 2) |
||
45 | |||
46 | /** |
||
47 | * Checks to see if the variable is numeric or not |
||
48 | * @param mixed $variable This should be the variable you are testing if it is a number or not |
||
49 | * @return boolean Returns true if numeric else returns false |
||
50 | */ |
||
51 | View Code Duplication | public static function isRequiredNumeric($variable) |
|
58 | |||
59 | /** |
||
60 | * Checks to see if the variable is non numeric or not |
||
61 | * @param mixed $variable This should be the variable you are testing if it is a number or not |
||
62 | * @return boolean Returns true if non numeric else returns false |
||
63 | */ |
||
64 | View Code Duplication | public static function isRequiredNonNumeric($variable) |
|
71 | |||
72 | /** |
||
73 | * Remove anything that isn't a number from a string |
||
74 | * @param string $string This should be the string that you want to remove any none numerical characters from |
||
75 | * @return string The filtered string is returned |
||
76 | */ |
||
77 | public static function removeNoneNumeric($string) |
||
81 | |||
82 | /** |
||
83 | * Removes anything that isn't in the alphabet A-Z |
||
84 | * @param string $string This should be the string that you want to remove all none alphabetical characters |
||
85 | * @return string The filtered string is returned |
||
86 | */ |
||
87 | public static function removeNoneAlpha($string) |
||
91 | |||
92 | /** |
||
93 | * Removes all non Alpha-numeric characters from a string |
||
94 | * @param string $string The string that you want to remove any non alpha-numerical characters from |
||
95 | * @return string The filtered string is returned |
||
96 | */ |
||
97 | public static function removeNoneAlphaNumeric($string) |
||
101 | |||
102 | /** |
||
103 | * Checks to see if array contains all of the required fields and are not empty |
||
104 | * @param array $mustContain an array of the values that must be included in the array |
||
105 | * @param array $valueArray The array you are checking for the values |
||
106 | * @return boolean If all of the fields exist and are not empty will return true else returns false |
||
107 | */ |
||
108 | public static function arrayMustContainFields($mustContain, $valueArray) |
||
123 | } |
||
124 |