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:
| 1 | <?php |
||
| 11 | class DutyUsers extends DutyHelper |
||
| 12 | { |
||
| 13 | public function __construct(Duty $duty) |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Generate a collection to be used for our view 'duty.show'. |
||
| 20 | * |
||
| 21 | * @return Collection |
||
| 22 | */ |
||
| 23 | public function htmlOutput() |
||
| 24 | { |
||
| 25 | $newCollection = new Collection(); |
||
| 26 | |||
| 27 | foreach ($this->list as $entry) { |
||
| 28 | if (Gate::allows('view')) { |
||
| 29 | $rowvalue = "<a href='".url('user', $entry['user']->id)."'>".$entry['user']->userFullName.'</a>'; |
||
| 30 | } else { |
||
| 31 | $rowvalue = $entry['user']->userFullName; |
||
| 32 | } |
||
| 33 | $newCollection->push([ |
||
| 34 | 'row' => $rowvalue, |
||
| 35 | 'id' => $entry['user']->id, |
||
| 36 | 'date' => $entry['date'], |
||
| 37 | ]); |
||
| 38 | } |
||
| 39 | |||
| 40 | return $newCollection; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Generate a collection to be used for emails. View is either emails.duty_future or emails.duty_today. |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Support\Collection |
||
| 47 | */ |
||
| 48 | public function emailOutput() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get function for the list. List is stored on the helper class. |
||
| 62 | * |
||
| 63 | * @return Collection |
||
| 64 | */ |
||
| 65 | public function getList() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Grab the next user to work the duty roster and record them in our database so they are the current worker. |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function recordNextEntry() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Get the current user in our database who is working the duty roster. |
||
| 85 | * |
||
| 86 | * @return DutyUsers |
||
| 87 | */ |
||
| 88 | public function getLastWorked() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a list of all users for a specific duty sorted by the user's last name. |
||
| 97 | * |
||
| 98 | * @return DutyUsers |
||
| 99 | */ |
||
| 100 | public function queryList() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Take our list of users and merge them with dates so that each user is assigned a duty date. |
||
| 109 | * |
||
| 110 | * @return DutyUsers |
||
| 111 | */ |
||
| 112 | public function combineListWithDates() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Query a list of users who we swapped around and insert them into our current duty list of users. |
||
| 131 | */ |
||
| 132 | public function insertFromDutySwap() |
||
| 153 | } |
||
| 154 |