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 |
||
| 21 | class Ping extends Model |
||
| 22 | { |
||
| 23 | use SoftDeletes; |
||
| 24 | |||
| 25 | protected $dates = ['last_ping', 'deleted_at']; |
||
| 26 | |||
| 27 | protected $appends = ['ping_url']; |
||
| 28 | |||
| 29 | protected $casts = [ |
||
| 30 | 'active' => 'boolean', |
||
| 31 | 'error' => 'boolean', |
||
| 32 | 'frequency_value' => 'integer', |
||
| 33 | 'created_by' => 'integer', |
||
| 34 | 'updated_by' => 'integer', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Attributes |
||
| 40 | */ |
||
| 41 | |||
| 42 | View Code Duplication | public function getTagsAttribute() |
|
| 51 | |||
| 52 | |||
| 53 | View Code Duplication | public function setTagsAttribute($tags) |
|
| 61 | |||
| 62 | public function getLastPingAttribute() |
||
| 70 | |||
| 71 | public function getPingUrlAttribute() |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * The date when the check becaomes overdue |
||
| 79 | * |
||
| 80 | * @return Carbon |
||
| 81 | */ |
||
| 82 | public function getOverdueDateAttribute() |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * This this ping overdue? |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function getOverdueAttribute() |
||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Scopes |
||
| 119 | */ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Scope a query to only include active pings. |
||
| 123 | * |
||
| 124 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 125 | */ |
||
| 126 | public function scopeActive($query) |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Methods |
||
| 134 | */ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Locate an existing ping or create a new one |
||
| 138 | * |
||
| 139 | * @param string $name |
||
| 140 | * |
||
| 141 | * @return Ping |
||
| 142 | */ |
||
| 143 | public static function findOrNewFromName($name) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Create a new ping object with the default starting params |
||
| 156 | * |
||
| 157 | * @param string $name |
||
| 158 | * @param bool $active |
||
| 159 | * @param null $createdBy |
||
| 160 | * |
||
| 161 | * @return Ping |
||
| 162 | */ |
||
| 163 | public static function createDefaultPing($name, $active, $createdBy = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the error state for the ping |
||
| 183 | */ |
||
| 184 | public function setError() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Clear the error state for the ping |
||
| 200 | */ |
||
| 201 | public function clearError() |
||
| 206 | |||
| 207 | public function recordPingUpdate() |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Static |
||
| 217 | */ |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * The base url needed to make a ping url |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public static function baseUrl() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Trim each item in the array |
||
| 232 | * |
||
| 233 | * @param array $tags |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | private function trimArrayItems(array $tags) |
||
| 243 | } |
||
| 244 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.