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 | 3 | public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
|
| 69 | { |
||
| 70 | 3 | $this->company = $company; |
|
|
|
|||
| 71 | 3 | $this->schedule = $schedule; |
|
| 72 | 3 | $this->laravel = $laravel; |
|
| 73 | 3 | $this->max_time_slots = config('app.timeslots.max'); |
|
| 74 | 3 | $this->time_slots = config('app.timeslots.number'); |
|
| 75 | |||
| 76 | |||
| 77 | 3 | $this->setStartTime((new \DateTime())->modify('next monday')); |
|
| 78 | 3 | } |
|
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \DateTime $startTime |
||
| 83 | */ |
||
| 84 | 3 | public function setStartTime(\DateTime $startTime) |
|
| 85 | { |
||
| 86 | 3 | $this->startTime = clone $startTime; |
|
| 87 | 3 | $this->endTime = clone $this->startTime; |
|
| 88 | 3 | $this->endTime->add(new \DateInterval('PT' . (($this->max_time_slots + $this->time_slots) * |
|
| 89 | 3 | config('app.timeslots.duration')) . 'S')); |
|
| 90 | 3 | } |
|
| 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) |
|
| 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 | 3 | public function getSolver() |
|
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @return Optimise |
||
| 152 | */ |
||
| 153 | 3 | public function optimise() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param Solver $solver |
||
| 170 | * @return Solver |
||
| 171 | * @throws OptimiseException |
||
| 172 | */ |
||
| 173 | 3 | private function setData(Solver $solver) |
|
| 182 | |||
| 183 | //TODO fix php doc with exceptions |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param Solver $solver |
||
| 187 | * @return Solver |
||
| 188 | * @throws OptimiseException |
||
| 189 | */ |
||
| 190 | 3 | private function setTimeSlotsSolver(Solver $solver) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param Solver $solver |
||
| 197 | * @return Solver |
||
| 198 | */ |
||
| 199 | 3 | private function setUsers(Solver $solver) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param Solver $solver |
||
| 208 | * @return Solver |
||
| 209 | */ |
||
| 210 | 3 | private function setAllMeetingsInfo(Solver $solver) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param mixed $item |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | 3 | private function durationConverter($item) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param int $duration |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | 3 | static private function convertDuration($duration) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param mixed $item |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 3 | private function timeSlotsConverter($item) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param mixed $time |
||
| 262 | * @return int |
||
| 263 | * @throws OptimiseException |
||
| 264 | */ |
||
| 265 | 3 | private function toTimeSlot($time) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 281 | * @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | 3 | static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free = true) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $array |
||
| 297 | * @param int $id |
||
| 298 | * @param \Illuminate\Support\Collection $timeSlots |
||
| 299 | * @param string $fill |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | 3 | static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param array $array |
||
| 314 | * @param int $from |
||
| 315 | * @param int $to |
||
| 316 | * @param string $pad |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 3 | static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param array $array |
||
| 328 | * @param int $id |
||
| 329 | * @param string $fill |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | 3 | static private function fillRow(array $array, $id, $until, $fill = '0') |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param Solver $solver |
||
| 344 | * @return Solver |
||
| 345 | * @throws OptimiseException |
||
| 346 | */ |
||
| 347 | 3 | private function setUserAvailability(Solver $solver) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param Solver $solver |
||
| 361 | * @return Solver |
||
| 362 | * @throws OptimiseException |
||
| 363 | */ |
||
| 364 | 3 | private function setUsersMeetings(Solver $solver) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param array $users |
||
| 378 | * @param array $meetings |
||
| 379 | * @param \Illuminate\Support\Collection $usersMeetings |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 3 | static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * @return Optimise |
||
| 401 | */ |
||
| 402 | 3 | public function save() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param Solver $solver |
||
| 426 | */ |
||
| 427 | 3 | private function saveMeetings(Solver $solver) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param int $timeslot |
||
| 439 | * @return \DateTime |
||
| 440 | */ |
||
| 441 | 3 | private function toDateTime($timeslot) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param Solver $solver |
||
| 449 | */ |
||
| 450 | 3 | private function saveEmployeesMeetings(Solver $solver) |
|
| 462 | } |
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..