Complex classes like Solver 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 Solver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Solver |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var Path |
||
| 36 | */ |
||
| 37 | private $path; |
||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | private $users; |
||
| 42 | /** |
||
| 43 | * @var string[] |
||
| 44 | */ |
||
| 45 | private $meetings; |
||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $timeSlots = 0; |
||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | private $maxTimeSlots = 0; |
||
| 54 | /** |
||
| 55 | * @var string[] |
||
| 56 | */ |
||
| 57 | private $meetingsAvailability; |
||
| 58 | /** |
||
| 59 | * @var string[] |
||
| 60 | */ |
||
| 61 | private $meetingsDuration; |
||
| 62 | /** |
||
| 63 | * @var string[] |
||
| 64 | */ |
||
| 65 | private $usersAvailability; |
||
| 66 | /** |
||
| 67 | * @var string[] |
||
| 68 | */ |
||
| 69 | private $usersMeetings; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Schedule laravel schedule object needed to perform command in background |
||
| 73 | */ |
||
| 74 | private $schedule; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var Application |
||
| 78 | */ |
||
| 79 | private $laravel; |
||
| 80 | |||
| 81 | //TODo clone function rmemeber to clone also path or create a new one |
||
| 82 | //TODO mehtod to check if all variables are correctly set |
||
| 83 | //TODO check no duplicates |
||
| 84 | //TODO exception if glpsol return erros |
||
| 85 | //TODO intercept all erros of system calls like mkdir |
||
| 86 | //TODO to_string |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Solver constructor. |
||
| 90 | * @param Schedule $schedule |
||
| 91 | * @param Application $laravel |
||
| 92 | * @throws OptimiseException on general problems |
||
| 93 | */ |
||
| 94 | 4 | public function __construct(Schedule $schedule, Application $laravel) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @throws OptimiseException |
||
| 104 | */ |
||
| 105 | 4 | static private function checkGlpsol() |
|
| 106 | { |
||
| 107 | 4 | if (!(`which glpsol`)) |
|
| 108 | 4 | throw new OptimiseException('glpsol is not installed'); |
|
| 109 | 4 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @throws OptimiseException |
||
| 113 | */ |
||
| 114 | 2 | public function __destruct() |
|
| 115 | { |
||
| 116 | 2 | $this->path = null; //call the path destruct |
|
| 117 | 2 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return Path |
||
| 121 | */ |
||
| 122 | public function getPath() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return Schedule |
||
| 129 | */ |
||
| 130 | public function getSchedule() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Schedule $schedule |
||
| 137 | * @return Solver |
||
| 138 | */ |
||
| 139 | public function setSchedule($schedule) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return \string[] |
||
| 147 | */ |
||
| 148 | 2 | public function getUsers() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param string[] $users |
||
| 155 | * @return Solver |
||
| 156 | */ |
||
| 157 | 4 | public function setUsers($users) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return string[] |
||
| 165 | */ |
||
| 166 | 2 | public function getMeetings() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param string[] $meetings |
||
| 173 | * @return Solver |
||
| 174 | */ |
||
| 175 | 4 | public function setMeetings($meetings) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | public function getTimeSlots() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param int $timeSlots |
||
| 191 | * @return Solver |
||
| 192 | * @throws OptimiseException |
||
| 193 | */ |
||
| 194 | 4 | public function setTimeSlots($timeSlots) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | public function getMaxTimeSlots() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param int $maxTimeSlots |
||
| 213 | * @return Solver |
||
| 214 | * @throws OptimiseException |
||
| 215 | */ |
||
| 216 | 4 | public function setMaxTimeSlots($maxTimeSlots) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return string[] |
||
| 227 | */ |
||
| 228 | public function getMeetingsAvailability() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string[] $meetingsAvailability |
||
| 235 | * @return Solver |
||
| 236 | * @throws OptimiseException |
||
| 237 | */ |
||
| 238 | 4 | public function setMeetingsAvailability($meetingsAvailability) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * implementation of arraypad that doesn't change original keys<br/> |
||
| 256 | * <strong>CAUTION: Only positive $len</strong> |
||
| 257 | * @param array $array |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | 4 | static private function arrayPad(array $array, $len, $pad) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @return string[] |
||
| 270 | */ |
||
| 271 | public function getMeetingsDuration() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string[] $meetingsDuration |
||
| 278 | * @return Solver |
||
| 279 | * @throws OptimiseException |
||
| 280 | */ |
||
| 281 | 4 | public function setMeetingsDuration($meetingsDuration) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return string[] |
||
| 300 | */ |
||
| 301 | public function getUsersAvailability() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string[] $usersAvailability |
||
| 308 | * @return Solver |
||
| 309 | * @throws OptimiseException |
||
| 310 | */ |
||
| 311 | 4 | public function setUsersAvailability($usersAvailability) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return string[] |
||
| 330 | */ |
||
| 331 | public function getUsersMeetings() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string[] $usersMeetings |
||
| 338 | * @return Solver |
||
| 339 | * @throws OptimiseException |
||
| 340 | */ |
||
| 341 | 4 | public function setUsersMeetings($usersMeetings) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return Solver |
||
| 358 | * @throws OptimiseException |
||
| 359 | */ |
||
| 360 | 4 | public function solve() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @throws OptimiseException |
||
| 374 | */ |
||
| 375 | 4 | private function writeData() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @throws OptimiseException |
||
| 388 | */ |
||
| 389 | 4 | private function checkData() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @param $proprieties |
||
| 397 | * @throws OptimiseException |
||
| 398 | */ |
||
| 399 | 4 | private function checkArrayProprieties($proprieties) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param $proprieties |
||
| 408 | * @throws OptimiseException |
||
| 409 | */ |
||
| 410 | 4 | private function checkIntProprieties($proprieties) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @throws OptimiseException |
||
| 419 | */ |
||
| 420 | 4 | private function writeUsers() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $file |
||
| 427 | * @param array $data |
||
| 428 | * @throws OptimiseException |
||
| 429 | */ |
||
| 430 | 4 | static private function writeCSVArrayNoKey($file, $data) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $file |
||
| 443 | * @param array $data |
||
| 444 | * @param array $heading |
||
| 445 | * @param \Closure $writer |
||
| 446 | * @throws OptimiseException |
||
| 447 | */ |
||
| 448 | 4 | static private function writeCSV($file, $data, $heading, \Closure $writer) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @throws OptimiseException |
||
| 465 | */ |
||
| 466 | 4 | private function writeMeetings() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @throws OptimiseException |
||
| 473 | */ |
||
| 474 | 4 | private function writeMeetingsDuration() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param string $file |
||
| 481 | * @param array $data |
||
| 482 | * @param string $name |
||
| 483 | * @throws OptimiseException |
||
| 484 | */ |
||
| 485 | 4 | static private function writeCSVArray($file, $data, $name) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @throws OptimiseException |
||
| 498 | */ |
||
| 499 | 4 | private function writeMeetingsAvailability() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $file |
||
| 506 | * @param array $data |
||
| 507 | * @param string $name |
||
| 508 | * @throws OptimiseException |
||
| 509 | */ |
||
| 510 | static private function writeCSVMatrix($file, $data, $name) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @throws OptimiseException |
||
| 524 | */ |
||
| 525 | 4 | private function writeUsersAvailability() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * @throws OptimiseException |
||
| 532 | */ |
||
| 533 | 4 | private function writeUsersMeetings() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @throws OptimiseException |
||
| 540 | */ |
||
| 541 | 4 | private function writeModelFile() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @return array |
||
| 554 | * @throws OptimiseException |
||
| 555 | */ |
||
| 556 | 4 | public function getXResults() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @param string $file |
||
| 563 | * @return array |
||
| 564 | * @throws OptimiseException |
||
| 565 | */ |
||
| 566 | 4 | static private function readCSVFile($file) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * @return array |
||
| 593 | * @throws OptimiseException |
||
| 594 | */ |
||
| 595 | 4 | public function getYResults() |
|
| 599 | |||
| 600 | /** |
||
| 601 | * @return string |
||
| 602 | * @throws OptimiseException |
||
| 603 | */ |
||
| 604 | public function getOutput() |
||
| 610 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.