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 | |||
| 26 | private $max_time_slots; |
||
| 27 | private $time_slots; |
||
| 28 | |||
| 29 | //TODO timezone |
||
| 30 | /** |
||
| 31 | * @var \DateTime |
||
| 32 | */ |
||
| 33 | private $startTime; |
||
| 34 | /** |
||
| 35 | * @var \DateTime |
||
| 36 | */ |
||
| 37 | private $endTime; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Company |
||
| 41 | */ |
||
| 42 | private $company; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Schedule laravel schedule object needed to perform command in background |
||
| 46 | */ |
||
| 47 | private $schedule; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \Illuminate\Contracts\Foundation\Application; |
||
| 51 | */ |
||
| 52 | private $laravel; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Solver |
||
| 56 | */ |
||
| 57 | private $solver = null; |
||
| 58 | |||
| 59 | //TODO clone |
||
| 60 | //TODO to_string |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Optimise constructor. |
||
| 64 | * @param company $company |
||
| 65 | * @param Schedule $schedule |
||
| 66 | * @param \Illuminate\Contracts\Foundation\Application $laravel |
||
| 67 | */ |
||
| 68 | 9 | public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
|
| 69 | { |
||
| 70 | 9 | $this->company = $company; |
|
|
|
|||
| 71 | 9 | $this->schedule = $schedule; |
|
| 72 | 9 | $this->laravel = $laravel; |
|
| 73 | 9 | $this->max_time_slots = config('app.timeslots.max'); |
|
| 74 | 9 | $this->time_slots = config('app.timeslots.number'); |
|
| 75 | |||
| 76 | |||
| 77 | 9 | $this->setStartTime((new \DateTime())->modify('next monday')); |
|
| 78 | 9 | } |
|
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \DateTime $startTime |
||
| 83 | */ |
||
| 84 | 9 | public function setStartTime(\DateTime $startTime) |
|
| 85 | { |
||
| 86 | 9 | $this->startTime = clone $startTime; |
|
| 87 | 9 | $this->endTime = clone $this->startTime; |
|
| 88 | 9 | $this->endTime->add(new \DateInterval('PT' . (($this->max_time_slots + $this->time_slots) * |
|
| 89 | 9 | config('app.timeslots.duration')) . 'S')); |
|
| 90 | 9 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return int |
||
| 94 | */ |
||
| 95 | 2 | public function getMaxTimeSlots() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param int $max_time_slots |
||
| 102 | */ |
||
| 103 | 3 | public function setMaxTimeSlots($max_time_slots) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | 3 | public function getTimeSlots() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param int $time_slots |
||
| 118 | */ |
||
| 119 | 3 | public function setTimeSlots($time_slots) |
|
| 120 | { |
||
| 121 | 3 | $this->time_slots = $time_slots; |
|
| 122 | 3 | } |
|
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @return Company |
||
| 127 | */ |
||
| 128 | public function getCompany() |
||
| 129 | { |
||
| 130 | return $this->company; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Company $company |
||
| 135 | */ |
||
| 136 | public function setCompany($company) |
||
| 137 | { |
||
| 138 | $this->company = $company; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return Solver |
||
| 143 | */ |
||
| 144 | 5 | public function getSolver() |
|
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @return Optimise |
||
| 152 | * @throws OptimiseException |
||
| 153 | */ |
||
| 154 | 9 | public function optimise() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param Solver $solver |
||
| 173 | * @return Solver |
||
| 174 | * @throws OptimiseException |
||
| 175 | */ |
||
| 176 | 9 | private function setData(Solver $solver) |
|
| 185 | |||
| 186 | //TODO fix php doc with exceptions |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param Solver $solver |
||
| 190 | * @return Solver |
||
| 191 | * @throws OptimiseException |
||
| 192 | */ |
||
| 193 | 9 | private function setTimeSlotsSolver(Solver $solver) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param Solver $solver |
||
| 200 | * @return Solver |
||
| 201 | * @throws OptimiseException |
||
| 202 | */ |
||
| 203 | 9 | private function setUsers(Solver $solver) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param Solver $solver |
||
| 214 | * @return Solver |
||
| 215 | * @throws OptimiseException |
||
| 216 | */ |
||
| 217 | 6 | private function setAllMeetingsInfo(Solver $solver) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param mixed $item |
||
| 235 | * @return mixed |
||
| 236 | */ |
||
| 237 | 6 | private function durationConverter($item) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param int $duration |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | 6 | static private function convertDuration($duration) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param mixed $item |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | 6 | private function timeSlotsConverter($item) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param mixed $time |
||
| 271 | * @return int |
||
| 272 | * @throws OptimiseException |
||
| 273 | */ |
||
| 274 | 6 | private function toTimeSlot($time) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 290 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | 6 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free = true) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param array $array |
||
| 306 | * @param int $id |
||
| 307 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 308 | * @param string $fill |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 6 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param array $array |
||
| 323 | * @param int $from |
||
| 324 | * @param int $to |
||
| 325 | * @param string $pad |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | 6 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $array |
||
| 337 | * @param int $id |
||
| 338 | * @param string $fill |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | 6 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param Solver $solver |
||
| 353 | * @return Solver |
||
| 354 | * @throws OptimiseException |
||
| 355 | */ |
||
| 356 | 6 | private function setUserAvailability(Solver $solver) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param Solver $solver |
||
| 372 | * @return Solver |
||
| 373 | * @throws OptimiseException |
||
| 374 | */ |
||
| 375 | 6 | private function setUsersMeetings(Solver $solver) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $users |
||
| 391 | * @param array $meetings |
||
| 392 | * @param \Illuminate\Support\Collection $usersMeetings |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | 6 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @return Optimise |
||
| 414 | * @throws OptimiseException |
||
| 415 | */ |
||
| 416 | 6 | public function save() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param Solver $solver |
||
| 440 | */ |
||
| 441 | 6 | private function saveMeetings(Solver $solver) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param int $timeslot |
||
| 453 | * @return \DateTime |
||
| 454 | */ |
||
| 455 | 6 | private function toDateTime($timeslot) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param Solver $solver |
||
| 463 | */ |
||
| 464 | 6 | private function saveEmployeesMeetings(Solver $solver) |
|
| 476 | } |
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..