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 | 15 | public function __construct(Schedule $schedule, Application $laravel) |
|
101 | |||
102 | /** |
||
103 | * @throws OptimiseException |
||
104 | */ |
||
105 | 15 | static private function checkGlpsol() |
|
106 | { |
||
107 | 15 | if (!(`which glpsol`)) |
|
108 | 10 | throw new OptimiseException('glpsol is not installed'); |
|
109 | 15 | } |
|
110 | |||
111 | /** |
||
112 | * @throws OptimiseException |
||
113 | */ |
||
114 | 8 | public function __destruct() |
|
115 | { |
||
116 | 8 | $this->path = null; //call the path destruct |
|
117 | 8 | } |
|
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) |
||
140 | { |
||
141 | $this->schedule = $schedule; |
||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return \string[] |
||
147 | */ |
||
148 | 6 | public function getUsers() |
|
152 | |||
153 | /** |
||
154 | * @param string[] $users |
||
155 | * @return Solver |
||
156 | */ |
||
157 | 12 | public function setUsers($users) |
|
158 | { |
||
159 | 12 | $this->users = $users; |
|
160 | 12 | return $this; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string[] |
||
165 | */ |
||
166 | 6 | public function getMeetings() |
|
170 | |||
171 | /** |
||
172 | * @param string[] $meetings |
||
173 | * @return Solver |
||
174 | */ |
||
175 | 9 | public function setMeetings($meetings) |
|
176 | { |
||
177 | 9 | $this->meetings = $meetings; |
|
178 | 9 | return $this; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getTimeSlots() |
||
188 | |||
189 | /** |
||
190 | * @param int $timeSlots |
||
191 | * @return Solver |
||
192 | * @throws OptimiseException |
||
193 | */ |
||
194 | 15 | public function setTimeSlots($timeSlots) |
|
195 | { |
||
196 | 15 | if (!is_int($timeSlots) || $timeSlots <= 0) |
|
197 | 10 | throw new OptimiseException('$timeSlots is not integer or it is not >0'); |
|
198 | |||
199 | 15 | $this->timeSlots = $timeSlots; |
|
200 | 15 | return $this; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getMaxTimeSlots() |
||
210 | |||
211 | /** |
||
212 | * @param int $maxTimeSlots |
||
213 | * @return Solver |
||
214 | * @throws OptimiseException |
||
215 | */ |
||
216 | 15 | public function setMaxTimeSlots($maxTimeSlots) |
|
217 | { |
||
218 | 15 | if (!is_int($maxTimeSlots) || $maxTimeSlots <= 0) |
|
219 | 10 | throw new OptimiseException('$maxTimeSlots is not integer or it is not >0'); |
|
220 | |||
221 | 15 | $this->maxTimeSlots = $maxTimeSlots; |
|
222 | 15 | return $this; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return string[] |
||
227 | */ |
||
228 | public function getMeetingsAvailability() |
||
232 | |||
233 | /** |
||
234 | * @param string[] $meetingsAvailability |
||
235 | * @return Solver |
||
236 | * @throws OptimiseException |
||
237 | */ |
||
238 | 9 | public function setMeetingsAvailability($meetingsAvailability) |
|
239 | { |
||
240 | 9 | $meetings = array_keys($meetingsAvailability); |
|
241 | 9 | if (array_diff($meetings, $this->meetings)) |
|
242 | 6 | throw new OptimiseException('meetings different from meetings set'); |
|
243 | 9 | foreach ($meetingsAvailability as $key => $meetingsAvailabilityS) { |
|
244 | 9 | $timeSlots = array_keys($meetingsAvailabilityS);//TODO this is useless, we can use directly $usersAvailabilityS |
|
245 | 9 | if (count($timeSlots) != $this->timeSlots) |
|
246 | 6 | throw new OptimiseException('timeSlots different from timeSlots set'); |
|
247 | 9 | $meetingsAvailability[$key] = self::arrayPad($meetingsAvailabilityS, $this->timeSlots + $this->maxTimeSlots, 0); |
|
|
|||
248 | 6 | } |
|
249 | |||
250 | 9 | $this->meetingsAvailability = $meetingsAvailability; |
|
251 | 9 | return $this; |
|
252 | } |
||
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 | 9 | 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 | 9 | public function setMeetingsDuration($meetingsDuration) |
|
296 | |||
297 | /** |
||
298 | * @return string[] |
||
299 | */ |
||
300 | public function getUsersAvailability() |
||
304 | |||
305 | /** |
||
306 | * @param string[] $usersAvailability |
||
307 | * @return Solver |
||
308 | * @throws OptimiseException |
||
309 | */ |
||
310 | 9 | public function setUsersAvailability($usersAvailability) |
|
326 | |||
327 | /** |
||
328 | * @return string[] |
||
329 | */ |
||
330 | public function getUsersMeetings() |
||
334 | |||
335 | /** |
||
336 | * @param string[] $usersMeetings |
||
337 | * @return Solver |
||
338 | * @throws OptimiseException |
||
339 | */ |
||
340 | 9 | public function setUsersMeetings($usersMeetings) |
|
354 | |||
355 | /** |
||
356 | * @return Solver |
||
357 | * @throws OptimiseException |
||
358 | */ |
||
359 | 9 | public function solve() |
|
370 | |||
371 | /** |
||
372 | * @throws OptimiseException |
||
373 | */ |
||
374 | 9 | private function writeData() |
|
384 | |||
385 | /** |
||
386 | * @throws OptimiseException |
||
387 | */ |
||
388 | 9 | private function checkData() |
|
393 | |||
394 | /** |
||
395 | * @param $proprieties |
||
396 | * @throws OptimiseException |
||
397 | */ |
||
398 | 9 | private function checkArrayProprieties($proprieties) |
|
404 | |||
405 | /** |
||
406 | * @param $proprieties |
||
407 | * @throws OptimiseException |
||
408 | */ |
||
409 | 9 | private function checkIntProprieties($proprieties) |
|
415 | |||
416 | /** |
||
417 | * @throws OptimiseException |
||
418 | */ |
||
419 | 9 | private function writeUsers() |
|
423 | |||
424 | /** |
||
425 | * @param string $file |
||
426 | * @param array $data |
||
427 | * @throws OptimiseException |
||
428 | */ |
||
429 | 9 | static private function writeCSVArrayNoKey($file, $data) |
|
439 | |||
440 | /** |
||
441 | * @param string $file |
||
442 | * @param array $data |
||
443 | * @param array $heading |
||
444 | * @param \Closure $writer |
||
445 | * @throws OptimiseException |
||
446 | */ |
||
447 | 9 | static private function writeCSV($file, $data, $heading, \Closure $writer) |
|
461 | |||
462 | /** |
||
463 | * @throws OptimiseException |
||
464 | */ |
||
465 | 9 | private function writeMeetings() |
|
469 | |||
470 | /** |
||
471 | * @throws OptimiseException |
||
472 | */ |
||
473 | 9 | private function writeMeetingsDuration() |
|
477 | |||
478 | /** |
||
479 | * @param string $file |
||
480 | * @param array $data |
||
481 | * @param string $name |
||
482 | * @throws OptimiseException |
||
483 | */ |
||
484 | 9 | static private function writeCSVArray($file, $data, $name) |
|
494 | |||
495 | /** |
||
496 | * @throws OptimiseException |
||
497 | */ |
||
498 | 9 | private function writeMeetingsAvailability() |
|
502 | |||
503 | /** |
||
504 | * @param string $file |
||
505 | * @param array $data |
||
506 | * @param string $name |
||
507 | * @throws OptimiseException |
||
508 | */ |
||
509 | static private function writeCSVMatrix($file, $data, $name) |
||
520 | |||
521 | /** |
||
522 | * @throws OptimiseException |
||
523 | */ |
||
524 | 9 | private function writeUsersAvailability() |
|
528 | |||
529 | /** |
||
530 | * @throws OptimiseException |
||
531 | */ |
||
532 | 9 | private function writeUsersMeetings() |
|
536 | |||
537 | /** |
||
538 | * @throws OptimiseException |
||
539 | */ |
||
540 | 9 | private function writeModelFile() |
|
550 | |||
551 | /** |
||
552 | * @return array |
||
553 | * @throws OptimiseException |
||
554 | */ |
||
555 | 9 | public function getXResults() |
|
559 | |||
560 | /** |
||
561 | * @param string $file |
||
562 | * @return array |
||
563 | * @throws OptimiseException |
||
564 | */ |
||
565 | 9 | static private function readCSVFile($file) |
|
589 | |||
590 | /** |
||
591 | * @return array |
||
592 | * @throws OptimiseException |
||
593 | */ |
||
594 | 9 | public function getYResults() |
|
598 | |||
599 | /** |
||
600 | * @return string |
||
601 | * @throws OptimiseException |
||
602 | */ |
||
603 | public function getOutput() |
||
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.