| Total Complexity | 84 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 3 | 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) |
||
| 12 | } |
||
| 13 | |||
| 14 | public function classB($arr, $len = 2) |
||
| 63 | } |
||
| 64 | |||
| 65 | public function classC($arr, $len = 3) |
||
| 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) |
||
| 199 | } |
||
| 200 | |||
| 201 | public function classG($arr,$len = 7) |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | // public function classH($arr,$len = 8) |
||
| 220 | // { |
||
| 221 | // $classC = [$arr[2],$arr[4],$arr[5]]; |
||
| 222 | // //$classC = [$arr[1],$arr[2],$arr[3]]; |
||
| 223 | // $classE = [$arr[1],$arr[2],$arr[3],$arr[4],$arr[5],$arr[6],$arr[7]]; |
||
| 224 | // |
||
| 225 | // if($arr[0]<=2) |
||
| 226 | // $million = $this->millions[$arr[0]] ; |
||
| 227 | // else |
||
| 228 | // $million = $this->ones[$arr[0]] . ' ' . $this->millions['39']; |
||
| 229 | // |
||
| 230 | // return $million . $this->config['connection_tool'] . $this->classG($classE); |
||
| 231 | // |
||
| 232 | // } |
||
| 233 | public function classH($arr, $len = 8) |
||
| 234 | { |
||
| 235 | $classF = [$arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7]]; |
||
| 236 | |||
| 237 | if ($arr[0] != 0) { |
||
| 238 | $conn = ' '; |
||
| 239 | |||
| 240 | if ($arr[1] >= 2 and $arr[0] > 1) { |
||
| 241 | $conn = $this->config['connection_tool']; |
||
| 242 | } |
||
| 243 | |||
| 244 | if (in_array($arr[1], [2, 1])) { |
||
| 245 | $million = $this->others[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 246 | } else { |
||
| 247 | $million = $this->ones[$arr[1]].$conn.$this->tens[$arr[0]]; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($arr[1] == 0) { |
||
| 251 | if ($arr[0] == 1) { |
||
| 252 | $million = $this->ones[10]; |
||
| 253 | $million .= ' '.$this->millions[39]; |
||
| 254 | } else { |
||
| 255 | $million = $this->tens[$arr[0]]; |
||
| 256 | $million .= ' '.$this->millions[1]; |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | if ($arr[2] == 0 and $arr[3] == 0 and $arr[4] == 0) { |
||
| 260 | $million .= ' '.$this->millions[1]; |
||
| 261 | } else { |
||
| 262 | $million .= ' '.$this->millions[1199]; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } else { |
||
| 266 | if (in_array($arr[1], [2, 1])) { |
||
| 267 | $million = $this->others[$arr[1]].' '; |
||
| 268 | } else { |
||
| 269 | $million = $this->ones[$arr[1]].' '; |
||
| 270 | } |
||
| 271 | |||
| 272 | if ($arr[1] == 0) { |
||
| 273 | $million = $this->tens[$arr[2]]; |
||
| 274 | } |
||
| 275 | |||
| 276 | $million .= ' '.$this->millions[39]; |
||
| 277 | } |
||
| 278 | // if(in_array($arr[1],[2])) |
||
| 279 | // $million = $this->others[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 280 | // else |
||
| 281 | // $million = $this->ones[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 282 | |||
| 283 | //$million = $this->ones[$arr[1]] . $this->config['connection_tool'] . $this->tens[$arr[0]] ; |
||
| 284 | |||
| 285 | // if($arr[1] == 0) |
||
| 286 | // { |
||
| 287 | // $million = $this->tens[$arr[0]] ; |
||
| 288 | // } |
||
| 289 | |||
| 290 | //$million.=' ' . $this->millions[1199]; |
||
| 291 | |||
| 292 | return $million.$this->config['connection_tool'].$this->classF($classF); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function classI($arr, $len = 9) |
||
| 312 | } |
||
| 313 | } |
||
| 314 |