| Total Complexity | 45 |
| Total Lines | 396 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 1 | Features | 3 |
Complex classes like TaskRecurrence 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.
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 TaskRecurrence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class TaskRecurrence extends BaseRecurrence { |
||
| 10 | /** |
||
| 11 | * Timezone info which is always false for task. |
||
| 12 | * |
||
| 13 | * @var false |
||
| 14 | */ |
||
| 15 | public $tz = false; |
||
| 16 | |||
| 17 | private $action; |
||
| 18 | |||
| 19 | public function __construct(mixed $store, mixed $message) { |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Function which saves recurrence and also regenerates task if necessary. |
||
| 70 | * |
||
| 71 | * @param mixed $recur new recurrence properties |
||
| 72 | * |
||
| 73 | * @return array|bool of properties of regenerated task else false |
||
| 74 | */ |
||
| 75 | public function setRecurrence(mixed &$recur): array|bool { |
||
| 76 | $this->recur = $recur; |
||
| 77 | $this->action = &$recur; |
||
| 78 | |||
| 79 | $this->recur["changed_occurrences"] ??= []; |
||
| 80 | $this->recur["deleted_occurrences"] ??= []; |
||
| 81 | $this->recur['startocc'] ??= 0; |
||
| 82 | $this->recur['endocc'] ??= 0; |
||
| 83 | |||
| 84 | // Save recurrence because we need proper startrecurrdate and endrecurrdate |
||
| 85 | $this->saveRecurrence(); |
||
| 86 | |||
| 87 | // Update $this->recur with proper startrecurrdate and endrecurrdate updated after saving recurrence |
||
| 88 | $msgProps = mapi_getprops($this->message, [$this->proptags['recurring_data']]); |
||
| 89 | $recurring_data = $this->parseRecurrence($msgProps[$this->proptags['recurring_data']]); |
||
| 90 | foreach ($recurring_data as $key => $value) { |
||
| 91 | $this->recur[$key] = $value; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->setFirstOccurrence(); |
||
| 95 | |||
| 96 | // Let's see if next occurrence has to be generated |
||
| 97 | return $this->moveToNextOccurrence(); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Sets task object to first occurrence if startdate/duedate of task object is different from first occurrence. |
||
| 102 | */ |
||
| 103 | public function setFirstOccurrence(): void { |
||
| 104 | // Check if it is already the first occurrence |
||
| 105 | if ($this->action['start'] == $this->recur["start"]) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | $items = $this->getNextOccurrence(); |
||
| 109 | |||
| 110 | $props = []; |
||
| 111 | $props[$this->proptags['startdate']] = $items[$this->proptags['startdate']]; |
||
| 112 | $props[$this->proptags['commonstart']] = $items[$this->proptags['startdate']]; |
||
| 113 | |||
| 114 | $props[$this->proptags['duedate']] = $items[$this->proptags['duedate']]; |
||
| 115 | $props[$this->proptags['commonend']] = $items[$this->proptags['duedate']]; |
||
| 116 | |||
| 117 | mapi_setprops($this->message, $props); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Function which creates new task as current occurrence and moves the |
||
| 122 | * existing task to next occurrence. |
||
| 123 | * |
||
| 124 | * @return array|bool properties of newly created task if moving to next occurrence succeeds |
||
| 125 | * false if that was last occurrence |
||
| 126 | */ |
||
| 127 | public function moveToNextOccurrence(): array|bool { |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Function which return properties of next occurrence. |
||
| 187 | * |
||
| 188 | * @return array|false|null startdate/enddate of next occurrence |
||
| 189 | */ |
||
| 190 | public function getNextOccurrence(): mixed { |
||
| 191 | if ($this->recur) { |
||
| 192 | // @TODO: fix start of range |
||
| 193 | $start = $this->messageprops[$this->proptags["duedate"]] ?? $this->action['start']; |
||
| 194 | $dayend = ($this->recur['term'] == 0x23) ? 0x7FFFFFFF : $this->dayStartOf($this->recur["end"]); |
||
| 195 | |||
| 196 | // Fix recur object |
||
| 197 | $this->recur['startocc'] = 0; |
||
| 198 | $this->recur['endocc'] = 0; |
||
| 199 | |||
| 200 | // Retrieve next occurrence |
||
| 201 | $items = $this->getItems($start, $dayend, 1); |
||
| 202 | |||
| 203 | return !empty($items) ? $items[0] : false; |
||
| 204 | } |
||
| 205 | |||
| 206 | return null; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Function which clones current occurrence and sets appropriate properties. |
||
| 211 | * The original recurring item is moved to next occurrence. |
||
| 212 | * |
||
| 213 | * @param bool $markComplete true if existing occurrence has to be marked complete |
||
| 214 | */ |
||
| 215 | public function regenerateTask(bool $markComplete): array { |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * processOccurrenceItem, adds an item to a list of occurrences, but only if the |
||
| 305 | * resulting occurrence starts or ends in the interval <$start, $end>. |
||
| 306 | * |
||
| 307 | * @param array $items reference to the array to be added to |
||
| 308 | * @param int $start start of timeframe in GMT TIME |
||
| 309 | * @param int $end end of timeframe in GMT TIME |
||
| 310 | * @param int $basedate (hour/sec/min assumed to be 00:00:00) in LOCAL TIME OF THE OCCURRENCE |
||
| 311 | * @param int $startocc start of occurrence since beginning of day in minutes |
||
| 312 | * @param int $endocc end of occurrence since beginning of day in minutes |
||
| 313 | * @param int $tz the timezone info for this occurrence ( applied to $basedate / $startocc / $endocc ) |
||
| 314 | * @param bool $reminderonly If TRUE, only add the item if the reminder is set |
||
| 315 | */ |
||
| 316 | public function processOccurrenceItem(array &$items, mixed $start, int $end, mixed $basedate, mixed $startocc, mixed $endocc, mixed $tz, mixed $reminderonly): ?false { |
||
| 317 | if ($basedate > $start) { |
||
| 318 | $newItem = []; |
||
| 319 | $newItem[$this->proptags['startdate']] = $basedate; |
||
| 320 | |||
| 321 | // If startdate and enddate are set on task, then slide enddate according to duration |
||
| 322 | if (isset($this->messageprops[$this->proptags["startdate"]], $this->messageprops[$this->proptags["duedate"]])) { |
||
| 323 | $newItem[$this->proptags['duedate']] = $newItem[$this->proptags['startdate']] + ($this->messageprops[$this->proptags["duedate"]] - $this->messageprops[$this->proptags["startdate"]]); |
||
| 324 | } |
||
| 325 | else { |
||
| 326 | $newItem[$this->proptags['duedate']] = $newItem[$this->proptags['startdate']]; |
||
| 327 | } |
||
| 328 | |||
| 329 | $items[] = $newItem; |
||
| 330 | } |
||
| 331 | |||
| 332 | return null; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Function which marks existing occurrence to 'Complete'. |
||
| 337 | * |
||
| 338 | * @param array $recur array action from client |
||
| 339 | * |
||
| 340 | * @return array|bool of properties of regenerated task else false |
||
| 341 | */ |
||
| 342 | public function markOccurrenceComplete(array &$recur): array|bool { |
||
| 343 | // Fix timezone object |
||
| 344 | $this->tz = false; |
||
| 345 | $this->action = &$recur; |
||
| 346 | $dead_occurrence = $this->messageprops[$this->proptags['dead_occurrence']] ?? false; |
||
| 347 | |||
| 348 | if (!$dead_occurrence) { |
||
| 349 | return $this->moveToNextOccurrence(); |
||
| 350 | } |
||
| 351 | |||
| 352 | return false; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Function which sets reminder on recurring task after existing occurrence has been deleted or marked complete. |
||
| 357 | * |
||
| 358 | * @param mixed $nextOccurrence properties of next occurrence |
||
| 359 | */ |
||
| 360 | public function setReminder(mixed $nextOccurrence): void { |
||
| 361 | $props = []; |
||
| 362 | if (!empty($nextOccurrence)) { |
||
| 363 | // Check if reminder is reset. Default is 'false' |
||
| 364 | $reset_reminder = $this->messageprops[$this->proptags['reset_reminder']] ?? false; |
||
| 365 | $reminder = $this->messageprops[$this->proptags['reminder']]; |
||
| 366 | |||
| 367 | // Either reminder was already set OR reminder was set but was dismissed bty user |
||
| 368 | if ($reminder || $reset_reminder) { |
||
| 369 | // Reminder can be set at any time either before or after the duedate, so get duration between the reminder time and duedate |
||
| 370 | $reminder_time = $this->messageprops[$this->proptags['reminder_time']] ?? 0; |
||
| 371 | $reminder_difference = $this->messageprops[$this->proptags['duedate']] ?? 0; |
||
| 372 | $reminder_difference = $reminder_difference - $reminder_time; |
||
| 373 | |||
| 374 | // Apply duration to next calculated duedate |
||
| 375 | $next_reminder_time = $nextOccurrence[$this->proptags['duedate']] - $reminder_difference; |
||
| 376 | |||
| 377 | $props[$this->proptags['reminder_time']] = $next_reminder_time; |
||
| 378 | $props[$this->proptags['flagdueby']] = $next_reminder_time; |
||
| 379 | $this->action['reminder'] = $props[$this->proptags['reminder']] = true; |
||
| 380 | } |
||
| 381 | } |
||
| 382 | else { |
||
| 383 | // Didn't get next occurrence, probably this is the last occurrence |
||
| 384 | $props[$this->proptags['reminder']] = false; |
||
| 385 | $props[$this->proptags['reset_reminder']] = false; |
||
| 386 | } |
||
| 387 | |||
| 388 | if (!empty($props)) { |
||
| 389 | mapi_setprops($this->message, $props); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Function which recurring task to next occurrence. |
||
| 395 | * It simply doesn't regenerate task. |
||
| 396 | */ |
||
| 397 | public function deleteOccurrence(array $action): array|bool { |
||
| 405 | } |
||
| 406 | } |
||
| 407 |