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 | //TODo fix php doc |
||
| 150 | /** |
||
| 151 | * @return Optimise |
||
| 152 | */ |
||
| 153 | 2 | public function optimise() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return Optimise |
||
| 169 | */ |
||
| 170 | 2 | public function save() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param Solver $solver |
||
| 185 | */ |
||
| 186 | 2 | private function saveMeetings(Solver $solver) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param Solver $solver |
||
| 198 | */ |
||
| 199 | 2 | private function saveEmployeesMeetings(Solver $solver) |
|
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param Solver $solver |
||
| 216 | * @return Solver |
||
| 217 | * @throws OptimiseException |
||
| 218 | */ |
||
| 219 | 2 | private function setData(Solver $solver) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param Solver $solver |
||
| 234 | * @return Solver |
||
| 235 | * @throws OptimiseException |
||
| 236 | */ |
||
| 237 | 2 | private function setTimeSlotsSolver(Solver $solver) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param Solver $solver |
||
| 244 | * @return Solver |
||
| 245 | */ |
||
| 246 | 2 | private function setUsers(Solver $solver) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param Solver $solver |
||
| 255 | * @return Solver |
||
| 256 | */ |
||
| 257 | 2 | private function setAllMeetingsInfo(Solver $solver) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param Solver $solver |
||
| 273 | * @return Solver |
||
| 274 | * @throws OptimiseException |
||
| 275 | */ |
||
| 276 | 2 | private function setUserAvailability(Solver $solver) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param Solver $solver |
||
| 290 | * @return Solver |
||
| 291 | * @throws OptimiseException |
||
| 292 | */ |
||
| 293 | 2 | private function setUsersMeetings(Solver $solver) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $users |
||
| 307 | * @param array $meetings |
||
| 308 | * @param \Illuminate\Support\Collection $usersMeetings |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 2 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
| 328 | |||
| 329 | private function timeSlotsConverter($item) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 341 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | 2 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $array |
||
| 358 | * @param int $id |
||
| 359 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 360 | * @param string $fill |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 2 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param array $array |
||
| 375 | * @param int $id |
||
| 376 | * @param string $fill |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | 2 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $array |
||
| 391 | * @param int $from |
||
| 392 | * @param int $to |
||
| 393 | * @param string $pad |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | 2 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * @param mixed $time |
||
| 406 | * @return int |
||
| 407 | * @throws OptimiseException |
||
| 408 | */ |
||
| 409 | 2 | private function toTimeSlot($time) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $timeslot |
||
| 425 | * @return \DateTime |
||
| 426 | */ |
||
| 427 | 2 | private function toDateTime($timeslot) |
|
| 432 | } |
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..