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;  | 
            ||
| 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, array $groupConfig = [])  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Set a value for a particular key in the group config  | 
            ||
| 50 | * @param string $key  | 
            ||
| 51 | * @param mixed $value  | 
            ||
| 52 | * @return $this  | 
            ||
| 53 | */  | 
            ||
| 54 | public function setGroupConfig($key, $value)  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * Get a value for a particular key in the group config.  | 
            ||
| 63 | * Returns null if the key isn't defined in the config.  | 
            ||
| 64 | * @param string $key  | 
            ||
| 65 | * @return mixed|null  | 
            ||
| 66 | */  | 
            ||
| 67 | public function getGroupConfig($key)  | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * @return string  | 
            ||
| 78 | */  | 
            ||
| 79 | public function getName()  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Namespace a group key  | 
            ||
| 86 | * Append the namespace 'group' before the set key.  | 
            ||
| 87 | *  | 
            ||
| 88 | * @param string $key Field Key  | 
            ||
| 89 | * @return string Field Key  | 
            ||
| 90 | */  | 
            ||
| 91 | private function namespaceGroupKey($key)  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Build the final config array. Build any other builders that may exist  | 
            ||
| 101 | * in the config.  | 
            ||
| 102 | * @return array Final field config  | 
            ||
| 103 | */  | 
            ||
| 104 | public function build()  | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * Return a fields config array  | 
            ||
| 115 | * @return array  | 
            ||
| 116 | */  | 
            ||
| 117 | private function buildFields()  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Apply field transforms  | 
            ||
| 128 | * @param array $fields  | 
            ||
| 129 | * @return array Transformed fields config  | 
            ||
| 130 | */  | 
            ||
| 131 | private function transformFields($fields)  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Return a locations config array  | 
            ||
| 144 | * @return array|LocationBuilder  | 
            ||
| 145 | */  | 
            ||
| 146 | private function buildLocation()  | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Add multiple fields either via an array or from another builder  | 
            ||
| 154 | * @param FieldsBuilder|array $fields  | 
            ||
| 155 | * @return $this  | 
            ||
| 156 | */  | 
            ||
| 157 | public function addFields($fields)  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * Add a field of a specific type  | 
            ||
| 173 | * @param string $name  | 
            ||
| 174 | * @param string $type  | 
            ||
| 175 | * @param array $args field configuration  | 
            ||
| 176 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 177 | * @return FieldBuilder  | 
            ||
| 178 | */  | 
            ||
| 179 | public function addField($name, $type, array $args = [])  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * Add a field of a choice type, allows choices to be added.  | 
            ||
| 186 | * @param string $name  | 
            ||
| 187 | * @param string $type 'select', 'radio', 'checkbox'  | 
            ||
| 188 | * @param array $args field configuration  | 
            ||
| 189 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 190 | * @return FieldBuilder  | 
            ||
| 191 | */  | 
            ||
| 192 | public function addChoiceField($name, $type, array $args = [])  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Initialize the FieldBuilder, add to FieldManager  | 
            ||
| 199 | * @param FieldBuilder $field  | 
            ||
| 200 | * @return FieldBuilder  | 
            ||
| 201 | */  | 
            ||
| 202 | protected function initializeField($field)  | 
            ||
| 208 | |||
| 209 | /**  | 
            ||
| 210 | * @param string $name  | 
            ||
| 211 | * @param array $args field configuration  | 
            ||
| 212 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 213 | * @return FieldBuilder  | 
            ||
| 214 | */  | 
            ||
| 215 | public function addText($name, array $args = [])  | 
            ||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * @param string $name  | 
            ||
| 222 | * @param array $args field configuration  | 
            ||
| 223 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 224 | * @return FieldBuilder  | 
            ||
| 225 | */  | 
            ||
| 226 | public function addTextarea($name, array $args = [])  | 
            ||
| 230 | |||
| 231 | /**  | 
            ||
| 232 | * @param string $name  | 
            ||
| 233 | * @param array $args field configuration  | 
            ||
| 234 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 235 | * @return FieldBuilder  | 
            ||
| 236 | */  | 
            ||
