Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SendMessages 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 SendMessages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class SendMessages extends SendMessagesAbstract |
||
| 28 | { |
||
| 29 | protected $template = 'update_issue'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Collection of tags. |
||
| 33 | * |
||
| 34 | * @var Collection |
||
| 35 | */ |
||
| 36 | protected $tags; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns an instance of Issue. |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | 3 | protected function getIssue() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Returns an instance of Project. |
||
| 54 | * |
||
| 55 | * @return Project |
||
| 56 | */ |
||
| 57 | 3 | protected function getProject() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the project id. |
||
| 64 | * |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | 3 | protected function getProjectId() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Returns message data belongs to adding an issue. |
||
| 74 | * |
||
| 75 | * @param Queue $queue |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | 3 | protected function getMessageDataForAddIssue(Queue $queue) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Returns message data belongs to updating an issue. |
||
| 103 | * |
||
| 104 | * @param Queue $queue |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | 1 | protected function getMessageDataForUpdateIssue(Queue $queue) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Returns message data belongs to reopening an issue. |
||
| 134 | * |
||
| 135 | * @param Queue $queue |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | protected function getMessageDataForReopenIssue(Queue $queue) |
||
| 140 | { |
||
| 141 | $messageData = []; |
||
| 142 | $messageData['changeByHeading'] = $queue->changeBy->fullname . ' reopened an issue'; |
||
| 143 | $statusNow = $this->issue |
||
| 144 | ->tags()->with('parent')->get()->where('parent.name', Tag::GROUP_STATUS)->last(); |
||
| 145 | $messageData['changes']['status'] = [ |
||
| 146 | 'was' => trans('tinyissue.closed'), |
||
| 147 | 'now' => ($statusNow ? $statusNow->fullname : ''), |
||
| 148 | 'id' => ($statusNow ? $statusNow->id : ''), |
||
| 149 | ]; |
||
| 150 | |||
| 151 | return $messageData; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns message data belongs to closing an issue. |
||
| 156 | * |
||
| 157 | * @param Queue $queue |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | protected function getMessageDataForCloseIssue(Queue $queue) |
||
| 162 | { |
||
| 163 | $messageData = []; |
||
| 164 | $messageData['changeByHeading'] = $queue->changeBy->fullname . ' closed an issue'; |
||
| 165 | $statusWas = $this->issue |
||
| 166 | ->tags()->with('parent')->get()->where('parent.name', Tag::GROUP_STATUS)->last(); |
||
| 167 | $messageData['changes']['status'] = [ |
||
| 168 | 'was' => ($statusWas ? $statusWas->fullname : ''), |
||
| 169 | 'now' => trans('tinyissue.closed'), |
||
| 170 | ]; |
||
| 171 | |||
| 172 | return $messageData; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns message data belongs to assigning an issue to a user. |
||
| 177 | * |
||
| 178 | * @param Queue $queue |
||
| 179 | * @param array $extraData |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function getMessageDataForAssignIssue(Queue $queue, array $extraData) |
||
| 184 | { |
||
| 185 | $messageData = []; |
||
| 186 | if (!array_key_exists('now', $extraData)) { |
||
| 187 | $assignTo = $this->getUserById($queue->getDataFromPayload('dirty.assigned_to')); |
||
| 188 | $extraData = ['now' => $assignTo->fullname]; |
||
| 189 | } |
||
| 190 | |||
| 191 | $messageData['changes']['assignee'] = $extraData; |
||
| 192 | $messageData['changeByHeading'] = $queue->changeBy->fullname . ' assigned an issue to ' . $extraData['now']; |
||
| 193 | |||
| 194 | return $messageData; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns message data belongs to changing an issue tags. |
||
| 199 | * |
||
| 200 | * @param Queue $queue |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 2 | protected function getMessageDataForChangeTagIssue(Queue $queue) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Check that the issue is load. |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 3 | protected function validateData() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Populate assigned relation in the current issue. |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | 3 | View Code Duplication | protected function populateData() |
|
1 ignored issue
–
show
|
|||
| 247 | { |
||
| 248 | 3 | $this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to)); |
|
| 249 | 3 | $creator = $this->getUserById($this->issue->created_by); |
|
| 250 | 3 | if ($creator) { |
|
| 251 | 3 | $this->issue->setRelation('user', $creator); |
|
| 252 | } |
||
| 253 | 3 | $this->loadIssueCreatorToProjectUsers(); |
|
| 254 | 3 | } |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Check if the latest message is about closing or reopening an issue. |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | 3 | public function isStatusMessage() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Process assign to user message. Send direct message to new and previous users and full subscribers. |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | 3 | protected function processDirectMessages() |
|
| 273 | { |
||
| 274 | // Stop if issue is closed |
||
| 275 | 3 | if (!$this->getIssue()->isOpen()) { |
|
| 276 | return; |
||
| 277 | } |
||
| 278 | |||
| 279 | // Fetch all of the assign issue changes |
||
| 280 | 3 | $assignMessages = $this->allMessages->where('event', Queue::ASSIGN_ISSUE); |
|
| 281 | |||
| 282 | // Skip if no changes |
||
| 283 | 3 | if ($assignMessages->isEmpty()) { |
|
| 284 | 3 | return; |
|
| 285 | } |
||
| 286 | |||
| 287 | // Fetch the latest assignee |
||
| 288 | /** @var Queue $assignMessage */ |
||
| 289 | $assignMessage = $assignMessages->first(); |
||
| 290 | |||
| 291 | // Fetch the user details of the new assignee & previous assignee if this isn't new issue |
||
| 292 | $assigns = []; |
||
| 293 | $assigns['new'] = (int) $assignMessage->getDataFromPayload('dirty.assigned_to'); |
||
| 294 | if (!$this->addMessage) { |
||
| 295 | $previousAssignMessage = $assignMessages->last(); |
||
| 296 | $assigns['old'] = (int) $previousAssignMessage->getDataFromPayload('origin.assigned_to'); |
||
| 297 | } |
||
| 298 | |||
| 299 | // Fetch users objects for old and new assignee |
||
| 300 | /** @var \Illuminate\Database\Eloquent\Collection $assignObjects */ |
||
| 301 | $assignObjects = (new User())->whereIn('id', $assigns)->get(); |
||
| 302 | |||
| 303 | // If for what ever reason the user does not exists, skip this change |
||
| 304 | // or if there is only one user and is not matching the new assignee. |
||
| 305 | // then skip this message |
||
| 306 | if ($assignObjects->count() === 0 |
||
| 307 | || ($assignObjects->count() === 1 && (int) $assignObjects->first()->id !== $assigns['new']) |
||
| 308 | ) { |
||
| 309 | return; |
||
| 310 | } |
||
| 311 | |||
| 312 | // Get the object of the new assignee |
||
| 313 | $assignTo = $assignObjects->where('id', $assigns['new'], false)->first(); |
||
| 314 | $users = collect([$this->createProjectUserObject($assignTo)]); |
||
| 315 | |||
| 316 | // Exclude the user from any other message for this issue |
||
| 317 | $this->addToExcludeUsers($assignTo); |
||
| 318 | |||
| 319 | // Data about new and previous assignee |
||
| 320 | $extraMessageData = [ |
||
| 321 | 'now' => $assignTo->fullname, |
||
| 322 | ]; |
||
| 323 | |||
| 324 | // Make sure that the previous assignee was not the same as the new user |
||
| 325 | if (array_key_exists('old', $assigns) && $assigns['old'] > 0 && $assigns['new'] !== $assigns['old']) { |
||
| 326 | $previousAssign = $assignObjects->where('id', $assigns['old'], false)->first(); |
||
| 327 | if ($previousAssign) { |
||
| 328 | $extraMessageData['was'] = $previousAssign->fullname; |
||
| 329 | $users->push($this->createProjectUserObject($previousAssign)); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | // Get message data needed for the message & send |
||
| 334 | $messageData = $this->getMessageData($assignMessage, $extraMessageData); |
||
| 335 | $this->sendMessages($users, $messageData); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Create user project object for a user. |
||
| 340 | * |
||
| 341 | * @param User $user |
||
| 342 | * |
||
| 343 | * @return Project\User |
||
| 344 | */ |
||
| 345 | protected function createProjectUserObject(User $user) |
||
| 346 | { |
||
| 347 | $userProject = new Project\User([ |
||
| 348 | 'user_id' => $user->id, |
||
| 349 | 'project_id' => $this->getProjectId(), |
||
| 350 | ]); |
||
| 351 | $userProject->setRelation('user', $user); |
||
| 352 | $userProject->setRelation('project', $this->getProject()); |
||
| 353 | |||
| 354 | return $userProject; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get collection of tags or one by ID. |
||
| 359 | * |
||
| 360 | * @param null|int $tagId |
||
| 361 | * |
||
| 362 | * @return \Illuminate\Support\Collection|Tag |
||
| 363 | */ |
||
| 364 | 2 | protected function getTags($tagId = null) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Whether or not the user wants to receive the message. |
||
| 386 | * |
||
| 387 | * @param Project\User $user |
||
| 388 | * @param array $data |
||
| 389 | * |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 3 | protected function wantToReceiveMessage(Project\User $user, array $data) |
|
| 428 | } |
||
| 429 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.