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 |
||
| 8 | class FieldsBuilder extends ParentDelegationBuilder implements NamedBuilder |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Field Group Configuration |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $config = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Manages the Field Configurations |
||
| 18 | * @var FieldManager |
||
| 19 | */ |
||
| 20 | protected $fieldManager; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Location configuration for Field Group |
||
| 24 | * @var LocationBuilder |
||
| 25 | */ |
||
| 26 | protected $location = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Field Group Name |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $name; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $name Field Group name |
||
| 36 | * @param array $groupConfig Field Group configuration |
||
| 37 | */ |
||
| 38 | public function __construct($name, $groupConfig = []) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Set a value for a particular key in the group config |
||
| 50 | * @param string $key |
||
| 51 | * @param mixed $value |
||
| 52 | */ |
||
| 53 | public function setGroupConfig($key, $value) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getName() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Namespace a group key |
||
| 70 | * Append the namespace 'group' before the set key. |
||
| 71 | * |
||
| 72 | * @param string $key Field Key |
||
| 73 | * @return string Field Key |
||
| 74 | */ |
||
| 75 | private function namespaceGroupKey($key) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Build the final config array. Build any other builders that may exist |
||
| 85 | * in the config. |
||
| 86 | * @return array Final field config |
||
| 87 | */ |
||
| 88 | public function build() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return a fields config array |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | private function buildFields() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Apply field transforms |
||
| 112 | * @param array $fields |
||
| 113 | * @return array Transformed fields config |
||
| 114 | */ |
||
| 115 | private function transformFields($fields) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return a locations config array |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | private function buildLocation() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Add multiple fields either via an array or from another builder |
||
| 138 | * @param mixed $fields array of fields or a FieldBuilder |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function addFields($fields) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Add a field of a specific type |
||
| 157 | * @param string $name |
||
| 158 | * @param string $type |
||
| 159 | * @param array $args field configuration |
||
| 160 | * @throws FieldNameCollisionException if name already exists. |
||
| 161 | * @return FieldBuilder |
||
| 162 | */ |
||
| 163 | public function addField($name, $type, $args = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add a field of a choice type, allows choices to be added. |
||
| 170 | * @param string $name |
||
| 171 | * @param string $type 'select', 'radio', 'checkbox' |
||
| 172 | * @param array $args field configuration |
||
| 173 | * @throws FieldNameCollisionException if name already exists. |
||
| 174 | * @return FieldBuilder |
||
| 175 | */ |
||
| 176 | public function addChoiceField($name, $type, $args = []) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Initialize the FieldBuilder, add to FieldManager |
||
| 183 | * @param FieldBuilder $field |
||
| 184 | * @return FieldBuilder |
||
| 185 | */ |
||
| 186 | protected function initializeField($field) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $name |
||
| 195 | * @param array $args field configuration |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function addText($name, $args = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $name |
||
| 205 | * @param array $args field configuration |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function addTextarea($name, $args = []) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $name |
||
| 215 | * @param array $args field configuration |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function addNumber($name, $args = []) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $name |
||
| 225 | * @param array $args field configuration |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function addEmail($name, $args = []) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $name |
||
| 235 | * @param array $args field configuration |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function addUrl($name, $args = []) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $name |
||
| 245 | * @param array $args field configuration |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function addPassword($name, $args = []) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $name |
||
| 255 | * @param array $args field configuration |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function addWysiwyg($name, $args = []) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $name |
||
| 265 | * @param array $args field configuration |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function addOembed($name, $args = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $name |
||
| 275 | * @param array $args field configuration |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function addImage($name, $args = []) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $name |
||
| 285 | * @param array $args field configuration |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function addFile($name, $args = []) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $name |
||
| 295 | * @param array $args field configuration |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function addGallery($name, $args = []) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $name |
||
| 305 | * @param array $args field configuration |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function addTrueFalse($name, $args = []) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $name |
||
| 315 | * @param array $args field configuration |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function addSelect($name, $args = []) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $name |
||
| 325 | * @param array $args field configuration |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function addRadio($name, $args = []) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $name |
||
| 335 | * @param array $args field configuration |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function addCheckbox($name, $args = []) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $name |
||
| 345 | * @param array $args field configuration |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function addPostObject($name, $args = []) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $name |
||
| 355 | * @param array $args field configuration |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function addPostLink($name, $args = []) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $name |
||
| 365 | * @param array $args field configuration |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function addRelationship($name, $args = []) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $name |
||
| 375 | * @param array $args field configuration |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function addTaxonomy($name, $args = []) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $name |
||
| 385 | * @param array $args field configuration |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | public function addUser($name, $args = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $name |
||
| 395 | * @param array $args field configuration |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function addDatePicker($name, $args = []) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $name |
||
| 405 | * @param array $args field configuration |
||
| 406 | * @return $this |
||
| 407 | */ |
||
| 408 | public function addTimePicker($name, $args = []) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $name |
||
| 415 | * @param array $args field configuration |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function addDateTimePicker($name, $args = []) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param string $name |
||
| 425 | * @param array $args field configuration |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function addColorPicker($name, $args = []) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * All fields added after will appear under this tab, until another tab |
||
| 435 | * is added. |
||
| 436 | * @param string $label Tab label |
||
| 437 | * @param array $args field configuration |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function addTab($label, $args = []) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Addes a message field |
||
| 447 | * @param string $label |
||
| 448 | * @param string $message |
||
| 449 | * @param array $args field configuration |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function addMessage($label, $message, $args = []) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Add a repeater field. Any fields added after will be added to the repeater |
||
| 465 | * until `endRepeater` is called. |
||
| 466 | * @param string $name |
||
| 467 | * @param array $args field configuration |
||
| 468 | * @return RepeaterBuilder |
||
| 469 | */ |
||
| 470 | public function addRepeater($name, $args = []) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Add a flexible content field. Once adding a layout with `addLayout`, |
||
| 477 | * any fields added after will be added to that layout until another |
||
| 478 | * `addLayout` call is made, or until `endFlexibleContent` is called. |
||
| 479 | * @param string $name |
||
| 480 | * @param array $args field configuration |
||
| 481 | * @return FlexibleContentBuilder |
||
| 482 | */ |
||
| 483 | public function addFlexibleContent($name, $args = []) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return FieldManager |
||
| 490 | */ |
||
| 491 | protected function getFieldManager() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | public function getFields() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $name [description] |
||
| 506 | * @return FieldBuilder |
||
| 507 | */ |
||
| 508 | public function getField($name) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Modify an already defined field |
||
| 515 | * @param string $name Name of the field |
||
| 516 | * @param array|\Closure $modify Array of field configs or a closure that accepts |
||
| 517 | * a FieldsBuilder and returns a FieldsBuilder. |
||
| 518 | * @throws ModifyFieldReturnTypeException if $modify is a closure and doesn't |
||
| 519 | * return a FieldsBuilder. |
||
| 520 | * @throws FieldNotFoundException if the field name doesn't exist. |
||
| 521 | * @return $this |
||
| 522 | */ |
||
| 523 | public function modifyField($name, $modify) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Remove a field by name |
||
| 556 | * @param string $name Field to remove |
||
| 557 | * @return $this |
||
| 558 | */ |
||
| 559 | public function removeField($name) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Set the location of the field group. See |
||
| 568 | * https://github.com/StoutLogic/acf-builder/wiki/location and |
||
| 569 | * https://www.advancedcustomfields.com/resources/custom-location-rules/ |
||
| 570 | * for more details. |
||
| 571 | * @param string $param |
||
| 572 | * @param string $operator |
||
| 573 | * @param string $value |
||
| 574 | * @return LocationBuilder |
||
| 575 | */ |
||
| 576 | public function setLocation($param, $operator, $value) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @return LocationBuilder |
||
| 590 | */ |
||
| 591 | public function getLocation() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Create a field label based on the field's name. Generates title case. |
||
| 598 | * @param string $name |
||
| 599 | * @return string label |
||
| 600 | */ |
||
| 601 | protected function generateLabel($name) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Generates a snaked cased name. |
||
| 608 | * @param string $name |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | protected function generateName($name) |
||
| 615 | } |
||
| 616 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: