| Total Complexity | 2 |
| Total Lines | 124 |
| Duplicated Lines | 10.48 % |
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 |
||
| 37 | class Achieve extends \Eloquent |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $table = 'achievements'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | public $timestamps = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $guarded = ['id']; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $appends = ['title', 'description', 'image']; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var AbstractAchieve |
||
| 61 | */ |
||
| 62 | protected $cachedAchieve = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Boot |
||
| 66 | */ |
||
| 67 | public static function boot() |
||
| 68 | { |
||
| 69 | parent::boot(); |
||
| 70 | |||
| 71 | static::creating(function (Achieve $achieve) { |
||
| 72 | if (!$achieve->created_at) { |
||
| 73 | $achieve->created_at = $achieve->freshTimestamp(); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (static::has($achieve->user, $achieve->name)) { |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | \Event::fire('achieve.add', ['achieve' => $achieve]); |
||
| 81 | |||
| 82 | return null; |
||
| 83 | }); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param User $user |
||
| 88 | * @param string $name |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public static function has(User $user, $name) |
||
| 92 | { |
||
| 93 | return !!static::query() |
||
| 94 | ->where('user_id', $user->id) |
||
| 95 | ->where('name', $name) |
||
| 96 | ->count(); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 101 | */ |
||
| 102 | public function user() |
||
| 103 | { |
||
| 104 | return $this->belongsTo(User::class, 'user_id', 'id'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return AbstractAchieve |
||
| 109 | */ |
||
| 110 | public function getAchieveAttribute() |
||
| 111 | { |
||
| 112 | if ($this->cachedAchieve === null) { |
||
| 113 | $this->cachedAchieve = new $this->name; |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->cachedAchieve; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function getTitleAttribute() |
||
| 123 | { |
||
| 124 | return $this->achieve->title; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getDescriptionAttribute() |
||
| 131 | { |
||
| 132 | return $this->achieve->description; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getImageAttribute() |
||
| 139 | { |
||
| 140 | return $this->achieve->image; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $time |
||
| 145 | * @return Carbon |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function getCreatedAtAttribute($time) |
|
|
|
|||
| 148 | { |
||
| 149 | return new class($time) extends Carbon implements Arrayable |
||
| 150 | { |
||
| 151 | public function toArray() |
||
| 158 | }; |
||
| 159 | } |
||
| 160 | } |
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.