| Total Complexity | 83 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Calculators 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 Calculators, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | trait Calculators |
||
| 6 | { |
||
| 7 | use Digit; |
||
| 8 | |||
| 9 | public function classA($arr, $len = 1) |
||
| 10 | { |
||
| 11 | return $this->ones[$arr[0]]; |
||
| 12 | } |
||
| 13 | |||
| 14 | public function classB($arr, $len = 2) |
||
| 15 | { |
||
| 16 | if ($this->before_comma_len >= 2) { |
||
| 17 | $ten_number_index = $this->before_comma_len - 2; |
||
| 18 | $single_number_index = $this->before_comma_len - 1; |
||
| 19 | $detecd_array = [ |
||
| 20 | $this->before_comma_array[$ten_number_index], |
||
| 21 | $this->before_comma_array[$single_number_index], |
||
| 22 | ]; |
||
| 23 | if ($arr == $detecd_array) { |
||
| 24 | if ($arr[0] == 0 and $arr[1] >= 1 and $arr[1] <= 10) { |
||
| 25 | $this->is_main1_currency = true; |
||
| 26 | } elseif ($arr[0] >= 1 and $arr[1] >= 1 and $arr[1] <= 9) { |
||
| 27 | $this->is_main1_currency = false; |
||
| 28 | } elseif ($arr[0] >= 2) { |
||
| 29 | $this->is_main1_currency = false; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | if ($arr[0] == 0 and $arr[1] == 0) { |
||
| 35 | return ''; |
||
| 36 | } |
||
| 37 | |||
| 38 | if ($arr[0] == 0) { |
||
| 39 | return $this->ones[$arr[1]]; |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($arr[0] == 1 and $arr[1] == 1) { |
||
| 43 | return $this->ones[11]; |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($arr[0] == 1 and $arr[1] == 0) { |
||
| 47 | return $this->ones[10]; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($arr[1] == 0) { |
||
| 51 | return $this->tens[$arr[0]]; |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($arr[0] > 1) { |
||
| 55 | return $this->ones[$arr[1]].$this->config['connection_tool'].$this->tens[$arr[0]]; |
||
| 56 | } |
||
| 57 | |||
| 58 | if (in_array($arr[1], [1, 2])) { |
||
| 59 | return $this->others[$arr[1]].' '.$this->tens[$arr[0]]; |
||
| 60 | } |
||
| 61 | |||
| 62 | return $this->ones[$arr[1]].' '.$this->tens[$arr[0]]; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function classC($arr, $len = 3) |
||
| 66 | { |
||
| 67 | if ($arr[0] == 0 and $arr[1] == 0 and $arr[2] == 0) { |
||
| 68 | return ''; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($arr[0] == 0 and $arr[1] == 0) { |
||
| 72 | return $this->ones[$arr[2]]; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($arr[0] == 0) { |
||
| 76 | return $this->classB([$arr[1], $arr[2]]); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($arr[2] == 0 and $arr[1] == 0) { |
||
| 80 | return $this->hundreds[$arr[0]]; |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($arr[1] != 0) { |
||
| 84 | return $this->hundreds[$arr[0]].$this->config['connection_tool'].$this->classB([$arr[1], $arr[2]]); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $this->hundreds[$arr[0]].$this->config['connection_tool'].$this->classA([$arr[2]]); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function classD($arr, $len = 4) |
||
| 91 | { |
||
| 92 | $classC = [$arr[1], $arr[2], $arr[3]]; |
||
| 93 | |||
| 94 | if ($arr[0] <= 2) { |
||
| 95 | $thousands = $this->thousands[$arr[0]]; |
||
| 96 | } else { |
||
| 97 | $thousands = $this->ones[$arr[0]].' '.$this->thousands['39']; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($arr[1] == 0 and $arr[2] == 0 and $arr[3] == 0) { |
||
| 101 | return $thousands; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $thousands.$this->config['connection_tool'].$this->classC($classC); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function classE($arr, $len = 5) |
||
| 108 | { |
||
| 109 | $classC = [$arr[2], $arr[3], $arr[4]]; |
||
| 110 | |||
| 111 | if ($arr[0] != 0) { |
||
| 112 | $conn = ' '; |
||
| 113 | |||
| 114 | if ($arr[1] >= 2 and $arr[0] > 1) { |
||
| 115 | $conn = $this->config['connection_tool']; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (in_array($arr[1], [2, 1])) { |
||
| 119 | $thousands = $this->others[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 120 | } else { |
||
| 121 | $thousands = $this->ones[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($arr[1] == 0) { |
||
| 125 | if ($arr[0] == 1) { |
||
| 126 | $thousands = $this->ones[10]; |
||
| 127 | $thousands .= ' '.$this->thousands[39]; |
||
| 128 | } else { |
||
| 129 | $thousands = $this->tens[$arr[0]]; |
||
| 130 | $thousands .= ' '.$this->thousands[1]; |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | if ($arr[2] == 0 and $arr[3] == 0 and $arr[4] == 0) { |
||
| 134 | $thousands .= ' '.$this->thousands[1]; |
||
| 135 | } else { |
||
| 136 | $thousands .= ' '.$this->thousands[1199]; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | if (in_array($arr[1], [2, 1])) { |
||
| 141 | $thousands = $this->others[$arr[1]].' '; |
||
| 142 | } else { |
||
| 143 | $thousands = $this->ones[$arr[1]].' '; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($arr[1] == 0) { |
||
| 147 | $thousands = $this->tens[$arr[2]]; |
||
| 148 | } |
||
| 149 | |||
| 150 | $thousands .= ' '.$this->thousands[39]; |
||
| 151 | } |
||
| 152 | if ($this->classC($classC) != '') { |
||
| 153 | return $thousands.$this->config['connection_tool'].$this->classC($classC); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $thousands; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function classF($arr, $len = 6) |
||
| 160 | { |
||
| 161 | $classC = [$arr[3], $arr[4], $arr[5]]; |
||
| 162 | if ($arr[0] != 0) { |
||
| 163 | if ($arr[1] == 0 && $arr[2] == 0) { |
||
| 164 | $thousands = $this->hundreds[$arr[0]].' '.$this->thousands[1]; |
||
| 165 | } else { |
||
| 166 | if ($arr[1] == 0) { |
||
| 167 | $thousands = $this->hundreds[$arr[0]].$this->config['connection_tool']. |
||
| 168 | $this->ones[$arr[2]].' '.$this->thousands[1]; |
||
| 169 | } elseif ($arr[2] == 0) { |
||
| 170 | if ($arr[3] == 0 and $arr[4] == 0 and $arr[5] == 0) { |
||
| 171 | $thousands = $this->hundreds[$arr[0]].$this->config['connection_tool']. |
||
| 172 | $this->tens[$arr[1]].' '.$this->thousands[1]; |
||
| 173 | } else { |
||
| 174 | $thousands = $this->hundreds[$arr[0]].$this->config['connection_tool']. |
||
| 175 | $this->tens[$arr[1]].' '.$this->thousands[1199]; |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | // $thousands_lang = $this->thousands[1199]; |
||
| 179 | if ($arr[1] == 0 and $arr[2] >= 1 and $arr[1] <= 10) { |
||
| 180 | $thousands_lang = $this->thousands[1]; |
||
| 181 | } elseif ($arr[1] >= 1 and $arr[2] >= 1 and $arr[1] <= 9) { |
||
| 182 | $thousands_lang = $this->thousands[1199]; |
||
| 183 | } elseif ($arr[1] >= 2) { |
||
| 184 | $thousands_lang = $this->thousands[1199]; |
||
| 185 | } |
||
| 186 | |||
| 187 | $thousands = $this->classC([$arr[0], $arr[1], $arr[2]], 3).' '.$thousands_lang; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | return $this->classE([$arr[1], $arr[2], $arr[3], $arr[4], $arr[5]]); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($this->classC($classC) != '') { |
||
| 195 | return $thousands.$this->config['connection_tool'].$this->classC($classC); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $thousands; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function classG($arr,$len = 7) |
||
| 202 | { |
||
| 203 | //dd($arr,$len); |
||
| 204 | //$classC = [$arr[2],$arr[4],$arr[5]]; |
||
| 205 | $classC = [$arr[1],$arr[2],$arr[3]]; |
||
| 206 | //$classE = [$arr[1],$arr[2],$arr[3],$arr[4],$arr[5],$arr[6]]; |
||
| 207 | |||
| 208 | <<<<<<< HEAD |
||
|
|
|||
| 209 | if($arr[0]<=2) |
||
| 210 | $million = $this->millions[$arr[0]] ; |
||
| 211 | else |
||
| 212 | $million = $this->ones[$arr[0]] . ' ' . $this->millions['39']; |
||
| 213 | ======= |
||
| 214 | if ($arr[0] <= 2) { |
||
| 215 | $million = $this->millions[$arr[0]]; |
||
| 216 | } else { |
||
| 217 | $million = $this->ones[$arr[0]].' '.$this->millions['39']; |
||
| 218 | } |
||
| 219 | >>>>>>> c887a4394d3176241568f57202b5a8e8a73b160a |
||
| 220 | |||
| 221 | if($this->classC($classC)!='') |
||
| 222 | return $million . $this->config['connection_tool'] . $this->classC($classC); |
||
| 223 | return $million; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | // public function classH($arr,$len = 8) |
||
| 228 | // { |
||
| 229 | // $classC = [$arr[2],$arr[4],$arr[5]]; |
||
| 230 | // //$classC = [$arr[1],$arr[2],$arr[3]]; |
||
| 231 | // $classE = [$arr[1],$arr[2],$arr[3],$arr[4],$arr[5],$arr[6],$arr[7]]; |
||
| 232 | // |
||
| 233 | // if($arr[0]<=2) |
||
| 234 | // $million = $this->millions[$arr[0]] ; |
||
| 235 | // else |
||
| 236 | // $million = $this->ones[$arr[0]] . ' ' . $this->millions['39']; |
||
| 237 | // |
||
| 238 | // return $million . $this->config['connection_tool'] . $this->classG($classE); |
||
| 239 | // |
||
| 240 | // } |
||
| 241 | public function classH($arr, $len = 8) |
||
| 242 | { |
||
| 243 | $classF = [$arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7]]; |
||
| 244 | |||
| 245 | if ($arr[0] != 0) { |
||
| 246 | $conn = ' '; |
||
| 247 | |||
| 248 | if ($arr[1] >= 2 and $arr[0] > 1) { |
||
| 249 | $conn = $this->config['connection_tool']; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (in_array($arr[1], [2, 1])) { |
||
| 253 | $million = $this->others[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 254 | } else { |
||
| 255 | $million = $this->ones[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 256 | } |
||
| 257 | |||
| 258 | if ($arr[1] == 0) { |
||
| 259 | if ($arr[0] == 1) { |
||
| 260 | $million = $this->ones[10]; |
||
| 261 | $million .= ' '.$this->millions[39]; |
||
| 262 | } else { |
||
| 263 | $million = $this->tens[$arr[0]]; |
||
| 264 | $million .= ' '.$this->millions[1]; |
||
| 265 | } |
||
| 266 | } else { |
||
| 267 | if ($arr[2] == 0 and $arr[3] == 0 and $arr[4] == 0) { |
||
| 268 | $million .= ' '.$this->millions[1]; |
||
| 269 | } else { |
||
| 270 | $million .= ' '.$this->millions[1199]; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } else { |
||
| 274 | if (in_array($arr[1], [2, 1])) { |
||
| 275 | $million = $this->others[$arr[1]].' '; |
||
| 276 | } else { |
||
| 277 | $million = $this->ones[$arr[1]].' '; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($arr[1] == 0) { |
||
| 281 | $million = $this->tens[$arr[2]]; |
||
| 282 | } |
||
| 283 | |||
| 284 | $million .= ' '.$this->millions[39]; |
||
| 285 | } |
||
| 286 | // if(in_array($arr[1],[2])) |
||
| 287 | // $million = $this->others[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 288 | // else |
||
| 289 | // $million = $this->ones[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 290 | |||
| 291 | //$million = $this->ones[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 292 | |||
| 293 | // if($arr[1] == 0) |
||
| 294 | // { |
||
| 295 | // $million = $this->tens[$arr[0]] ; |
||
| 296 | // } |
||
| 297 | |||
| 298 | //$million.=' ' . $this->millions[1199]; |
||
| 299 | |||
| 300 | return $million.$this->config['connection_tool'].$this->classF($classF); |
||
| 301 | } |
||
| 302 | |||
| 303 | public function classI($arr, $len = 9) |
||
| 304 | { |
||
| 305 | $classF = [$arr[3], $arr[4], $arr[5], $arr[6], $arr[7], $arr[8]]; |
||
| 306 | |||
| 307 | if (in_array($arr[1], [2])) { |
||
| 308 | $million = $this->others[$arr[1]].$this->config['connection_tool'].$this->tens[$arr[0]]; |
||
| 309 | } else { |
||
| 322 |