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 FieldsBuilder 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 FieldsBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class FieldsBuilder extends Builder |
||
| 6 | { |
||
| 7 | protected $config = []; |
||
| 8 | protected $fields = []; |
||
| 9 | protected $location = null; |
||
| 10 | protected $name; |
||
| 11 | |||
| 12 | public function __construct($name, $groupConfig = []) |
||
| 20 | |||
| 21 | public function setGroupConfig($key, $value) |
||
| 27 | |||
| 28 | public function getName() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Build the final config array. Build any other builders that may exist |
||
| 35 | * in the config. |
||
| 36 | * @return array final field config |
||
| 37 | */ |
||
| 38 | public function build() |
||
| 39 | { |
||
| 40 | $fields = $this->getFields(); |
||
| 41 | |||
| 42 | $fields = $this->buildFields($fields); |
||
| 43 | |||
| 44 | array_walk_recursive($fields, function(&$value, $key) { |
||
| 45 | |||
| 46 | switch ($key) { |
||
| 47 | case 'conditional_logic': |
||
| 48 | $value = $value->build(); |
||
| 49 | $value = $this->transformConditionalConfig($value); |
||
| 50 | break; |
||
| 51 | } |
||
| 52 | }); |
||
| 53 | |||
| 54 | $location = $this->getLocation(); |
||
| 55 | if (is_subclass_of($location, Builder::class)) { |
||
|
|
|||
| 56 | $location = $location->build(); |
||
| 57 | } |
||
| 58 | |||
| 59 | return array_merge($this->config, [ |
||
| 60 | 'fields' => $fields, |
||
| 61 | 'location' => $location, |
||
| 62 | ]); |
||
| 63 | } |
||
| 64 | |||
| 65 | private function buildFields($fields) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Replace field values with the field's respective key |
||
| 83 | * @param array $config |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | protected function transformConditionalConfig($config) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Add multiple fields either via an array or from another builder |
||
| 106 | * @param mixed $fields array of fields or a FieldBuilder |
||
| 107 | */ |
||
| 108 | public function addFields($fields) |
||
| 124 | |||
| 125 | public function addField($name, $args = []) |
||
| 137 | |||
| 138 | protected function addFieldType($name, $type, $args = []) |
||
| 144 | |||
| 145 | public function addText($name, $args = []) |
||
| 149 | |||
| 150 | public function addTextarea($name, $args = []) |
||
| 154 | |||
| 155 | public function addNumber($name, $args = []) |
||
| 159 | |||
| 160 | public function addEmail($name, $args = []) |
||
| 164 | |||
| 165 | public function addUrl($name, $args = []) |
||
| 169 | |||
| 170 | public function addPassword($name, $args = []) |
||
| 174 | |||
| 175 | public function addWysiwyg($name, $args = []) |
||
| 179 | |||
| 180 | public function addOembed($name, $args = []) |
||
| 184 | |||
| 185 | public function addImage($name, $args = []) |
||
| 189 | |||
| 190 | public function addFile($name, $args = []) |
||
| 194 | |||
| 195 | public function addGallery($name, $args = []) |
||
| 199 | |||
| 200 | public function addTrueFalse($name, $args = []) |
||
| 204 | |||
| 205 | public function addSelect($name, $args = []) |
||
| 209 | |||
| 210 | public function addRadio($name, $args = []) |
||
| 214 | |||
| 215 | public function addCheckbox($name, $args = []) |
||
| 219 | |||
| 220 | public function addPostObject($name, $args = []) |
||
| 224 | |||
| 225 | public function addPostLink($name, $args = []) |
||
| 229 | |||
| 230 | public function addRelationship($name, $args = []) |
||
| 234 | |||
| 235 | public function addTaxonomy($name, $args = []) |
||
| 239 | |||
| 240 | public function addUser($name, $args = []) |
||
| 244 | |||
| 245 | public function addDatePicker($name, $args = []) |
||
| 249 | |||
| 250 | public function addTimePicker($name, $args = []) |
||
| 254 | |||
| 255 | public function addDateTimePicker($name, $args = []) |
||
| 259 | |||
| 260 | public function addColorPicker($name, $args = []) |
||
| 264 | |||
| 265 | View Code Duplication | public function addTab($label, $args = []) |
|
| 274 | |||
| 275 | public function endpoint($value = 1) |
||
| 279 | |||
| 280 | View Code Duplication | public function addMessage($label, $message, $args = []) |
|
| 290 | |||
| 291 | public function addRepeater($name, $args = []) |
||
| 299 | |||
| 300 | public function addFlexibleContent($name, $args = []) |
||
| 308 | |||
| 309 | public function addChoice($choice, $label = null) |
||
| 321 | |||
| 322 | public function addChoices() |
||
| 335 | |||
| 336 | public function conditional($name, $operator, $value) |
||
| 347 | |||
| 348 | public function getFields() |
||
| 352 | |||
| 353 | protected function getFieldByName($name) |
||
| 363 | |||
| 364 | public function defaultValue($value) |
||
| 368 | |||
| 369 | public function required($value = true) |
||
| 373 | |||
| 374 | public function instructions($value) |
||
| 378 | |||
| 379 | public function setConfig($key, $value) |
||
| 387 | |||
| 388 | public function setLocation($param, $operator, $value) |
||
| 399 | |||
| 400 | public function getLocation() |
||
| 404 | |||
| 405 | protected function popLastField() |
||
| 409 | |||
| 410 | protected function pushField($field) |
||
| 414 | |||
| 415 | protected function generateLabel($name) |
||
| 419 | |||
| 420 | protected function generateName($name) |
||
| 424 | } |
||
| 425 |