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 |
||
25 | class ToolbarHelper |
||
26 | { |
||
27 | |||
28 | const ACTION_DELETE = 'delete'; |
||
29 | const ACTION_SAVE = 'save'; |
||
30 | const ACTION_APPLY = 'apply'; |
||
31 | |||
32 | /** |
||
33 | * Toolbar instance name. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected static $_toolbar = Toolbar::DEFAULT_NAME; |
||
38 | |||
39 | /** |
||
40 | * Create add link. |
||
41 | * |
||
42 | * @param string|null $title |
||
43 | * @param array|string $url |
||
44 | * @param string $icon |
||
45 | * @param array $options |
||
46 | * @return void |
||
47 | */ |
||
48 | View Code Duplication | public static function add($title = null, $url = ['action' => 'add'], $icon = 'plus', array $options = []) |
|
58 | |||
59 | /** |
||
60 | * Apply form button. |
||
61 | * |
||
62 | * @param null|string $title |
||
63 | * @return void |
||
64 | */ |
||
65 | View Code Duplication | public static function apply($title = null) |
|
76 | |||
77 | /** |
||
78 | * Cancel form button. |
||
79 | * |
||
80 | * @param string|null $title |
||
81 | * @param null|string|array $url |
||
82 | * @param array $options |
||
83 | * @return void |
||
84 | */ |
||
85 | public static function cancel($title = null, $url = null, array $options = []) |
||
102 | |||
103 | /** |
||
104 | * Delete for process form. |
||
105 | * |
||
106 | * @param string|null $title |
||
107 | * @return void |
||
108 | */ |
||
109 | public static function delete($title = null) |
||
116 | |||
117 | /** |
||
118 | * Create link output. |
||
119 | * |
||
120 | * @param string $title |
||
121 | * @param string|array $url |
||
122 | * @param array $options |
||
123 | */ |
||
124 | View Code Duplication | public static function link($title, $url, array $options = []) |
|
134 | |||
135 | /** |
||
136 | * Save form button. |
||
137 | * |
||
138 | * @param null|string $title |
||
139 | * @return void |
||
140 | */ |
||
141 | View Code Duplication | public static function save($title = null) |
|
153 | |||
154 | /** |
||
155 | * Setup toolbar instance name. |
||
156 | * |
||
157 | * @param string $name |
||
158 | * @return void |
||
159 | */ |
||
160 | public static function setToolbar($name) |
||
164 | } |
||
165 |
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.