Complex classes like Optimise 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 Optimise, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Optimise |
||
23 | { |
||
24 | //TODO insert MAX timeslots limit during meeting creation |
||
25 | //TODo max timeslots can be an environment var |
||
26 | const TIME_SLOT_DURATION = 900; //seconds -> 15 minutes |
||
27 | |||
28 | private $max_time_slots = 20; //max duration of a meeting in term of timeslots //20 |
||
29 | private $time_slots = 672; //total amount of timeslots that must be optimised -> one week 4*24*7 = 672 |
||
30 | |||
31 | //TODO timezone |
||
32 | //TODO fix here |
||
33 | /** |
||
34 | * @var \DateTime |
||
35 | */ |
||
36 | private $startTime; |
||
37 | /** |
||
38 | * @var \DateTime |
||
39 | */ |
||
40 | private $endTime; |
||
41 | |||
42 | /** |
||
43 | * @var Company |
||
44 | */ |
||
45 | private $company; |
||
46 | |||
47 | /** |
||
48 | * @var Schedule laravel schedule object needed to perform command in background |
||
49 | */ |
||
50 | private $schedule; |
||
51 | |||
52 | /** |
||
53 | * @var \Illuminate\Contracts\Foundation\Application; |
||
54 | */ |
||
55 | private $laravel; |
||
56 | |||
57 | /** |
||
58 | * @var Solver |
||
59 | */ |
||
60 | private $solver = null; |
||
61 | |||
62 | //TODO clone |
||
63 | //TODO to_string |
||
64 | |||
65 | /** |
||
66 | * Optimise constructor. |
||
67 | * @param company $company |
||
68 | * @param Schedule $schedule |
||
69 | * @param \Illuminate\Contracts\Foundation\Application $laravel |
||
70 | */ |
||
71 | 2 | public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
|
79 | |||
80 | |||
81 | /** |
||
82 | * @param \DateTime $startTime |
||
83 | */ |
||
84 | 2 | public function setStartTime(\DateTime $startTime) |
|
90 | |||
91 | /** |
||
92 | * @return int |
||
93 | */ |
||
94 | public function getMaxTimeSlots() |
||
98 | |||
99 | /** |
||
100 | * @param int $max_time_slots |
||
101 | */ |
||
102 | 2 | public function setMaxTimeSlots($max_time_slots) |
|
106 | |||
107 | /** |
||
108 | * @return int |
||
109 | */ |
||
110 | 2 | public function getTimeSlots() |
|
114 | |||
115 | /** |
||
116 | * @param int $time_slots |
||
117 | */ |
||
118 | 2 | public function setTimeSlots($time_slots) |
|
122 | |||
123 | |||
124 | /** |
||
125 | * @return Company |
||
126 | */ |
||
127 | public function getCompany() |
||
131 | |||
132 | /** |
||
133 | * @param Company $company |
||
134 | */ |
||
135 | public function setCompany($company) |
||
139 | |||
140 | /** |
||
141 | * @return Solver |
||
142 | */ |
||
143 | 2 | public function getSolver() |
|
147 | |||
148 | |||
149 | /** |
||
150 | * @return Optimise |
||
151 | */ |
||
152 | 2 | public function optimise() |
|
167 | |||
168 | /** |
||
169 | * @return Optimise |
||
170 | */ |
||
171 | 2 | public function save() |
|
191 | |||
192 | //TODO fix php doc with exceptions |
||
193 | |||
194 | /** |
||
195 | * @param Solver $solver |
||
196 | */ |
||
197 | 2 | private function saveMeetings(Solver $solver) |
|
206 | |||
207 | /** |
||
208 | * @param Solver $solver |
||
209 | */ |
||
210 | 2 | private function saveEmployeesMeetings(Solver $solver) |
|
223 | |||
224 | |||
225 | /** |
||
226 | * @param Solver $solver |
||
227 | * @return Solver |
||
228 | * @throws OptimiseException |
||
229 | */ |
||
230 | 2 | private function setData(Solver $solver) |
|
241 | |||
242 | /** |
||
243 | * @param Solver $solver |
||
244 | * @return Solver |
||
245 | * @throws OptimiseException |
||
246 | */ |
||
247 | 2 | private function setTimeSlotsSolver(Solver $solver) |
|
251 | |||
252 | /** |
||
253 | * @param Solver $solver |
||
254 | * @return Solver |
||
255 | */ |
||
256 | 2 | private function setUsers(Solver $solver) |
|
262 | |||
263 | /** |
||
264 | * @param Solver $solver |
||
265 | * @return Solver |
||
266 | */ |
||
267 | 2 | private function setAllMeetingsInfo(Solver $solver) |
|
280 | |||
281 | /** |
||
282 | * @param Solver $solver |
||
283 | * @return Solver |
||
284 | * @throws OptimiseException |
||
285 | */ |
||
286 | 2 | private function setUserAvailability(Solver $solver) |
|
297 | |||
298 | /** |
||
299 | * @param Solver $solver |
||
300 | * @return Solver |
||
301 | * @throws OptimiseException |
||
302 | */ |
||
303 | 2 | private function setUsersMeetings(Solver $solver) |
|
314 | |||
315 | /** |
||
316 | * @param array $users |
||
317 | * @param array $meetings |
||
318 | * @param \Illuminate\Support\Collection $usersMeetings |
||
319 | * @return array |
||
320 | */ |
||
321 | 2 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
338 | |||
339 | private function timeSlotsConverter($item) |
||
348 | |||
349 | /** |
||
350 | * @param \Illuminate\Support\Collection $timeSlots |
||
351 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
352 | * @return array |
||
353 | */ |
||
354 | 2 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
|
365 | |||
366 | /** |
||
367 | * @param array $array |
||
368 | * @param int $id |
||
369 | * @param \Illuminate\Support\Collection $timeSlots |
||
370 | * @param string $fill |
||
371 | * @return array |
||
372 | */ |
||
373 | 2 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
382 | |||
383 | /** |
||
384 | * @param array $array |
||
385 | * @param int $id |
||
386 | * @param string $fill |
||
387 | * @return array |
||
388 | */ |
||
389 | 2 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
398 | |||
399 | /** |
||
400 | * @param array $array |
||
401 | * @param int $from |
||
402 | * @param int $to |
||
403 | * @param string $pad |
||
404 | * @return array |
||
405 | */ |
||
406 | 2 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
412 | |||
413 | |||
414 | /** |
||
415 | * @param mixed $time |
||
416 | * @return int |
||
417 | * @throws OptimiseException |
||
418 | */ |
||
419 | 2 | private function toTimeSlot($time) |
|
432 | |||
433 | /** |
||
434 | * @param int $timeslot |
||
435 | * @return \DateTime |
||
436 | */ |
||
437 | 2 | private function toDateTime($timeslot) |
|
442 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..