| 237 | public function addNumber($name, array $args = [])  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * @param string $name  | 
            ||
| 244 | * @param array $args field configuration  | 
            ||
| 245 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 246 | * @return FieldBuilder  | 
            ||
| 247 | */  | 
            ||
| 248 | public function addEmail($name, array $args = [])  | 
            ||
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * @param string $name  | 
            ||
| 255 | * @param array $args field configuration  | 
            ||
| 256 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 257 | * @return FieldBuilder  | 
            ||
| 258 | */  | 
            ||
| 259 | public function addUrl($name, array $args = [])  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * @param string $name  | 
            ||
| 266 | * @param array $args field configuration  | 
            ||
| 267 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 268 | * @return FieldBuilder  | 
            ||
| 269 | */  | 
            ||
| 270 | public function addPassword($name, array $args = [])  | 
            ||
| 274 | |||
| 275 | /**  | 
            ||
| 276 | * @param string $name  | 
            ||
| 277 | * @param array $args field configuration  | 
            ||
| 278 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 279 | * @return FieldBuilder  | 
            ||
| 280 | */  | 
            ||
| 281 | public function addWysiwyg($name, array $args = [])  | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * @param string $name  | 
            ||
| 288 | * @param array $args field configuration  | 
            ||
| 289 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 290 | * @return FieldBuilder  | 
            ||
| 291 | */  | 
            ||
| 292 | public function addOembed($name, array $args = [])  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * @param string $name  | 
            ||
| 299 | * @param array $args field configuration  | 
            ||
| 300 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 301 | * @return FieldBuilder  | 
            ||
| 302 | */  | 
            ||
| 303 | public function addImage($name, array $args = [])  | 
            ||
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * @param string $name  | 
            ||
| 310 | * @param array $args field configuration  | 
            ||
| 311 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 312 | * @return FieldBuilder  | 
            ||
| 313 | */  | 
            ||
| 314 | public function addFile($name, array $args = [])  | 
            ||
| 318 | |||
| 319 | /**  | 
            ||
| 320 | * @param string $name  | 
            ||
| 321 | * @param array $args field configuration  | 
            ||
| 322 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 323 | * @return FieldBuilder  | 
            ||
| 324 | */  | 
            ||
| 325 | public function addGallery($name, array $args = [])  | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 | * @param string $name  | 
            ||
| 332 | * @param array $args field configuration  | 
            ||
| 333 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 334 | * @return FieldBuilder  | 
            ||
| 335 | */  | 
            ||
| 336 | public function addTrueFalse($name, array $args = [])  | 
            ||
| 340 | |||
| 341 | /**  | 
            ||
| 342 | * @param string $name  | 
            ||
| 343 | * @param array $args field configuration  | 
            ||
| 344 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 345 | * @return FieldBuilder  | 
            ||
| 346 | */  | 
            ||
| 347 | public function addSelect($name, array $args = [])  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * @param string $name  | 
            ||
| 354 | * @param array $args field configuration  | 
            ||
| 355 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 356 | * @return FieldBuilder  | 
            ||
| 357 | */  | 
            ||
| 358 | public function addRadio($name, array $args = [])  | 
            ||
| 362 | |||
| 363 | /**  | 
            ||
| 364 | * @param string $name  | 
            ||
| 365 | * @param array $args field configuration  | 
            ||
| 366 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 367 | * @return FieldBuilder  | 
            ||
| 368 | */  | 
            ||
| 369 | public function addCheckbox($name, array $args = [])  | 
            ||
| 373 | |||
| 374 | /**  | 
            ||
| 375 | * @param string $name  | 
            ||
| 376 | * @param array $args field configuration  | 
            ||
| 377 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 378 | * @return FieldBuilder  | 
            ||
| 379 | */  | 
            ||
| 380 | public function addPostObject($name, array $args = [])  | 
            ||
| 384 | |||
| 385 | /**  | 
            ||
| 386 | * @param string $name  | 
            ||
| 387 | * @param array $args field configuration  | 
            ||
| 388 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 389 | * @return FieldBuilder  | 
            ||
| 390 | */  | 
            ||
