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 |
||
12 | class Tools implements Renderable |
||
13 | { |
||
14 | /** |
||
15 | * @var Builder |
||
16 | */ |
||
17 | protected $form; |
||
18 | |||
19 | /** |
||
20 | * Collection of tools. |
||
21 | * |
||
22 | * @var Collection |
||
23 | */ |
||
24 | protected $tools; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options = [ |
||
30 | 'enableListButton' => true, |
||
31 | 'enableBackButton' => true, |
||
32 | 'enableSaveButton' => true |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Create a new Tools instance. |
||
37 | * |
||
38 | * @param Builder $builder |
||
39 | */ |
||
40 | public function __construct(Builder $builder) |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | protected function backButton() |
||
69 | |||
70 | public function listButton() |
||
83 | |||
84 | /** |
||
85 | * Prepend a tool. |
||
86 | * |
||
87 | * @param string $tool |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function add($tool) |
||
97 | |||
98 | /** |
||
99 | * Disable back button. |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function disableBackButton() |
||
109 | |||
110 | /** |
||
111 | * Disable list button. |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function disableListButton() |
||
121 | |||
122 | /** |
||
123 | * Disasble save button. |
||
124 | * an extra save button at the top (made specially for long forms) |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function disableSaveButton() |
||
133 | |||
134 | /** |
||
135 | * Render header tools bar. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function render() |
||
165 | } |
||
166 |
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.