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 max timeslots can be an environment var |
||
25 | const TIME_SLOT_DURATION = 900; //seconds -> 15 minutes |
||
26 | |||
27 | private $max_time_slots = 20; //max duration of a meeting in term of timeslots //20 |
||
28 | private $time_slots = 672; //total amount of timeslots that must be optimised -> one week 4*24*7 = 672 |
||
29 | |||
30 | //TODO timezone |
||
31 | /** |
||
32 | * @var \DateTime |
||
33 | */ |
||
34 | private $startTime; |
||
35 | /** |
||
36 | * @var \DateTime |
||
37 | */ |
||
38 | private $endTime; |
||
39 | |||
40 | /** |
||
41 | * @var Company |
||
42 | */ |
||
43 | private $company; |
||
44 | |||
45 | /** |
||
46 | * @var Schedule laravel schedule object needed to perform command in background |
||
47 | */ |
||
48 | private $schedule; |
||
49 | |||
50 | /** |
||
51 | * @var \Illuminate\Contracts\Foundation\Application; |
||
52 | */ |
||
53 | private $laravel; |
||
54 | |||
55 | /** |
||
56 | * @var Solver |
||
57 | */ |
||
58 | private $solver = null; |
||
59 | |||
60 | //TODO clone |
||
61 | //TODO to_string |
||
62 | |||
63 | /** |
||
64 | * Optimise constructor. |
||
65 | * @param company $company |
||
66 | * @param Schedule $schedule |
||
67 | * @param \Illuminate\Contracts\Foundation\Application $laravel |
||
68 | */ |
||
69 | 2 | public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
|
77 | |||
78 | |||
79 | /** |
||
80 | * @param \DateTime $startTime |
||
81 | */ |
||
82 | 2 | public function setStartTime(\DateTime $startTime) |
|
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | public function getMaxTimeSlots() |
||
96 | |||
97 | /** |
||
98 | * @param int $max_time_slots |
||
99 | */ |
||
100 | 2 | public function setMaxTimeSlots($max_time_slots) |
|
104 | |||
105 | /** |
||
106 | * @return int |
||
107 | */ |
||
108 | 2 | public function getTimeSlots() |
|
112 | |||
113 | /** |
||
114 | * @param int $time_slots |
||
115 | */ |
||
116 | 2 | public function setTimeSlots($time_slots) |
|
120 | |||
121 | |||
122 | /** |
||
123 | * @return Company |
||
124 | */ |
||
125 | public function getCompany() |
||
129 | |||
130 | /** |
||
131 | * @param Company $company |
||
132 | */ |
||
133 | public function setCompany($company) |
||
137 | |||
138 | /** |
||
139 | * @return Solver |
||
140 | */ |
||
141 | 2 | public function getSolver() |
|
145 | |||
146 | |||
147 | /** |
||
148 | * @return Optimise |
||
149 | */ |
||
150 | 2 | public function optimise() |
|
165 | |||
166 | /** |
||
167 | * @return Optimise |
||
168 | */ |
||
169 | 2 | public function save() |
|
189 | |||
190 | //TODO fix php doc with exceptions |
||
191 | |||
192 | /** |
||
193 | * @param Solver $solver |
||
194 | */ |
||
195 | 2 | private function saveMeetings(Solver $solver) |
|
204 | |||
205 | /** |
||
206 | * @param Solver $solver |
||
207 | */ |
||
208 | 2 | private function saveEmployeesMeetings(Solver $solver) |
|
221 | |||
222 | |||
223 | /** |
||
224 | * @param Solver $solver |
||
225 | * @return Solver |
||
226 | * @throws OptimiseException |
||
227 | */ |
||
228 | 2 | private function setData(Solver $solver) |
|
237 | |||
238 | /** |
||
239 | * @param Solver $solver |
||
240 | * @return Solver |
||
241 | * @throws OptimiseException |
||
242 | */ |
||
243 | 2 | private function setTimeSlotsSolver(Solver $solver) |
|
247 | |||
248 | /** |
||
249 | * @param Solver $solver |
||
250 | * @return Solver |
||
251 | */ |
||
252 | 2 | private function setUsers(Solver $solver) |
|
258 | |||
259 | /** |
||
260 | * @param Solver $solver |
||
261 | * @return Solver |
||
262 | */ |
||
263 | 2 | private function setAllMeetingsInfo(Solver $solver) |
|
276 | |||
277 | /** |
||
278 | * @param Solver $solver |
||
279 | * @return Solver |
||
280 | * @throws OptimiseException |
||
281 | */ |
||
282 | 2 | private function setUserAvailability(Solver $solver) |
|
293 | |||
294 | /** |
||
295 | * @param Solver $solver |
||
296 | * @return Solver |
||
297 | * @throws OptimiseException |
||
298 | */ |
||
299 | 2 | private function setUsersMeetings(Solver $solver) |
|
310 | |||
311 | /** |
||
312 | * @param array $users |
||
313 | * @param array $meetings |
||
314 | * @param \Illuminate\Support\Collection $usersMeetings |
||
315 | * @return array |
||
316 | */ |
||
317 | 2 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
334 | |||
335 | private function timeSlotsConverter($item) |
||
344 | |||
345 | /** |
||
346 | * @param \Illuminate\Support\Collection $timeSlots |
||
347 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
348 | * @return array |
||
349 | */ |
||
350 | 2 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
|
361 | |||
362 | /** |
||
363 | * @param array $array |
||
364 | * @param int $id |
||
365 | * @param \Illuminate\Support\Collection $timeSlots |
||
366 | * @param string $fill |
||
367 | * @return array |
||
368 | */ |
||
369 | 2 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
378 | |||
379 | /** |
||
380 | * @param array $array |
||
381 | * @param int $id |
||
382 | * @param string $fill |
||
383 | * @return array |
||
384 | */ |
||
385 | 2 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
394 | |||
395 | /** |
||
396 | * @param array $array |
||
397 | * @param int $from |
||
398 | * @param int $to |
||
399 | * @param string $pad |
||
400 | * @return array |
||
401 | */ |
||
402 | 2 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
408 | |||
409 | |||
410 | /** |
||
411 | * @param mixed $time |
||
412 | * @return int |
||
413 | * @throws OptimiseException |
||
414 | */ |
||
415 | 2 | private function toTimeSlot($time) |
|
428 | |||
429 | /** |
||
430 | * @param int $timeslot |
||
431 | * @return \DateTime |
||
432 | */ |
||
433 | 2 | private function toDateTime($timeslot) |
|
438 | } |
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..