| 391 | public function addPageLink($name, array $args = [])  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * @param string $name  | 
            ||
| 398 | * @param array $args field configuration  | 
            ||
| 399 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 400 | * @return FieldBuilder  | 
            ||
| 401 | */  | 
            ||
| 402 | public function addRelationship($name, array $args = [])  | 
            ||
| 406 | |||
| 407 | /**  | 
            ||
| 408 | * @param string $name  | 
            ||
| 409 | * @param array $args field configuration  | 
            ||
| 410 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 411 | * @return FieldBuilder  | 
            ||
| 412 | */  | 
            ||
| 413 | public function addTaxonomy($name, array $args = [])  | 
            ||
| 417 | |||
| 418 | /**  | 
            ||
| 419 | * @param string $name  | 
            ||
| 420 | * @param array $args field configuration  | 
            ||
| 421 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 422 | * @return FieldBuilder  | 
            ||
| 423 | */  | 
            ||
| 424 | public function addUser($name, array $args = [])  | 
            ||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * @param string $name  | 
            ||
| 431 | * @param array $args field configuration  | 
            ||
| 432 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 433 | * @return FieldBuilder  | 
            ||
| 434 | */  | 
            ||
| 435 | public function addDatePicker($name, array $args = [])  | 
            ||
| 439 | |||
| 440 | /**  | 
            ||
| 441 | * @param string $name  | 
            ||
| 442 | * @param array $args field configuration  | 
            ||
| 443 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 444 | * @return FieldBuilder  | 
            ||
| 445 | */  | 
            ||
| 446 | public function addTimePicker($name, array $args = [])  | 
            ||
| 450 | |||
| 451 | /**  | 
            ||
| 452 | * @param string $name  | 
            ||
| 453 | * @param array $args field configuration  | 
            ||
| 454 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 455 | * @return FieldBuilder  | 
            ||
| 456 | */  | 
            ||
| 457 | public function addDateTimePicker($name, array $args = [])  | 
            ||
| 461 | |||
| 462 | /**  | 
            ||
| 463 | * @param string $name  | 
            ||
| 464 | * @param array $args field configuration  | 
            ||
| 465 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 466 | * @return FieldBuilder  | 
            ||
| 467 | */  | 
            ||
| 468 | public function addColorPicker($name, array $args = [])  | 
            ||
| 472 | |||
| 473 | /**  | 
            ||
| 474 | * @param string $name  | 
            ||
| 475 | * @param array $args field configuration  | 
            ||
| 476 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 477 | * @return FieldBuilder  | 
            ||
| 478 | */  | 
            ||
| 479 | public function addGoogleMap($name, array $args = [])  | 
            ||
| 483 | |||
| 484 | /**  | 
            ||
| 485 | * All fields added after will appear under this tab, until another tab  | 
            ||
| 486 | * is added.  | 
            ||
| 487 | * @param string $label Tab label  | 
            ||
| 488 | * @param array $args field configuration  | 
            ||
| 489 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 490 | * @return FieldBuilder  | 
            ||
| 491 | */  | 
            ||
| 492 | public function addTab($label, array $args = [])  | 
            ||
| 496 | |||
| 497 | /**  | 
            ||
| 498 | * Addes a message field  | 
            ||
| 499 | * @param string $label  | 
            ||
| 500 | * @param string $message  | 
            ||
| 501 | * @param array $args field configuration  | 
            ||
| 502 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 503 | * @return FieldBuilder  | 
            ||
| 504 | */  | 
            ||
| 505 | public function addMessage($label, $message, array $args = [])  | 
            ||
| 515 | |||
| 516 | /**  | 
            ||
| 517 | * Add a repeater field. Any fields added after will be added to the repeater  | 
            ||
| 518 | * until `endRepeater` is called.  | 
            ||
| 519 | * @param string $name  | 
            ||
| 520 | * @param array $args field configuration  | 
            ||
| 521 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 522 | * @return RepeaterBuilder  | 
            ||
| 523 | */  | 
            ||
