| Total Complexity | 42 |
| Total Lines | 222 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NumberFilter 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.
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 NumberFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class NumberFilter extends AbstractFilter |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * Addition |
||
| 62 | * @param mixed $variable |
||
| 63 | * @param mixed $operand |
||
| 64 | * @return float|int|mixed |
||
| 65 | */ |
||
| 66 | public static function plus($variable, $operand) |
||
| 67 | { |
||
| 68 | if (!is_numeric($variable) || !is_numeric($operand)) { |
||
| 69 | return $variable; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (is_float($operand) || is_float($variable)) { |
||
| 73 | return (float) $variable + (float) $operand; |
||
| 74 | } |
||
| 75 | |||
| 76 | return (int) $variable + (int) $operand; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * subtraction |
||
| 81 | * @param mixed $variable |
||
| 82 | * @param mixed $operand |
||
| 83 | * @return int|float|mixed |
||
| 84 | */ |
||
| 85 | public static function minus($variable, $operand) |
||
| 86 | { |
||
| 87 | if (!is_numeric($variable) || !is_numeric($operand)) { |
||
| 88 | return $variable; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (is_float($operand) || is_float($variable)) { |
||
| 92 | return (float) $variable - (float) $operand; |
||
| 93 | } |
||
| 94 | |||
| 95 | return (int) $variable - (int) $operand; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Times |
||
| 100 | * @param mixed $variable |
||
| 101 | * @param mixed $operand |
||
| 102 | * @return int|float|mixed |
||
| 103 | */ |
||
| 104 | public static function times($variable, $operand) |
||
| 105 | { |
||
| 106 | if (!is_numeric($variable) || !is_numeric($operand)) { |
||
| 107 | return $variable; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (is_float($operand) || is_float($variable)) { |
||
| 111 | return (float) $variable * (float) $operand; |
||
| 112 | } |
||
| 113 | |||
| 114 | return (int) $variable * (int) $operand; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Modulo |
||
| 119 | * @param mixed $variable |
||
| 120 | * @param mixed $operand |
||
| 121 | * @return int|float|mixed |
||
| 122 | */ |
||
| 123 | public static function modulo($variable, $operand) |
||
| 124 | { |
||
| 125 | if (!is_numeric($variable) || !is_numeric($operand)) { |
||
| 126 | return $variable; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (is_float($operand) || is_float($variable)) { |
||
| 130 | return fmod((float) $variable, (float) $operand); |
||
| 131 | } |
||
| 132 | |||
| 133 | return fmod((int) $variable, (int) $operand); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Division filter |
||
| 138 | * @param mixed $variable |
||
| 139 | * @param mixed $operand |
||
| 140 | * @return int|float|mixed |
||
| 141 | */ |
||
| 142 | public static function div($variable, $operand) |
||
| 143 | { |
||
| 144 | if (!is_numeric($variable) || !is_numeric($operand) || $operand == 0) { |
||
| 145 | return $variable; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (is_float($operand) || is_float($variable)) { |
||
| 149 | return (float) ($variable / $operand); |
||
| 150 | } |
||
| 151 | |||
| 152 | return (int) ($variable / $operand); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Round the number |
||
| 157 | * @param mixed $variable |
||
| 158 | * @param mixed $number |
||
| 159 | * @return float|mixed |
||
| 160 | */ |
||
| 161 | public static function round($variable, $number = 0) |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Number format |
||
| 172 | * @param mixed $variable |
||
| 173 | * @param mixed $decimal |
||
| 174 | * @param string $decimalPoint |
||
| 175 | * @param string $separator |
||
| 176 | * @return float|mixed |
||
| 177 | */ |
||
| 178 | public static function format( |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Number format for money |
||
| 198 | * @param mixed $variable |
||
| 199 | * @param mixed $decimal |
||
| 200 | * @param string $decimalPoint |
||
| 201 | * @param string $separator |
||
| 202 | * @return float|mixed |
||
| 203 | */ |
||
| 204 | public static function formatMoney( |
||
| 205 | $variable, |
||
| 206 | $decimal = 0, |
||
| 207 | $decimalPoint = '.', |
||
| 208 | $separator = ',' |
||
| 209 | ) { |
||
| 210 | if (!is_numeric($variable)) { |
||
| 211 | return $variable; |
||
| 212 | } |
||
| 213 | |||
| 214 | $number = (string) $variable; |
||
| 215 | if (strpos($number, '.') === false && strpos($number, ',') === false) { |
||
| 216 | $decimal = 0; |
||
| 217 | } |
||
| 218 | |||
| 219 | return number_format( |
||
| 220 | (float) $variable, |
||
| 221 | (int) $decimal, |
||
| 222 | $decimalPoint, |
||
| 223 | $separator |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return the given number to string |
||
| 229 | * @param mixed $variable |
||
| 230 | * @return string|mixed |
||
| 231 | */ |
||
| 232 | public static function numberToString($variable) |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Units format |
||
| 256 | * @param mixed $variable |
||
| 257 | * @param mixed $precision |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public static function sizeFormat( |
||
| 282 |