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 | const DEFAULT_MAX_TIME_SLOTS = 20; //max duration of a meeting in term of timeslots //20 |
||
| 27 | const DEFAULT_TIME_SLOTS = 672; //total amount of timeslots that must be optimised -> one week 4*24*7 = 672 |
||
| 28 | |||
| 29 | private $max_time_slots = self::DEFAULT_MAX_TIME_SLOTS; |
||
| 30 | private $time_slots = self::DEFAULT_TIME_SLOTS; |
||
| 31 | |||
| 32 | //TODO timezone |
||
| 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) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param Solver $solver |
||
| 242 | * @return Solver |
||
| 243 | * @throws OptimiseException |
||
| 244 | */ |
||
| 245 | 2 | private function setTimeSlotsSolver(Solver $solver) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param Solver $solver |
||
| 252 | * @return Solver |
||
| 253 | */ |
||
| 254 | 2 | private function setUsers(Solver $solver) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param Solver $solver |
||
| 263 | * @return Solver |
||
| 264 | */ |
||
| 265 | 2 | private function setAllMeetingsInfo(Solver $solver) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param Solver $solver |
||
| 281 | * @return Solver |
||
| 282 | * @throws OptimiseException |
||
| 283 | */ |
||
| 284 | 2 | private function setUserAvailability(Solver $solver) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param Solver $solver |
||
| 298 | * @return Solver |
||
| 299 | * @throws OptimiseException |
||
| 300 | */ |
||
| 301 | 2 | private function setUsersMeetings(Solver $solver) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param array $users |
||
| 315 | * @param array $meetings |
||
| 316 | * @param \Illuminate\Support\Collection $usersMeetings |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 2 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param mixed $item |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | 2 | private function durationConverter($item) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param mixed $item |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | private function timeSlotsConverter($item) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 366 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | 2 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $array |
||
| 383 | * @param int $id |
||
| 384 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 385 | * @param string $fill |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | 2 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param array $array |
||
| 400 | * @param int $id |
||
| 401 | * @param string $fill |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | 2 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param array $array |
||
| 416 | * @param int $from |
||
| 417 | * @param int $to |
||
| 418 | * @param string $pad |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | 2 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param mixed $time |
||
| 431 | * @return int |
||
| 432 | * @throws OptimiseException |
||
| 433 | */ |
||
| 434 | 2 | private function toTimeSlot($time) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param int $timeslot |
||
| 450 | * @return \DateTime |
||
| 451 | */ |
||
| 452 | 2 | private function toDateTime($timeslot) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @param int $duration |
||
| 460 | * @return int |
||
| 461 | */ |
||
| 462 | 2 | static private function convertDuration($duration) |
|
| 466 | } |
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..