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:
Complex classes like NestedForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NestedForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class NestedForm |
||
| 55 | { |
||
| 56 | const DEFAULT_KEY_NAME = '__LA_KEY__'; |
||
| 57 | |||
| 58 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 59 | |||
| 60 | const REMOVE_FLAG_CLASS = 'fom-removed'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var mixed |
||
| 64 | */ |
||
| 65 | protected $key; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $relationName; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * NestedForm key. |
||
| 74 | * |
||
| 75 | * @var Model |
||
| 76 | */ |
||
| 77 | protected $model; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Fields in form. |
||
| 81 | * |
||
| 82 | * @var Collection |
||
| 83 | */ |
||
| 84 | protected $fields; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Original data for this field. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $original = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var \Encore\Admin\Form |
||
| 95 | */ |
||
| 96 | protected $form; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Create a new NestedForm instance. |
||
| 100 | * |
||
| 101 | * NestedForm constructor. |
||
| 102 | * |
||
| 103 | * @param string $relation |
||
| 104 | * @param Model $model |
||
| 105 | */ |
||
| 106 | public function __construct($relation, $model = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get current model. |
||
| 117 | * |
||
| 118 | * @return Model|null |
||
| 119 | */ |
||
| 120 | public function model() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the value of the model's primary key. |
||
| 127 | * |
||
| 128 | * @return mixed|null |
||
| 129 | */ |
||
| 130 | public function getKey() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set key for current form. |
||
| 149 | * |
||
| 150 | * @param mixed $key |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function setKey($key) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set Form. |
||
| 162 | * |
||
| 163 | * @param Form $form |
||
| 164 | * |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function setForm(Form $form = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get form. |
||
| 176 | * |
||
| 177 | * @return Form |
||
| 178 | */ |
||
| 179 | public function getForm() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set original values for fields. |
||
| 186 | * |
||
| 187 | * @param array $data |
||
| 188 | * @param string $relatedKeyName |
||
| 189 | * |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function setOriginal($data, $relatedKeyName = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Prepare for insert or update. |
||
| 214 | * |
||
| 215 | * @param array $input |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | public function prepare($input) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set original data for each field. |
||
| 231 | * |
||
| 232 | * @param string $key |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | View Code Duplication | protected function setFieldOriginalValue($key) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Do prepare work before store and update. |
||
| 250 | * |
||
| 251 | * @param array $record |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | protected function prepareRecord($record) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Fetch value in input data by column name. |
||
| 295 | * |
||
| 296 | * @param array $data |
||
| 297 | * @param string|array $columns |
||
| 298 | * |
||
| 299 | * @return array|mixed |
||
| 300 | */ |
||
| 301 | View Code Duplication | protected function fetchColumnValue($data, $columns) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param Field $field |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function pushField(Field $field) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get fields of this form. |
||
| 334 | * |
||
| 335 | * @return Collection |
||
| 336 | */ |
||
| 337 | public function fields() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Fill data to all fields in form. |
||
| 344 | * |
||
| 345 | * @param array $data |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function fill(array $data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the html and script of template. |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function getTemplateHtmlAndScript() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set `errorKey` `elementName` `elementClass` for fields inside hasmany fields. |
||
| 388 | * |
||
| 389 | * @param Field $field |
||
| 390 | * |
||
| 391 | * @return Field |
||
| 392 | */ |
||
| 393 | protected function formatField(Field $field) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Add nested-form fields dynamically. |
||
| 420 | * |
||
| 421 | * @param string $method |
||
| 422 | * @param array $arguments |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function __call($method, $arguments) |
|
| 445 | } |
||
| 446 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: