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 |
||
| 5 | class Session extends Base |
||
| 6 | { |
||
| 7 | protected $table = 'tracker_sessions'; |
||
| 8 | |||
| 9 | protected $fillable = [ |
||
| 10 | 'uuid', |
||
| 11 | 'user_id', |
||
| 12 | 'device_id', |
||
| 13 | 'language_id', |
||
| 14 | 'agent_id', |
||
| 15 | 'client_ip', |
||
| 16 | 'cookie_id', |
||
| 17 | 'referer_id', |
||
| 18 | 'geoip_id', |
||
| 19 | 'is_robot', |
||
| 20 | ]; |
||
| 21 | |||
| 22 | public function user() |
||
| 26 | |||
| 27 | public function device() |
||
| 31 | |||
| 32 | public function language() |
||
| 36 | |||
| 37 | public function agent() |
||
| 41 | |||
| 42 | public function referer() |
||
| 46 | |||
| 47 | public function geoIp() |
||
| 51 | |||
| 52 | public function cookie() |
||
| 56 | |||
| 57 | public function log() |
||
| 61 | |||
| 62 | public function getPageViewsAttribute() |
||
| 66 | |||
| 67 | View Code Duplication | public function users($minutes, $result) |
|
| 86 | |||
| 87 | View Code Duplication | public function userDevices($minutes, $result, $user_id) |
|
| 106 | } |
||
| 107 |
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.