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 |
||
| 31 | class Menu extends Node |
||
| 32 | { |
||
| 33 | use PresentableTrait, PublishableTrait, CanRequireAuthentication; |
||
| 34 | use AuditableTrait, HasParameters; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $touches = ['navigation']; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Yajra\CMS\Presenters\MenuPresenter |
||
| 43 | */ |
||
| 44 | protected $presenter = MenuPresenter::class; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $fillable = [ |
||
| 50 | 'title', |
||
| 51 | 'url', |
||
| 52 | 'target', |
||
| 53 | 'order', |
||
| 54 | 'parent_id', |
||
| 55 | 'published', |
||
| 56 | 'authenticated', |
||
| 57 | 'authorization', |
||
| 58 | 'parameters', |
||
| 59 | 'type', |
||
| 60 | ]; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 64 | */ |
||
| 65 | public function navigation() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Related menu permissions. |
||
| 72 | * |
||
| 73 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 74 | */ |
||
| 75 | public function permissions() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get related article. |
||
| 82 | * |
||
| 83 | * @return \Yajra\CMS\Entities\Article |
||
| 84 | */ |
||
| 85 | public function article() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get related category. |
||
| 92 | * |
||
| 93 | * @return \Yajra\CMS\Entities\Article |
||
| 94 | */ |
||
| 95 | public function category() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Menu type relation. |
||
| 105 | * |
||
| 106 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 107 | */ |
||
| 108 | public function menuType() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get list of possible parent node. |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function getParentsList() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Check if the menu has the given widget. |
||
| 153 | * |
||
| 154 | * @param mixed $widget |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function hasWidget($widget) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get related widgets. |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function widgets() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if the menu is currently selected/active. |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function isActive() |
||
| 185 | } |
||
| 186 |
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.