Complex classes like Updater 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 Updater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Updater extends RepositoryUpdater |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Project\Issue |
||
| 26 | */ |
||
| 27 | protected $model; |
||
| 28 | |||
| 29 | public function __construct(Project\Issue $model) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set the issue is updated by a user. |
||
| 36 | * |
||
| 37 | * @return Project\Issue |
||
| 38 | */ |
||
| 39 | public function changeUpdatedBy() |
||
| 40 | { |
||
| 41 | $this->model->updated_by = $this->user->id; |
||
| 42 | $this->model->touch(); |
||
| 43 | |||
| 44 | return $this->save(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Reassign the issue to a new user. |
||
| 49 | * |
||
| 50 | * @param int|User $assignTo |
||
| 51 | * @param User $user |
||
| 52 | * |
||
| 53 | * @return Project\Issue |
||
| 54 | */ |
||
| 55 | public function reassign($assignTo, User $user) |
||
| 56 | { |
||
| 57 | $this->setUser($user); |
||
| 58 | $assignToId = !$assignTo instanceof User ? $assignTo : $assignTo->id; |
||
| 59 | $this->model->assigned_to = $assignToId; |
||
| 60 | |||
| 61 | // Add event on successful save |
||
| 62 | Project\Issue::saved(function (Project\Issue $issue) { |
||
| 63 | $this->queueAssign($issue, $this->user); |
||
| 64 | }); |
||
| 65 | |||
| 66 | $this->save(); |
||
| 67 | |||
| 68 | $this->saveToActivities([ |
||
| 69 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
||
| 70 | 'parent_id' => $this->model->project->id, |
||
| 71 | 'user_id' => $user->id, |
||
| 72 | 'action_id' => $this->model->assigned_to, |
||
| 73 | ]); |
||
| 74 | |||
| 75 | return $this->model; |
||
| 76 | } |
||
| 77 | |||
| 78 | protected function filterUpdateAttributes(array $input) |
||
| 79 | { |
||
| 80 | $fill = array_only($input, ['title', 'body', 'assigned_to']); |
||
| 81 | $fill['updated_by'] = $this->model->updatedBy->id; |
||
| 82 | $fill['lock_quote'] = (bool)isset($input['time_quote']['lock']); |
||
| 83 | |||
| 84 | // Only save quote if not locked or locked & user allowed to modify it |
||
| 85 | if (array_key_exists('time_quote', $input) && |
||
| 86 | (!$this->model->isQuoteLocked() || $this->getLoggedUser()->can('lockQuote', $this->model)) |
||
| 87 | ) { |
||
| 88 | $fill['time_quote'] = $input['time_quote']; |
||
| 89 | } |
||
| 90 | |||
| 91 | return $fill; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Update the given issue. |
||
| 96 | * |
||
| 97 | * @param array $input |
||
| 98 | * |
||
| 99 | * @return Project\Issue |
||
| 100 | */ |
||
| 101 | public function update(array $input = []) |
||
| 102 | { |
||
| 103 | $this->model->fill($this->filterUpdateAttributes($input)); |
||
| 104 | |||
| 105 | /* Add to activity log for assignment if changed */ |
||
| 106 | if ($this->model->isDirty('assigned_to') && $this->model->assigned_to > 0) { |
||
| 107 | $this->saveToActivities([ |
||
| 108 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
||
| 109 | 'parent_id' => $this->model->project->id, |
||
| 110 | 'user_id' => $this->model->updatedBy->id, |
||
| 111 | 'action_id' => $this->model->assigned_to, |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->syncTags($input, $this->model->tags()->with('parent')->get()); |
||
| 116 | |||
| 117 | // Add event on successful save |
||
| 118 | Project\Issue::saved(function (Project\Issue $issue) { |
||
| 119 | $this->queueUpdate($issue, $this->user); |
||
| 120 | }); |
||
| 121 | |||
| 122 | return $this->save(); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Create a new issue. |
||
| 127 | * |
||
| 128 | * @param array $input |
||
| 129 | * |
||
| 130 | * @return Project\Issue |
||
| 131 | */ |
||
| 132 | public function create(array $input) |
||
| 133 | { |
||
| 134 | if (array_key_exists('project_id', $input)) { |
||
| 135 | $this->model->setRelation('project', Project::find((int)$input['project_id'])); |
||
| 136 | } |
||
| 137 | |||
| 138 | $fill = [ |
||
| 139 | 'created_by' => $this->model->user->id, |
||
| 140 | 'project_id' => $this->model->project->id, |
||
| 141 | 'title' => $input['title'], |
||
| 142 | 'body' => $input['body'], |
||
| 143 | 'assigned_to' => (int)$this->model->project->default_assignee, |
||
| 144 | ]; |
||
| 145 | |||
| 146 | if ($this->model->user->isDeveloperOrMore()) { |
||
| 147 | $fill['assigned_to'] = array_get($input, 'assigned_to', $fill['assigned_to']); |
||
| 148 | $fill['time_quote'] = array_get($input, 'time_quote'); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Project internal issue number |
||
| 152 | $this->model->issue_no = $this->model->forProject($this->model->project->id)->max('issue_no') + 1; |
||
|
|
|||
| 153 | |||
| 154 | $this->model->fill($fill)->save(); |
||
| 155 | |||
| 156 | // Add issue to messages queue |
||
| 157 | $this->queueAdd($this->model, $this->user); |
||
| 158 | |||
| 159 | /* Add to user's activity log */ |
||
| 160 | $this->saveToActivities([ |
||
| 161 | 'type_id' => Activity::TYPE_CREATE_ISSUE, |
||
| 162 | 'parent_id' => $this->model->project->id, |
||
| 163 | 'user_id' => $this->model->user->id, |
||
| 164 | ]); |
||
| 165 | |||
| 166 | /* Add attachments to issue */ |
||
| 167 | Project\Issue\Attachment::instance()->updater()->updateIssueToken($input['upload_token'], $this->model->user->id, $this->model->id); |
||
| 168 | |||
| 169 | // Add default tag to newly created issue |
||
| 170 | $defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
||
| 171 | if ($defaultTag > 0 && empty($input['tag_status'])) { |
||
| 172 | $input['tag_status'] = $defaultTag; |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->syncTags($input); |
||
| 176 | |||
| 177 | return $this->model; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Move the issue (comments & activities) to another project. |
||
| 182 | * |
||
| 183 | * @param int $projectId |
||
| 184 | * |
||
| 185 | * @return Project\Issue |
||
| 186 | */ |
||
| 187 | public function changeProject($projectId) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Delete an issue. |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | * |
||
| 211 | * @throws \Exception |
||
| 212 | */ |
||
| 213 | public function delete() |
||
| 217 | |||
| 218 | protected function deleteIssue() |
||
| 219 | { |
||
| 220 | // Delete issue related data |
||
| 221 | $this->deleteComments(); |
||
| 222 | $this->deleteAttachments(); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | protected function deleteComments() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | protected function deleteAttachments() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | protected function deleteIssueTags() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | protected function deleteUserActivities() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Change the status of an issue. |
||
| 270 | * |
||
| 271 | * @param int $status |
||
| 272 | * @param User $user |
||
| 273 | * |
||
| 274 | * @return Project\Issue |
||
| 275 | */ |
||
| 276 | public function changeStatus($status, User $user) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Sync the issue tags. |
||
| 307 | * |
||
| 308 | * @param array $input |
||
| 309 | * @param Collection $currentTags |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function syncTags(array $input, Collection $currentTags = null) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Add tag to the issue & close issue if added tag is Closed. |
||
| 384 | * |
||
| 385 | * @param Tag $newTag |
||
| 386 | * @param Tag $oldTag |
||
| 387 | * |
||
| 388 | * @return Project\Issue |
||
| 389 | */ |
||
| 390 | public function changeKanbanTag(Tag $newTag, Tag $oldTag) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Insert update issue to message queue. |
||
| 429 | * |
||
| 430 | * @param Project\Issue $issue |
||
| 431 | * @param User $changeBy |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function queueUpdate(Project\Issue $issue, User $changeBy) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param Project\Issue $issue |
||
| 452 | * @param User $changeBy |
||
| 453 | * @param bool|int $noMessageForMe |
||
| 454 | */ |
||
| 455 | protected function queueUpdateIssue(Project\Issue $issue, User $changeBy, $noMessageForMe = false) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param Project\Issue $issue |
||
| 467 | * @param User $changeBy |
||
| 468 | */ |
||
| 469 | protected function queueClosedIssue(Project\Issue $issue, User $changeBy) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param Project\Issue $issue |
||
| 478 | * @param User $changeBy |
||
| 479 | */ |
||
| 480 | protected function queueReopenedIssue(Project\Issue $issue, User $changeBy) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Insert add issue to message queue. |
||
| 489 | * |
||
| 490 | * @param Project\Issue $issue |
||
| 491 | * @param User $changeBy |
||
| 492 | * |
||
| 493 | * @return void |
||
| 494 | */ |
||
| 495 | public function queueAdd(Project\Issue $issue, User $changeBy) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Insert assign issue to message queue. |
||
| 502 | * |
||
| 503 | * @param Project\Issue $issue |
||
| 504 | * @param User $changeBy |
||
| 505 | * |
||
| 506 | * @return bool|int |
||
| 507 | */ |
||
| 508 | public function queueAssign(Project\Issue $issue, User $changeBy) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Insert issue tag changes to message queue. |
||
| 525 | * |
||
| 526 | * @param Project\Issue $issue |
||
| 527 | * @param array $addedTags |
||
| 528 | * @param array $removedTags |
||
| 529 | * @param User $changeBy |
||
| 530 | * |
||
| 531 | * @return mixed |
||
| 532 | */ |
||
| 533 | public function queueChangeTags(Project\Issue $issue, array $addedTags, array $removedTags, User $changeBy) |
||
| 537 | } |
||
| 538 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: