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 |
||
| 8 | class Box extends Widget implements Renderable |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $view = 'admin::widgets.box'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $title = 'Box header'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $content = 'here is the box content.'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $tools = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $script; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Box constructor. |
||
| 37 | * |
||
| 38 | * @param string $title |
||
| 39 | * @param string $content |
||
| 40 | */ |
||
| 41 | public function __construct($title = '', $content = '') |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set box content. |
||
| 56 | * |
||
| 57 | * @param string $content |
||
| 58 | * |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | public function content($content) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set box title. |
||
| 74 | * |
||
| 75 | * @param string $title |
||
| 76 | * |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function title($title) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set box as collapsable. |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function collapsable() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set box body scrollable |
||
| 101 | * |
||
| 102 | * @param array $options |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function scrollable($options = [], $nodeSelector = '') |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set box as removable. |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function removable() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set box style. |
||
| 133 | * |
||
| 134 | * @param string $styles |
||
| 135 | * |
||
| 136 | * @return $this|Box |
||
| 137 | */ |
||
| 138 | public function style($styles) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Add `box-solid` class to box. |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function solid() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Variables in view. |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | View Code Duplication | protected function variables() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Render box. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function render() |
||
| 188 | } |
||
| 189 |
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.