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 |
||
| 52 | class NestedForm |
||
| 53 | { |
||
| 54 | const DEFAULT_KEY_NAME = '__LA_KEY__'; |
||
| 55 | |||
| 56 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 57 | |||
| 58 | const REMOVE_FLAG_CLASS = 'fom-removed'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $relationName; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * NestedForm key. |
||
| 67 | * |
||
| 68 | * @var |
||
| 69 | */ |
||
| 70 | protected $key; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fields in form. |
||
| 74 | * |
||
| 75 | * @var Collection |
||
| 76 | */ |
||
| 77 | protected $fields; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Original data for this field. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $original = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var \Encore\Admin\Form |
||
| 88 | */ |
||
| 89 | protected $form; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create a new NestedForm instance. |
||
| 93 | * |
||
| 94 | * NestedForm constructor. |
||
| 95 | * |
||
| 96 | * @param string $relation |
||
| 97 | * @param null $key |
||
| 98 | */ |
||
| 99 | public function __construct($relation, $key = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set Form. |
||
| 110 | * |
||
| 111 | * @param Form $form |
||
| 112 | * |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setForm(Form $form = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get form. |
||
| 124 | * |
||
| 125 | * @return Form |
||
| 126 | */ |
||
| 127 | public function getForm() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set original values for fields. |
||
| 134 | * |
||
| 135 | * @param array $data |
||
| 136 | * @param string $relatedKeyName |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setOriginal($data, $relatedKeyName) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Prepare for insert or update. |
||
| 158 | * |
||
| 159 | * @param array $input |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function prepare($input) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set original data for each field. |
||
| 175 | * |
||
| 176 | * @param string $key |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | View Code Duplication | protected function setFieldOriginalValue($key) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Do prepare work before store and update. |
||
| 193 | * |
||
| 194 | * @param array $record |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | protected function prepareRecord($record) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Fetch value in input data by column name. |
||
| 238 | * |
||
| 239 | * @param array $data |
||
| 240 | * @param string|array $columns |
||
| 241 | * |
||
| 242 | * @return array|mixed |
||
| 243 | */ |
||
| 244 | View Code Duplication | protected function fetchColumnValue($data, $columns) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param Field $field |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function pushField(Field $field) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get fields of this form. |
||
| 277 | * |
||
| 278 | * @return Collection |
||
| 279 | */ |
||
| 280 | public function fields() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Fill data to all fields in form. |
||
| 287 | * |
||
| 288 | * @param array $data |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function fill(array $data) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the html and script of template. |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function getTemplateHtmlAndScript() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set `errorKey` `elementName` `elementClass` for fields inside hasmany fields. |
||
| 331 | * |
||
| 332 | * @param Field $field |
||
| 333 | * |
||
| 334 | * @return Field |
||
| 335 | */ |
||
| 336 | protected function formatField(Field $field) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Add nested-form fields dynamically. |
||
| 363 | * |
||
| 364 | * @param string $method |
||
| 365 | * @param array $arguments |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public function __call($method, $arguments) |
||
| 388 | } |
||
| 389 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.