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 string |
||
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 |
||
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 | 2 | function __destruct() |
|
110 | |||
111 | /** |
||
112 | * @throws OptimiseException |
||
113 | */ |
||
114 | 4 | static private function checkGlpsol() |
|
119 | |||
120 | /** |
||
121 | * remove a no empty dir |
||
122 | * @param $dir |
||
123 | * @return bool |
||
124 | */ |
||
125 | 2 | private static function delTree($dir) { |
|
132 | |||
133 | |||
134 | /** |
||
135 | * @throws OptimiseException on problems during creation of tmp dir |
||
136 | */ |
||
137 | 4 | private function createPath() |
|
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getPath() |
||
156 | |||
157 | /** |
||
158 | * @return Schedule |
||
159 | */ |
||
160 | public function getSchedule() |
||
164 | |||
165 | /** |
||
166 | * @param Schedule $schedule |
||
167 | * @return Solver |
||
168 | */ |
||
169 | public function setSchedule($schedule) |
||
174 | |||
175 | /** |
||
176 | * @return \string[] |
||
177 | */ |
||
178 | 2 | public function getUsers() |
|
182 | |||
183 | /** |
||
184 | * @param \string[] $users |
||
185 | * @return Solver |
||
186 | */ |
||
187 | 4 | public function setUsers($users) |
|
192 | |||
193 | /** |
||
194 | * @return \string[] |
||
195 | */ |
||
196 | 2 | public function getMeetings() |
|
200 | |||
201 | /** |
||
202 | * @param \string[] $meetings |
||
203 | * @return Solver |
||
204 | */ |
||
205 | 4 | public function setMeetings($meetings) |
|
210 | |||
211 | /** |
||
212 | * @return int |
||
213 | */ |
||
214 | public function getTimeSlots() |
||
218 | |||
219 | /** |
||
220 | * @param int $timeSlots |
||
221 | * @return Solver |
||
222 | * @throws OptimiseException |
||
223 | */ |
||
224 | 4 | public function setTimeSlots($timeSlots) |
|
232 | |||
233 | /** |
||
234 | * @return int |
||
235 | */ |
||
236 | public function getMaxTimeSlots() |
||
240 | |||
241 | /** |
||
242 | * @param int $maxTimeSlots |
||
243 | * @return Solver |
||
244 | * @throws OptimiseException |
||
245 | */ |
||
246 | 4 | public function setMaxTimeSlots($maxTimeSlots) |
|
254 | |||
255 | /** |
||
256 | * @return \string[] |
||
257 | */ |
||
258 | public function getMeetingsAvailability() |
||
262 | |||
263 | /** |
||
264 | * @param \string[] $meetingsAvailability |
||
265 | * @return Solver |
||
266 | * @throws OptimiseException |
||
267 | */ |
||
268 | 4 | public function setMeetingsAvailability($meetingsAvailability) |
|
283 | |||
284 | /** |
||
285 | * @return \string[] |
||
286 | */ |
||
287 | public function getMeetingsDuration() |
||
291 | |||
292 | /** |
||
293 | * @param \string[] $meetingsDuration |
||
294 | * @return Solver |
||
295 | * @throws OptimiseException |
||
296 | */ |
||
297 | 4 | public function setMeetingsDuration($meetingsDuration) |
|
313 | |||
314 | /** |
||
315 | * @return \string[] |
||
316 | */ |
||
317 | public function getUsersAvailability() |
||
321 | |||
322 | /** |
||
323 | * @param \string[] $usersAvailability |
||
324 | * @return Solver |
||
325 | * @throws OptimiseException |
||
326 | */ |
||
327 | 4 | public function setUsersAvailability($usersAvailability) |
|
343 | |||
344 | /** |
||
345 | * @return \string[] |
||
346 | */ |
||
347 | public function getUsersMeetings() |
||
351 | |||
352 | /** |
||
353 | * @param \string[] $usersMeetings |
||
354 | * @return Solver |
||
355 | * @throws OptimiseException |
||
356 | */ |
||
357 | 4 | public function setUsersMeetings($usersMeetings) |
|
371 | |||
372 | /** |
||
373 | * @throws OptimiseException |
||
374 | */ |
||
375 | 4 | private function writeUsers() |
|
379 | |||
380 | /** |
||
381 | * @throws OptimiseException |
||
382 | */ |
||
383 | 4 | private function writeMeetings() |
|
387 | |||
388 | /** |
||
389 | * @throws OptimiseException |
||
390 | */ |
||
391 | 4 | private function writeMeetingsDuration() |
|
395 | |||
396 | /** |
||
397 | * @throws OptimiseException |
||
398 | */ |
||
399 | 4 | private function writeMeetingsAvailability() |
|
403 | |||
404 | /** |
||
405 | * @throws OptimiseException |
||
406 | */ |
||
407 | 4 | private function writeUsersAvailability() |
|
411 | |||
412 | /** |
||
413 | * @throws OptimiseException |
||
414 | */ |
||
415 | 4 | private function writeUsersMeetings() |
|
419 | |||
420 | /** |
||
421 | * @param string $file |
||
422 | * @param array $data |
||
423 | * @throws OptimiseException |
||
424 | */ |
||
425 | 4 | static private function writeCSVArrayNoKey($file, $data) |
|
435 | |||
436 | /** |
||
437 | * @param string $file |
||
438 | * @param array $data |
||
439 | * @param string $name |
||
440 | * @throws OptimiseException |
||
441 | */ |
||
442 | 4 | static private function writeCSVArray($file, $data, $name) |
|
452 | |||
453 | /** |
||
454 | * @param string $file |
||
455 | * @param array $data |
||
456 | * @param string $name |
||
457 | * @throws OptimiseException |
||
458 | */ |
||
459 | 4 | static private function writeCSVMatrix($file, $data, $name) |
|
470 | |||
471 | /** |
||
472 | * @param string $file |
||
473 | * @param array $data |
||
474 | * @param array $heading |
||
475 | * @param \Closure $writer |
||
476 | * @throws OptimiseException |
||
477 | */ |
||
478 | 4 | static private function writeCSV($file, $data, $heading, \Closure $writer) |
|
492 | |||
493 | /** |
||
494 | * @return Solver |
||
495 | * @throws OptimiseException |
||
496 | */ |
||
497 | 4 | public function solve() |
|
507 | |||
508 | /** |
||
509 | * @throws OptimiseException |
||
510 | */ |
||
511 | 4 | private function writeModelFile() |
|
521 | |||
522 | /** |
||
523 | * @return array |
||
524 | * @throws OptimiseException |
||
525 | */ |
||
526 | 4 | public function getXResults() |
|
530 | |||
531 | /** |
||
532 | * @return array |
||
533 | * @throws OptimiseException |
||
534 | */ |
||
535 | 4 | public function getYResults() |
|
539 | |||
540 | /** |
||
541 | * @return string |
||
542 | * @throws OptimiseException |
||
543 | */ |
||
544 | public function getOutput() |
||
550 | |||
551 | /** |
||
552 | * @param string $file |
||
553 | * @return array |
||
554 | * @throws OptimiseException |
||
555 | */ |
||
556 | 4 | static private function readCSVFile($file) |
|
580 | |||
581 | /** |
||
582 | * @return string |
||
583 | */ |
||
584 | 4 | private function getModelPath() |
|
588 | |||
589 | /** |
||
590 | * @return string |
||
591 | */ |
||
592 | 4 | private function getUsersPath() |
|
596 | |||
597 | /** |
||
598 | * @return string |
||
599 | */ |
||
600 | 4 | private function getMeetingsPath() |
|
604 | |||
605 | /** |
||
606 | * @return string |
||
607 | */ |
||
608 | 4 | private function getMeetingsDurationPath() |
|
612 | |||
613 | /** |
||
614 | * @return string |
||
615 | */ |
||
616 | 4 | private function getMeetingsAvailabilityPath() |
|
620 | |||
621 | /** |
||
622 | * @return string |
||
623 | */ |
||
624 | 4 | private function getUsersAvailabilityPath() |
|
628 | |||
629 | /** |
||
630 | * @return string |
||
631 | */ |
||
632 | 4 | private function getUsersMeetingsPath() |
|
636 | |||
637 | /** |
||
638 | * @return string |
||
639 | */ |
||
640 | 4 | private function getXPath() |
|
644 | |||
645 | /** |
||
646 | * @return string |
||
647 | */ |
||
648 | 4 | private function getYPath() |
|
652 | |||
653 | /** |
||
654 | * @return string |
||
655 | */ |
||
656 | 4 | private function getOutputPath() |
|
660 | |||
661 | /** |
||
662 | * @throws OptimiseException |
||
663 | */ |
||
664 | 4 | private function writeData() |
|
674 | |||
675 | /** |
||
676 | * @throws OptimiseException |
||
677 | */ |
||
678 | 4 | private function checkData() |
|
683 | |||
684 | /** |
||
685 | * @param $proprieties |
||
686 | * @throws OptimiseException |
||
687 | */ |
||
688 | 4 | private function checkArrayProprieties($proprieties) |
|
694 | |||
695 | /** |
||
696 | * @param $proprieties |
||
697 | * @throws OptimiseException |
||
698 | */ |
||
699 | 4 | private function checkIntProprieties($proprieties) |
|
705 | |||
706 | /** |
||
707 | * implementation of arraypad that doesn't change original keys<br/> |
||
708 | * <strong>CAUTION: Only positive $len</strong> |
||
709 | * @param array $array |
||
710 | * @return array |
||
711 | */ |
||
712 | 4 | static private function arrayPad(array $array, $len, $pad) |
|
719 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.