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 |
||
23 | class Optimise |
||
24 | { |
||
25 | //TODo max timeslots can be an environment var |
||
26 | const TIME_SLOT_DURATION = 900; //seconds -> 15 minutes |
||
27 | const DEFAULT_MAX_TIME_SLOTS = 20; //max duration of a meeting in term of timeslots //20 |
||
28 | const DEFAULT_TIME_SLOTS = 672; //total amount of timeslots that must be optimised -> one week 4*24*7 = 672 |
||
29 | |||
30 | private $max_time_slots = self::DEFAULT_MAX_TIME_SLOTS; |
||
31 | private $time_slots = self::DEFAULT_TIME_SLOTS; |
||
32 | |||
33 | //TODO timezone |
||
34 | /** |
||
35 | * @var \DateTime |
||
36 | */ |
||
37 | private $startTime; |
||
38 | /** |
||
39 | * @var \DateTime |
||
40 | */ |
||
41 | private $endTime; |
||
42 | |||
43 | /** |
||
44 | * @var Company |
||
45 | */ |
||
46 | private $company; |
||
47 | |||
48 | /** |
||
49 | * @var Schedule laravel schedule object needed to perform command in background |
||
50 | */ |
||
51 | private $schedule; |
||
52 | |||
53 | /** |
||
54 | * @var \Illuminate\Contracts\Foundation\Application; |
||
55 | */ |
||
56 | private $laravel; |
||
57 | |||
58 | /** |
||
59 | * @var Solver |
||
60 | */ |
||
61 | private $solver = null; |
||
62 | |||
63 | //TODO clone |
||
64 | //TODO to_string |
||
65 | |||
66 | /** |
||
67 | * Optimise constructor. |
||
68 | * @param company $company |
||
69 | * @param Schedule $schedule |
||
70 | * @param \Illuminate\Contracts\Foundation\Application $laravel |
||
71 | */ |
||
72 | 2 | public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
|
73 | { |
||
74 | 2 | $this->company = $company; |
|
|
|||
75 | 2 | $this->schedule = $schedule; |
|
76 | 2 | $this->laravel = $laravel; |
|
77 | |||
78 | 2 | $this->setStartTime((new \DateTime())->modify('next monday')); |
|
79 | 2 | } |
|
80 | |||
81 | |||
82 | /** |
||
83 | * @param \DateTime $startTime |
||
84 | */ |
||
85 | 2 | public function setStartTime(\DateTime $startTime) |
|
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | 2 | public function getMaxTimeSlots() |
|
99 | |||
100 | /** |
||
101 | * @param int $max_time_slots |
||
102 | */ |
||
103 | 2 | public function setMaxTimeSlots($max_time_slots) |
|
107 | |||
108 | /** |
||
109 | * @return int |
||
110 | */ |
||
111 | 2 | public function getTimeSlots() |
|
115 | |||
116 | /** |
||
117 | * @param int $time_slots |
||
118 | */ |
||
119 | 2 | public function setTimeSlots($time_slots) |
|
123 | |||
124 | |||
125 | /** |
||
126 | * @return Company |
||
127 | */ |
||
128 | public function getCompany() |
||
132 | |||
133 | /** |
||
134 | * @param Company $company |
||
135 | */ |
||
136 | public function setCompany($company) |
||
140 | |||
141 | /** |
||
142 | * @return Solver |
||
143 | */ |
||
144 | 2 | public function getSolver() |
|
148 | |||
149 | |||
150 | /** |
||
151 | * @return Optimise |
||
152 | */ |
||
153 | 2 | public function optimise() |
|
168 | |||
169 | /** |
||
170 | * @return Optimise |
||
171 | */ |
||
172 | 2 | public function save() |
|
173 | { |
||
174 | 2 | if(!($this->solver instanceof Solver)) { |
|
175 | \Event::fire(new ErrorEvent($this->company, 'solver is not an instace of Solver')); |
||
176 | throw new OptimiseException('solver is not an instance of Solver'); |
||
177 | return; |
||
178 | } |
||
179 | //TODO check results before save them |
||
180 | |||
181 | try { |
||
182 | 2 | $this->saveMeetings($this->solver); |
|
183 | 2 | $this->saveEmployeesMeetings($this->solver); |
|
184 | 2 | }catch(\Exception $e) |
|
185 | { |
||
186 | \Event::fire(new ErrorEvent($this->company, $e->getMessage())); |
||
187 | throw new OptimiseException('Optimising error', 0, $e); |
||
188 | //TODO catch specif exception |
||
189 | 2 | } |
|
190 | //TODO Is this the correct place? |
||
191 | 2 | \Event::fire(new OkEvent($this->company)); |
|
192 | 2 | return $this; |
|
193 | } |
||
194 | |||
195 | //TODO fix php doc with exceptions |
||
196 | |||
197 | /** |
||
198 | * @param Solver $solver |
||
199 | */ |
||
200 | 2 | private function saveMeetings(Solver $solver) |
|
209 | |||
210 | /** |
||
211 | * @param Solver $solver |
||
212 | */ |
||
213 | 2 | private function saveEmployeesMeetings(Solver $solver) |
|
226 | |||
227 | |||
228 | /** |
||
229 | * @param Solver $solver |
||
230 | * @return Solver |
||
231 | * @throws OptimiseException |
||
232 | */ |
||
233 | 2 | private function setData(Solver $solver) |
|
242 | |||
243 | /** |
||
244 | * @param Solver $solver |
||
245 | * @return Solver |
||
246 | * @throws OptimiseException |
||
247 | */ |
||
248 | 2 | private function setTimeSlotsSolver(Solver $solver) |
|
252 | |||
253 | /** |
||
254 | * @param Solver $solver |
||
255 | * @return Solver |
||
256 | */ |
||
257 | 2 | private function setUsers(Solver $solver) |
|
263 | |||
264 | /** |
||
265 | * @param Solver $solver |
||
266 | * @return Solver |
||
267 | */ |
||
268 | 2 | private function setAllMeetingsInfo(Solver $solver) |
|
281 | |||
282 | /** |
||
283 | * @param Solver $solver |
||
284 | * @return Solver |
||
285 | * @throws OptimiseException |
||
286 | */ |
||
287 | 2 | private function setUserAvailability(Solver $solver) |
|
298 | |||
299 | /** |
||
300 | * @param Solver $solver |
||
301 | * @return Solver |
||
302 | * @throws OptimiseException |
||
303 | */ |
||
304 | 2 | private function setUsersMeetings(Solver $solver) |
|
315 | |||
316 | /** |
||
317 | * @param array $users |
||
318 | * @param array $meetings |
||
319 | * @param \Illuminate\Support\Collection $usersMeetings |
||
320 | * @return array |
||
321 | */ |
||
322 | 2 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
339 | |||
340 | /** |
||
341 | * @param mixed $item |
||
342 | * @return mixed |
||
343 | */ |
||
344 | 2 | private function durationConverter($item) |
|
352 | |||
353 | /** |
||
354 | * @param mixed $item |
||
355 | * @return mixed |
||
356 | */ |
||
357 | private function timeSlotsConverter($item) |
||
366 | |||
367 | /** |
||
368 | * @param \Illuminate\Support\Collection $timeSlots |
||
369 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
370 | * @return array |
||
371 | */ |
||
372 | 2 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
|
383 | |||
384 | /** |
||
385 | * @param array $array |
||
386 | * @param int $id |
||
387 | * @param \Illuminate\Support\Collection $timeSlots |
||
388 | * @param string $fill |
||
389 | * @return array |
||
390 | */ |
||
391 | 2 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
400 | |||
401 | /** |
||
402 | * @param array $array |
||
403 | * @param int $id |
||
404 | * @param string $fill |
||
405 | * @return array |
||
406 | */ |
||
407 | 2 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
416 | |||
417 | /** |
||
418 | * @param array $array |
||
419 | * @param int $from |
||
420 | * @param int $to |
||
421 | * @param string $pad |
||
422 | * @return array |
||
423 | */ |
||
424 | 2 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
430 | |||
431 | |||
432 | /** |
||
433 | * @param mixed $time |
||
434 | * @return int |
||
435 | * @throws OptimiseException |
||
436 | */ |
||
437 | 2 | private function toTimeSlot($time) |
|
450 | |||
451 | /** |
||
452 | * @param int $timeslot |
||
453 | * @return \DateTime |
||
454 | */ |
||
455 | 2 | private function toDateTime($timeslot) |
|
460 | |||
461 | /** |
||
462 | * @param int $duration |
||
463 | * @return int |
||
464 | */ |
||
465 | 2 | static private function convertDuration($duration) |
|
469 | } |
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..