Complex classes like TaskPlanner 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 TaskPlanner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class TaskPlanner |
||
| 11 | { |
||
| 12 | /** @var array Tasks currently scheduled */ |
||
| 13 | protected $tasks = []; |
||
| 14 | |||
| 15 | /** @var TaskBag */ |
||
| 16 | protected $currentTask; |
||
| 17 | |||
| 18 | /** @var string Namespace prefix */ |
||
| 19 | protected $nsPrefix = ''; |
||
| 20 | |||
| 21 | protected $currentEnvironment = self::PRODUCTION_ENVIRONMENT; |
||
| 22 | |||
| 23 | /* Environment Constants */ |
||
| 24 | const PRODUCTION_ENVIRONMENT = 'PRODUCTION'; |
||
| 25 | const DEVELOPMENT_ENVIRONMENT = 'DEVELOPMENT'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Mistletoe\Contracts\TaskRunnerInterface |
||
| 29 | */ |
||
| 30 | protected $taskRunner; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var integer |
||
| 34 | */ |
||
| 35 | protected $closureIncrement = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $currentTime; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $testing = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Begin a new Task Chain |
||
| 49 | * @param string|\Closure $task |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 26 | public function add($task) |
|
| 63 | |||
| 64 | /* Schedule a full expression */ |
||
| 65 | /** |
||
| 66 | * Add a full expression |
||
| 67 | * @param string $expression |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 8 | public function schedule($expression) |
|
| 75 | |||
| 76 | 5 | public function always() |
|
| 80 | |||
| 81 | /* Intervals */ |
||
| 82 | /** |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 3 | public function yearly() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return TaskPlanner |
||
| 93 | */ |
||
| 94 | 2 | public function annually() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 5 | public function monthly() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | 2 | public function weekly() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | 10 | public function daily() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | 3 | public function hourly() |
|
| 134 | |||
| 135 | /* Time intervals */ |
||
| 136 | |||
| 137 | 1 | public function everyXHours($hrs) |
|
| 142 | |||
| 143 | 1 | public function everyXMinutes($mins) |
|
| 148 | |||
| 149 | /* @todo: this could be cleaned up and expanded for minutes, hours, days, and weeks |
||
| 150 | * @param $name |
||
| 151 | * @param $arguments |
||
| 152 | * @return |
||
| 153 | */ |
||
| 154 | 1 | public function __call($name, $arguments) |
|
| 183 | |||
| 184 | /* Times */ |
||
| 185 | /** |
||
| 186 | * @param string $time |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 12 | public function at($time) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return TaskPlanner |
||
| 197 | */ |
||
| 198 | 3 | public function atMidnight() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return TaskPlanner |
||
| 205 | */ |
||
| 206 | 1 | public function atNoon() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string|array|integer $minute |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | 2 | public function atMinute($minute) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param string|array|integer $minute |
||
| 223 | * @return TaskPlanner |
||
| 224 | */ |
||
| 225 | 2 | public function andAtMinute($minute) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string|array|integer $hour |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | 2 | public function atHour($hour) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string|array|integer $hour |
||
| 242 | * @return TaskPlanner |
||
| 243 | */ |
||
| 244 | 1 | public function andAtHour($hour) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param string|array|integer $date |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 4 | public function on($date) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param string|array|integer $date |
||
| 261 | * @return TaskPlanner |
||
| 262 | */ |
||
| 263 | 2 | public function andOn($date) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param string|array|integer $day |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 5 | public function onDay($day) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param string|array|integer $day |
||
| 280 | * @return TaskPlanner |
||
| 281 | */ |
||
| 282 | 4 | public function andOnDay($day) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string|array|integer $month |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | 4 | public function onMonth($month) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param string|array|integer $month |
||
| 299 | * @return TaskPlanner |
||
| 300 | */ |
||
| 301 | 1 | public function andOnMonth($month) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param string|array|integer $weekday |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | 3 | public function onWeekday($weekday) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param string|array|integer $weekday |
||
| 318 | * @return TaskPlanner |
||
| 319 | */ |
||
| 320 | public function andOnWeekday($weekday) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | 2 | public function onSunday() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return TaskPlanner |
||
| 335 | */ |
||
| 336 | public function onMonday() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return TaskPlanner |
||
| 343 | */ |
||
| 344 | public function onTuesday() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return TaskPlanner |
||
| 351 | */ |
||
| 352 | public function onWednesday() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return TaskPlanner |
||
| 359 | */ |
||
| 360 | 1 | public function onThursday() |
|
| 361 | { |
||
| 362 | return $this->onWeekday(4); |
||
| 363 | 1 | } |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @return TaskPlanner |
||
| 367 | */ |
||
| 368 | public function onFriday() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return TaskPlanner |
||
| 375 | */ |
||
| 376 | 3 | public function onSaturday() |
|
| 380 | |||
| 381 | /* Environments */ |
||
| 382 | /** |
||
| 383 | * Restrict task to a specific environment |
||
| 384 | * @param string $environment |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | 2 | public function onEnvironment($environment) |
|
| 392 | |||
| 393 | public function setEnvironments(array $environment) |
||
| 402 | |||
| 403 | /* Alias for onEnvironment() */ |
||
| 404 | /** |
||
| 405 | * @return TaskPlanner |
||
| 406 | */ |
||
| 407 | 2 | public function onProductionOnly() |
|
| 411 | |||
| 412 | /* Alias for onEnvironment() */ |
||
| 413 | /** |
||
| 414 | * @return TaskPlanner |
||
| 415 | */ |
||
| 416 | 2 | public function onDevelopmentOnly() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $task |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | 7 | public function followedBy($task) |
|
| 430 | |||
| 431 | /* Running Tasks */ |
||
| 432 | /** |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | 1 | public function runDueTasks() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function runAllTasks() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param $task |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function runTask($task) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param array $tasks |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function runTasks(array $tasks) |
||
| 465 | |||
| 466 | /* Getters and Setters */ |
||
| 467 | /** |
||
| 468 | * @param \Mistletoe\Contracts\TaskRunnerInterface $runner |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setTaskRunner(TaskRunnerInterface $runner) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return TaskRunnerInterface |
||
| 479 | */ |
||
| 480 | 1 | public function getTaskRunner() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | 7 | public function getTasks() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $name |
||
| 502 | * @return TaskBag |
||
| 503 | */ |
||
| 504 | 15 | public function getTask($name) |
|
| 508 | |||
| 509 | public function getDueTasks() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getNsPrefix() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $nsPrefix |
||
| 524 | * @return $this |
||
| 525 | */ |
||
| 526 | public function setNsPrefix($nsPrefix) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function getCurrentEnvironment() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $currentEnvironment |
||
| 542 | * @return $this |
||
| 543 | */ |
||
| 544 | public function setCurrentEnvironment($currentEnvironment) |
||
| 549 | |||
| 550 | /* Internal Methods */ |
||
| 551 | /** |
||
| 552 | * @param string $task |
||
| 553 | * @param \Closure|null $body |
||
| 554 | */ |
||
| 555 | 26 | protected function createNewTask($task, $body = null) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $task |
||
| 567 | */ |
||
| 568 | 26 | protected function setCurrentTask($task) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return TaskBag |
||
| 575 | */ |
||
| 576 | 26 | protected function getCurrentTask() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * @return null|string |
||
| 583 | */ |
||
| 584 | 1 | private function getNextClosureIncrement() |
|
| 589 | |||
| 590 | /** |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getCurrentTime() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param mixed $currentTime |
||
| 600 | * @return $this |
||
| 601 | */ |
||
| 602 | 1 | public function setCurrentTime($currentTime) |
|
| 607 | |||
| 608 | public function flagForTesting() |
||
| 612 | } |
||
| 613 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.