| 524 | public function addRepeater($name, array $args = [])  | 
            ||
| 528 | |||
| 529 | /**  | 
            ||
| 530 | * Add a flexible content field. Once adding a layout with `addLayout`,  | 
            ||
| 531 | * any fields added after will be added to that layout until another  | 
            ||
| 532 | * `addLayout` call is made, or until `endFlexibleContent` is called.  | 
            ||
| 533 | * @param string $name  | 
            ||
| 534 | * @param array $args field configuration  | 
            ||
| 535 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 536 | * @return FlexibleContentBuilder  | 
            ||
| 537 | */  | 
            ||
| 538 | public function addFlexibleContent($name, array $args = [])  | 
            ||
| 542 | |||
| 543 | /**  | 
            ||
| 544 | * @return FieldManager  | 
            ||
| 545 | */  | 
            ||
| 546 | protected function getFieldManager()  | 
            ||
| 550 | |||
| 551 | /**  | 
            ||
| 552 | * @return array  | 
            ||
| 553 | */  | 
            ||
| 554 | public function getFields()  | 
            ||
| 558 | |||
| 559 | /**  | 
            ||
| 560 | * @param string $name [description]  | 
            ||
| 561 | * @return FieldBuilder  | 
            ||
| 562 | */  | 
            ||
| 563 | public function getField($name)  | 
            ||
| 567 | |||
| 568 | public function fieldExists($name)  | 
            ||
| 572 | |||
| 573 | /**  | 
            ||
| 574 | * Modify an already defined field  | 
            ||
| 575 | * @param string $name Name of the field  | 
            ||
| 576 | * @param array|\Closure $modify Array of field configs or a closure that accepts  | 
            ||
| 577 | * a FieldsBuilder and returns a FieldsBuilder.  | 
            ||
| 578 | * @throws ModifyFieldReturnTypeException if $modify is a closure and doesn't  | 
            ||
| 579 | * return a FieldsBuilder.  | 
            ||
| 580 | * @throws FieldNotFoundException if the field name doesn't exist.  | 
            ||
| 581 | * @return $this  | 
            ||
| 582 | */  | 
            ||
| 583 | public function modifyField($name, $modify)  | 
            ||
| 613 | |||
| 614 | /**  | 
            ||
| 615 | * Remove a field by name  | 
            ||
| 616 | * @param string $name Field to remove  | 
            ||
| 617 | * @return $this  | 
            ||
| 618 | */  | 
            ||
| 619 | public function removeField($name)  | 
            ||
| 625 | |||
| 626 | /**  | 
            ||
| 627 | * Set the location of the field group. See  | 
            ||
| 628 | * https://github.com/StoutLogic/acf-builder/wiki/location and  | 
            ||
| 629 | * https://www.advancedcustomfields.com/resources/custom-location-rules/  | 
            ||
| 630 | * for more details.  | 
            ||
| 631 | * @param string $param  | 
            ||
| 632 | * @param string $operator  | 
            ||
| 633 | * @param string $value  | 
            ||
| 634 | * @return LocationBuilder  | 
            ||
| 635 | */  | 
            ||
| 636 | public function setLocation($param, $operator, $value)  | 
            ||
| 647 | |||
| 648 | /**  | 
            ||
| 649 | * @return LocationBuilder  | 
            ||
| 650 | */  | 
            ||
| 651 | public function getLocation()  | 
            ||
| 655 | |||
| 656 | /**  | 
            ||
| 657 | * Create a field label based on the field's name. Generates title case.  | 
            ||
| 658 | * @param string $name  | 
            ||
| 659 | * @return string label  | 
            ||
| 660 | */  | 
            ||
| 661 | protected function generateLabel($name)  | 
            ||
| 665 | |||
| 666 | /**  | 
            ||
| 667 | * Generates a snaked cased name.  | 
            ||
| 668 | * @param string $name  | 
            ||
| 669 | * @return string  | 
            ||
| 670 | */  | 
            ||
| 671 | protected function generateName($name)  | 
            ||
| 675 | }  | 
            ||
| 676 | 
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: