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 namespace NukaCode\Users\Presenters; |
||
| 5 | class UserPresenter extends BasePresenter {
|
||
| 6 | |||
| 7 | /******************************************************************** |
||
| 8 | * Model attributes |
||
| 9 | *******************************************************************/ |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Always uppercase a user's username |
||
| 13 | * |
||
| 14 | * @return string |
||
| 15 | */ |
||
| 16 | public function username() |
||
| 20 | |||
| 21 | public function emailLink() |
||
| 31 | |||
| 32 | /******************************************************************** |
||
| 33 | * Preferences |
||
| 34 | *******************************************************************/ |
||
| 35 | |||
| 36 | public function alertLocation() |
||
| 55 | |||
| 56 | public function popover() |
||
| 60 | |||
| 61 | public function image() |
||
| 71 | |||
| 72 | public function avatar() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Check for an avatar uploaded to the site, resort to gravatar if none exists, resort to no user image if no gravatar exists |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function gravatar() |
||
| 102 | |||
| 103 | public function onlyGravatar() |
||
| 107 | |||
| 108 | /******************************************************************** |
||
| 109 | * New attributes |
||
| 110 | *******************************************************************/ |
||
| 111 | |||
| 112 | public function profile() |
||
| 116 | |||
| 117 | public function roleList() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the number of posts from this user |
||
| 128 | * |
||
| 129 | */ |
||
| 130 | public function postsCount() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the user's css file |
||
| 140 | * |
||
| 141 | */ |
||
| 142 | public function theme() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the user's css file for the laravel style method |
||
| 149 | * |
||
| 150 | */ |
||
| 151 | public function themeStyle() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Combine the user's first and last name to produce a full name |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function fullName() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Make the join date easier to read |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function joinDate() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Make the last active date easier to read |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function lastActiveReadable() |
||
| 185 | |||
| 186 | View Code Duplication | public function online($flip = false) |
|
| 187 | {
|
||
| 188 | if ($this->lastActive >= date('Y-m-d H:i:s', strtotime('-15 minutes'))) {
|
||
| 189 | $status = ['<i class="text-success fa fa-circle"></i>', 'Online']; |
||
| 190 | } else {
|
||
| 191 | $status = ['<i class="text-error fa fa-circle"></i>', 'Offline']; |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($flip) {
|
||
| 195 | return implode(' ', array_reverse($status));
|
||
| 196 | } |
||
| 197 | |||
| 198 | return implode(' ', $status);
|
||
| 199 | } |
||
| 200 | |||
| 201 | View Code Duplication | public function onlineMaterialize($flip = false) |
|
| 215 | } |
||
| 216 |