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 | 2 | public function __destruct() |
|
109 | |||
110 | /** |
||
111 | * @throws OptimiseException |
||
112 | */ |
||
113 | 4 | static private function checkGlpsol() |
|
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 | * @return string[] |
||
256 | */ |
||
257 | public function getMeetingsDuration() |
||
261 | |||
262 | /** |
||
263 | * @param string[] $meetingsDuration |
||
264 | * @return Solver |
||
265 | * @throws OptimiseException |
||
266 | */ |
||
267 | 4 | public function setMeetingsDuration($meetingsDuration) |
|
283 | |||
284 | /** |
||
285 | * @return string[] |
||
286 | */ |
||
287 | public function getUsersAvailability() |
||
291 | |||
292 | /** |
||
293 | * @param string[] $usersAvailability |
||
294 | * @return Solver |
||
295 | * @throws OptimiseException |
||
296 | */ |
||
297 | 4 | public function setUsersAvailability($usersAvailability) |
|
313 | |||
314 | /** |
||
315 | * @return string[] |
||
316 | */ |
||
317 | public function getUsersMeetings() |
||
321 | |||
322 | /** |
||
323 | * @param string[] $usersMeetings |
||
324 | * @return Solver |
||
325 | * @throws OptimiseException |
||
326 | */ |
||
327 | 4 | public function setUsersMeetings($usersMeetings) |
|
341 | |||
342 | /** |
||
343 | * @throws OptimiseException |
||
344 | */ |
||
345 | 4 | private function writeUsers() |
|
349 | |||
350 | /** |
||
351 | * @throws OptimiseException |
||
352 | */ |
||
353 | 4 | private function writeMeetings() |
|
357 | |||
358 | /** |
||
359 | * @throws OptimiseException |
||
360 | */ |
||
361 | 4 | private function writeMeetingsDuration() |
|
365 | |||
366 | /** |
||
367 | * @throws OptimiseException |
||
368 | */ |
||
369 | 4 | private function writeMeetingsAvailability() |
|
373 | |||
374 | /** |
||
375 | * @throws OptimiseException |
||
376 | */ |
||
377 | 4 | private function writeUsersAvailability() |
|
381 | |||
382 | /** |
||
383 | * @throws OptimiseException |
||
384 | */ |
||
385 | 4 | private function writeUsersMeetings() |
|
389 | |||
390 | /** |
||
391 | * @param string $file |
||
392 | * @param array $data |
||
393 | * @throws OptimiseException |
||
394 | */ |
||
395 | 4 | static private function writeCSVArrayNoKey($file, $data) |
|
405 | |||
406 | /** |
||
407 | * @param string $file |
||
408 | * @param array $data |
||
409 | * @param string $name |
||
410 | * @throws OptimiseException |
||
411 | */ |
||
412 | 4 | static private function writeCSVArray($file, $data, $name) |
|
422 | |||
423 | /** |
||
424 | * @param string $file |
||
425 | * @param array $data |
||
426 | * @param string $name |
||
427 | * @throws OptimiseException |
||
428 | */ |
||
429 | 4 | static private function writeCSVMatrix($file, $data, $name) |
|
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 | * @return Solver |
||
465 | * @throws OptimiseException |
||
466 | */ |
||
467 | 4 | public function solve() |
|
477 | |||
478 | /** |
||
479 | * @throws OptimiseException |
||
480 | */ |
||
481 | 4 | private function writeModelFile() |
|
491 | |||
492 | /** |
||
493 | * @return array |
||
494 | * @throws OptimiseException |
||
495 | */ |
||
496 | 4 | public function getXResults() |
|
500 | |||
501 | /** |
||
502 | * @return array |
||
503 | * @throws OptimiseException |
||
504 | */ |
||
505 | 4 | public function getYResults() |
|
509 | |||
510 | /** |
||
511 | * @return string |
||
512 | * @throws OptimiseException |
||
513 | */ |
||
514 | public function getOutput() |
||
520 | |||
521 | /** |
||
522 | * @param string $file |
||
523 | * @return array |
||
524 | * @throws OptimiseException |
||
525 | */ |
||
526 | 4 | static private function readCSVFile($file) |
|
550 | |||
551 | /** |
||
552 | * @throws OptimiseException |
||
553 | */ |
||
554 | 4 | private function writeData() |
|
564 | |||
565 | /** |
||
566 | * @throws OptimiseException |
||
567 | */ |
||
568 | 4 | private function checkData() |
|
573 | |||
574 | /** |
||
575 | * @param $proprieties |
||
576 | * @throws OptimiseException |
||
577 | */ |
||
578 | 4 | private function checkArrayProprieties($proprieties) |
|
584 | |||
585 | /** |
||
586 | * @param $proprieties |
||
587 | * @throws OptimiseException |
||
588 | */ |
||
589 | 4 | private function checkIntProprieties($proprieties) |
|
595 | |||
596 | /** |
||
597 | * implementation of arraypad that doesn't change original keys<br/> |
||
598 | * <strong>CAUTION: Only positive $len</strong> |
||
599 | * @param array $array |
||
600 | * @return array |
||
601 | */ |
||
602 | 4 | static private function arrayPad(array $array, $len, $pad) |
|
609 | } |
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.