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) |
|
95 | { |
||
96 | 4 | self::checkGlpsol(); |
|
97 | 4 | $this->path = Path::createPath(); |
|
98 | 4 | $this->schedule = $schedule; |
|
99 | 4 | $this->laravel = $laravel; |
|
100 | 4 | } |
|
101 | |||
102 | /** |
||
103 | * @throws OptimiseException |
||
104 | */ |
||
105 | public function __destruct() |
||
106 | { |
||
107 | $this->path = null; //call the path destruct |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @throws OptimiseException |
||
112 | */ |
||
113 | 4 | static private function checkGlpsol() |
|
114 | { |
||
115 | 4 | if(!(`which glpsol`)) |
|
116 | 4 | throw new OptimiseException('glpsol is not installed'); |
|
117 | 4 | } |
|
118 | |||
119 | /** |
||
120 | * @return Path |
||
121 | */ |
||
122 | public function getPath() |
||
123 | { |
||
124 | return clone $this->path; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return Schedule |
||
129 | */ |
||
130 | public function getSchedule() |
||
131 | { |
||
132 | return $this->schedule; |
||
133 | } |
||
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 | 2 | public function getUsers() |
|
149 | { |
||
150 | 2 | return $this->users; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string[] $users |
||
155 | * @return Solver |
||
156 | */ |
||
157 | 4 | public function setUsers($users) |
|
158 | { |
||
159 | 4 | $this->users = $users; |
|
160 | 4 | return $this; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string[] |
||
165 | */ |
||
166 | 2 | public function getMeetings() |
|
167 | { |
||
168 | 2 | return $this->meetings; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string[] $meetings |
||
173 | * @return Solver |
||
174 | */ |
||
175 | 4 | public function setMeetings($meetings) |
|
176 | { |
||
177 | 4 | $this->meetings = $meetings; |
|
178 | 4 | return $this; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getTimeSlots() |
||
185 | { |
||
186 | return $this->timeSlots; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param int $timeSlots |
||
191 | * @return Solver |
||
192 | * @throws OptimiseException |
||
193 | */ |
||
194 | 4 | public function setTimeSlots($timeSlots) |
|
195 | { |
||
196 | 4 | if(!is_int($timeSlots) || $timeSlots <=0) |
|
197 | 4 | throw new OptimiseException('$timeSlots is not integer or it is not >0'); |
|
198 | |||
199 | 4 | $this->timeSlots = $timeSlots; |
|
200 | 4 | return $this; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getMaxTimeSlots() |
||
207 | { |
||
208 | return $this->maxTimeSlots; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param int $maxTimeSlots |
||
213 | * @return Solver |
||
214 | * @throws OptimiseException |
||
215 | */ |
||
216 | 4 | public function setMaxTimeSlots($maxTimeSlots) |
|
217 | { |
||
218 | 4 | if(!is_int($maxTimeSlots) || $maxTimeSlots <=0) |
|
219 | 4 | throw new OptimiseException('$maxTimeSlots is not integer or it is not >0'); |
|
220 | |||
221 | 4 | $this->maxTimeSlots = $maxTimeSlots; |
|
222 | 4 | return $this; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return string[] |
||
227 | */ |
||
228 | public function getMeetingsAvailability() |
||
229 | { |
||
230 | return $this->meetingsAvailability; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string[] $meetingsAvailability |
||
235 | * @return Solver |
||
236 | * @throws OptimiseException |
||
237 | */ |
||
238 | 4 | public function setMeetingsAvailability($meetingsAvailability) |
|
239 | { |
||
240 | 4 | $meetings = array_keys($meetingsAvailability); |
|
241 | 4 | if(array_diff($meetings, $this->meetings)) |
|
242 | 4 | throw new OptimiseException('meetings different from meetings set'); |
|
243 | 4 | foreach($meetingsAvailability as $key=>$meetingsAvailabilityS) { |
|
244 | 4 | $timeSlots = array_keys($meetingsAvailabilityS); |
|
245 | 4 | if(count($timeSlots) != $this->timeSlots) |
|
246 | 4 | throw new OptimiseException('timeSlots different from timeSlots set'); |
|
247 | 4 | $meetingsAvailability[$key] = self::arrayPad($meetingsAvailabilityS, $this->timeSlots + $this->maxTimeSlots, 0); |
|
|
|||
248 | 4 | } |
|
249 | |||
250 | 4 | $this->meetingsAvailability = $meetingsAvailability; |
|
251 | 4 | return $this; |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return string[] |
||
256 | */ |
||
257 | public function getMeetingsDuration() |
||
258 | { |
||
259 | return $this->meetingsDuration; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param string[] $meetingsDuration |
||
264 | * @return Solver |
||
265 | * @throws OptimiseException |
||
266 | */ |
||
267 | 4 | public function setMeetingsDuration($meetingsDuration) |
|
268 | { |
||
269 | 4 | $meetings = array_keys($meetingsDuration); |
|
270 | 4 | if(array_diff($meetings, $this->meetings)) { |
|
271 | print ""; |
||
272 | throw new OptimiseException('meetings different from meetings set'); |
||
273 | } |
||
274 | 4 | foreach($meetingsDuration as $duration) { |
|
275 | 4 | $duration = (int) $duration; //TODO fix this (fix for optimise) |
|
276 | 4 | if(!is_int($duration) || $duration <=0) |
|
277 | 4 | throw new OptimiseException('duration is not integer or it is not >0'); |
|
278 | 4 | } |
|
279 | |||
280 | 4 | $this->meetingsDuration = $meetingsDuration; |
|
281 | 4 | return $this; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return string[] |
||
286 | */ |
||
287 | public function getUsersAvailability() |
||
288 | { |
||
289 | return $this->usersAvailability; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string[] $usersAvailability |
||
294 | * @return Solver |
||
295 | * @throws OptimiseException |
||
296 | */ |
||
297 | 4 | public function setUsersAvailability($usersAvailability) |
|
298 | { |
||
299 | 4 | $users = array_keys($usersAvailability); |
|
300 | 4 | if(array_diff($users, $this->users)) |
|
301 | 4 | throw new OptimiseException('users different from users set'); |
|
302 | 4 | foreach($usersAvailability as $key=>$usersAvailabilityS) { |
|
303 | 4 | $timeSlots = array_keys($usersAvailabilityS); |
|
304 | 4 | if(count($timeSlots) != $this->timeSlots) |
|
305 | 4 | throw new OptimiseException('timeSlots different from timeSlots set'); |
|
306 | |||
307 | 4 | $usersAvailability[$key] = self::arrayPad($usersAvailabilityS, $this->timeSlots + $this->maxTimeSlots, 0); |
|
308 | 4 | } |
|
309 | |||
310 | 4 | $this->usersAvailability = $usersAvailability; |
|
311 | 4 | return $this; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * @return string[] |
||
316 | */ |
||
317 | public function getUsersMeetings() |
||
318 | { |
||
319 | return $this->usersMeetings; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param string[] $usersMeetings |
||
324 | * @return Solver |
||
325 | * @throws OptimiseException |
||
326 | */ |
||
327 | 4 | public function setUsersMeetings($usersMeetings) |
|
328 | { |
||
329 | 4 | $users = array_keys($usersMeetings); |
|
330 | 4 | if(array_diff($users, $this->users)) |
|
331 | 4 | throw new OptimiseException('users different from users set'); |
|
332 | 4 | foreach($usersMeetings as $usersMeetingsS) { |
|
333 | 4 | $meetings = array_keys($usersMeetingsS); |
|
334 | 4 | if(array_diff($meetings, $this->meetings)) |
|
335 | 4 | throw new OptimiseException('meetings different from meetings set'); |
|
336 | 4 | } |
|
337 | |||
338 | 4 | $this->usersMeetings = $usersMeetings; |
|
339 | 4 | return $this; |
|
340 | } |
||
341 | |||
342 | /** |
||
343 | * @throws OptimiseException |
||
344 | */ |
||
345 | 4 | private function writeUsers() |
|
346 | { |
||
347 | 4 | self::writeCSVArrayNoKey($this->path->getUsersPath(), $this->users); |
|
348 | 4 | } |
|
349 | |||
350 | /** |
||
351 | * @throws OptimiseException |
||
352 | */ |
||
353 | 4 | private function writeMeetings() |
|
354 | { |
||
355 | 4 | self::writeCSVArrayNoKey($this->path->getMeetingsPath(), $this->meetings); |
|
356 | 4 | } |
|
357 | |||
358 | /** |
||
359 | * @throws OptimiseException |
||
360 | */ |
||
361 | 4 | private function writeMeetingsDuration() |
|
362 | { |
||
363 | 4 | self::writeCSVArray($this->path->getMeetingsDurationPath(), $this->meetingsDuration, 'MeetingsDuration'); |
|
364 | 4 | } |
|
365 | |||
366 | /** |
||
367 | * @throws OptimiseException |
||
368 | */ |
||
369 | 4 | private function writeMeetingsAvailability() |
|
370 | { |
||
371 | 4 | self::writeCSVMatrix($this->path->getMeetingsAvailabilityPath(), $this->meetingsAvailability, 'MeetingsAvailability'); |
|
372 | 4 | } |
|
373 | |||
374 | /** |
||
375 | * @throws OptimiseException |
||
376 | */ |
||
377 | 4 | private function writeUsersAvailability() |
|
378 | { |
||
379 | 4 | self::writeCSVMatrix($this->path->getUsersAvailabilityPath(), $this->usersAvailability, 'UsersAvailability'); |
|
380 | 4 | } |
|
381 | |||
382 | /** |
||
383 | * @throws OptimiseException |
||
384 | */ |
||
385 | 4 | private function writeUsersMeetings() |
|
386 | { |
||
387 | 4 | self::writeCSVMatrix($this->path->getUsersMeetingsPath(), $this->usersMeetings, 'UsersMeetings'); |
|
388 | 4 | } |
|
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) |
|
413 | { |
||
414 | $f = function ($fp, $data){ |
||
415 | 4 | foreach ($data as $key=>$field) { |
|
416 | 4 | fputcsv($fp, [$key, $field]); |
|
417 | 4 | } |
|
418 | 4 | }; |
|
419 | |||
420 | 4 | self::writeCSV($file, $data, ['i', $name], $f); |
|
421 | 4 | } |
|
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) |
|
430 | { |
||
431 | $f = function ($fp, $data){ |
||
432 | 4 | foreach ($data as $key=>$field) { |
|
433 | 4 | foreach ($field as $key2=>$field2) |
|
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.