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 |
||
49 | class Form implements Renderable |
||
50 | { |
||
51 | /** |
||
52 | * @var Field[] |
||
53 | */ |
||
54 | protected $fields = []; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $data = []; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $attributes = []; |
||
65 | |||
66 | /** |
||
67 | * Available buttons. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $buttons = ['reset', 'submit']; |
||
72 | |||
73 | /** |
||
74 | * Form constructor. |
||
75 | * |
||
76 | * @param array $data |
||
77 | */ |
||
78 | public function __construct($data = []) |
||
90 | |||
91 | /** |
||
92 | * Initialize the form attributes. |
||
93 | */ |
||
94 | protected function initFormAttributes() |
||
104 | |||
105 | /** |
||
106 | * Action uri of the form. |
||
107 | * |
||
108 | * @param string $action |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function action($action) |
||
116 | |||
117 | /** |
||
118 | * Method of the form. |
||
119 | * |
||
120 | * @param string $method |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function method($method = 'POST') |
||
128 | |||
129 | /** |
||
130 | * Add form attributes. |
||
131 | * |
||
132 | * @param string|array $attr |
||
133 | * @param string $value |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function attribute($attr, $value = '') |
||
149 | |||
150 | /** |
||
151 | * Disable Pjax. |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function disablePjax() |
||
161 | |||
162 | /** |
||
163 | * Disable reset button. |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function disableReset() |
||
173 | |||
174 | /** |
||
175 | * Disable submit button. |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function disableSubmit() |
||
185 | |||
186 | /** |
||
187 | * Set field and label width in current form. |
||
188 | * |
||
189 | * @param int $fieldWidth |
||
190 | * @param int $labelWidth |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
203 | |||
204 | /** |
||
205 | * Find field class with given name. |
||
206 | * |
||
207 | * @param string $method |
||
208 | * |
||
209 | * @return bool|string |
||
210 | */ |
||
211 | View Code Duplication | public static function findFieldClass($method) |
|
221 | |||
222 | /** |
||
223 | * Add a form field to form. |
||
224 | * |
||
225 | * @param Field $field |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | protected function pushField(Field &$field) |
||
235 | |||
236 | /** |
||
237 | * Get variables for render form. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function getVariables() |
||
254 | |||
255 | /** |
||
256 | * Format form attributes form array to html. |
||
257 | * |
||
258 | * @param array $attributes |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public function formatAttribute($attributes = []) |
||
277 | |||
278 | /** |
||
279 | * Determine if form fields has files. |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function hasFile() |
||
293 | |||
294 | /** |
||
295 | * Generate a Field object and add to form builder if Field exists. |
||
296 | * |
||
297 | * @param string $method |
||
298 | * @param array $arguments |
||
299 | * |
||
300 | * @return Field|null |
||
301 | */ |
||
302 | public function __call($method, $arguments) |
||
314 | |||
315 | /** |
||
316 | * Render the form. |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function render() |
||
324 | |||
325 | /** |
||
326 | * Output as string. |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | public function __toString() |
||
334 | } |
||
335 |
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.