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 | * Get a value for a particular key in the group config.  | 
            ||
| 62 | * Returns null if the key isn't defined in the config.  | 
            ||
| 63 | * @param string $key  | 
            ||
| 64 | * @return mixed|null  | 
            ||
| 65 | */  | 
            ||
| 66 | public function getGroupConfig($key)  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * @return string  | 
            ||
| 77 | */  | 
            ||
| 78 | public function getName()  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * Namespace a group key  | 
            ||
| 85 | * Append the namespace 'group' before the set key.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @param string $key Field Key  | 
            ||
| 88 | * @return string Field Key  | 
            ||
| 89 | */  | 
            ||
| 90 | private function namespaceGroupKey($key)  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Build the final config array. Build any other builders that may exist  | 
            ||
| 100 | * in the config.  | 
            ||
| 101 | * @return array Final field config  | 
            ||
| 102 | */  | 
            ||
| 103 | public function build()  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * Return a fields config array  | 
            ||
| 114 | * @return array  | 
            ||
| 115 | */  | 
            ||
| 116 | private function buildFields()  | 
            ||
| 124 | |||
| 125 | /**  | 
            ||
| 126 | * Apply field transforms  | 
            ||
| 127 | * @param array $fields  | 
            ||
| 128 | * @return array Transformed fields config  | 
            ||
| 129 | */  | 
            ||
| 130 | private function transformFields($fields)  | 
            ||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Return a locations config array  | 
            ||
| 143 | * @return array  | 
            ||
| 144 | */  | 
            ||
| 145 | private function buildLocation()  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * Add multiple fields either via an array or from another builder  | 
            ||
| 153 | * @param mixed $fields array of fields or a FieldBuilder  | 
            ||
| 154 | * @return $this  | 
            ||
| 155 | */  | 
            ||
| 156 | public function addFields($fields)  | 
            ||
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * Add a field of a specific type  | 
            ||
| 171 | * @param string $name  | 
            ||
| 172 | * @param string $type  | 
            ||
| 173 | * @param array $args field configuration  | 
            ||
| 174 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 175 | * @return FieldBuilder  | 
            ||
| 176 | */  | 
            ||
| 177 | public function addField($name, $type, $args = [])  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Add a field of a choice type, allows choices to be added.  | 
            ||
| 184 | * @param string $name  | 
            ||
| 185 | * @param string $type 'select', 'radio', 'checkbox'  | 
            ||
| 186 | * @param array $args field configuration  | 
            ||
| 187 | * @throws FieldNameCollisionException if name already exists.  | 
            ||
| 188 | * @return FieldBuilder  | 
            ||
| 189 | */  | 
            ||
| 190 | public function addChoiceField($name, $type, $args = [])  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Initialize the FieldBuilder, add to FieldManager  | 
            ||
| 197 | * @param FieldBuilder $field  | 
            ||
| 198 | * @return FieldBuilder  | 
            ||
| 199 | */  | 
            ||
| 200 | protected function initializeField($field)  | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * @param string $name  | 
            ||
| 209 | * @param array $args field configuration  | 
            ||
| 210 | * @return $this  | 
            ||
| 211 | */  | 
            ||
| 212 | public function addText($name, $args = [])  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * @param string $name  | 
            ||
| 219 | * @param array $args field configuration  | 
            ||
| 220 | * @return $this  | 
            ||
| 221 | */  | 
            ||
| 222 | public function addTextarea($name, $args = [])  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * @param string $name  | 
            ||
| 229 | * @param array $args field configuration  | 
            ||
| 230 | * @return $this  | 
            ||
| 231 | */  | 
            ||
| 232 | public function addNumber($name, $args = [])  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * @param string $name  | 
            ||
| 239 | * @param array $args field configuration  | 
            ||
| 240 | * @return $this  | 
            ||
| 241 | */  | 
            ||
| 242 | public function addEmail($name, $args = [])  | 
            ||
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * @param string $name  | 
            ||
| 249 | * @param array $args field configuration  | 
            ||
| 250 | * @return $this  | 
            ||
| 251 | */  | 
            ||
| 252 | public function addUrl($name, $args = [])  | 
            ||
| 256 | |||
| 257 | /**  | 
            ||
| 258 | * @param string $name  | 
            ||
| 259 | * @param array $args field configuration  | 
            ||
| 260 | * @return $this  | 
            ||
| 261 | */  | 
            ||
| 262 | public function addPassword($name, $args = [])  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * @param string $name  | 
            ||
| 269 | * @param array $args field configuration  | 
            ||
| 270 | * @return $this  | 
            ||
| 271 | */  | 
            ||
| 272 | public function addWysiwyg($name, $args = [])  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @param string $name  | 
            ||
| 279 | * @param array $args field configuration  | 
            ||
| 280 | * @return $this  | 
            ||
| 281 | */  | 
            ||
| 282 | public function addOembed($name, $args = [])  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * @param string $name  | 
            ||
| 289 | * @param array $args field configuration  | 
            ||
| 290 | * @return $this  | 
            ||
| 291 | */  | 
            ||
| 292 | public function addImage($name, $args = [])  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * @param string $name  | 
            ||
| 299 | * @param array $args field configuration  | 
            ||
| 300 | * @return $this  | 
            ||
| 301 | */  | 
            ||
| 302 | public function addFile($name, $args = [])  | 
            ||
| 306 | |||
| 307 | /**  | 
            ||
| 308 | * @param string $name  | 
            ||
| 309 | * @param array $args field configuration  | 
            ||
| 310 | * @return $this  | 
            ||
| 311 | */  | 
            ||
| 312 | public function addGallery($name, $args = [])  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * @param string $name  | 
            ||
| 319 | * @param array $args field configuration  | 
            ||
| 320 | * @return $this  | 
            ||
| 321 | */  | 
            ||
| 322 | public function addTrueFalse($name, $args = [])  | 
            ||
| 326 | |||
| 327 | /**  | 
            ||
| 328 | * @param string $name  | 
            ||
| 329 | * @param array $args field configuration  | 
            ||
| 330 | * @return $this  | 
            ||
| 331 | */  | 
            ||
| 332 | public function addSelect($name, $args = [])  | 
            ||
| 336 | |||
| 337 | /**  | 
            ||
| 338 | * @param string $name  | 
            ||
| 339 | * @param array $args field configuration  | 
            ||
| 340 | * @return $this  | 
            ||
| 341 | */  | 
            ||
| 342 | public function addRadio($name, $args = [])  | 
            ||
| 346 | |||
| 347 | /**  | 
            ||
| 348 | * @param string $name  | 
            ||
| 349 | * @param array $args field configuration  | 
            ||
| 350 | * @return $this  | 
            ||
| 351 | */  | 
            ||
| 352 | public function addCheckbox($name, $args = [])  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * @param string $name  | 
            ||
| 359 | * @param array $args field configuration  | 
            ||
| 360 | * @return $this  | 
            ||
| 361 | */  | 
            ||
| 362 | public function addPostObject($name, $args = [])  | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * @param string $name  | 
            ||
| 369 | * @param array $args field configuration  | 
            ||
| 370 | * @return $this  | 
            ||
| 371 | */  | 
            ||
| 372 | public function addPostLink($name, $args = [])  | 
            ||
| 376 | |||
| 377 | /**  | 
            ||
| 378 | * @param string $name  | 
            ||
| 379 | * @param array $args field configuration  | 
            ||
| 380 | * @return $this  | 
            ||
| 381 | */  | 
            ||
| 382 | public function addRelationship($name, $args = [])  | 
            ||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * @param string $name  | 
            ||
| 389 | * @param array $args field configuration  | 
            ||
| 390 | * @return $this  | 
            ||
| 391 | */  | 
            ||
| 392 | public function addTaxonomy($name, $args = [])  | 
            ||
| 396 | |||
| 397 | /**  | 
            ||
| 398 | * @param string $name  | 
            ||
| 399 | * @param array $args field configuration  | 
            ||
| 400 | * @return $this  | 
            ||
| 401 | */  | 
            ||
| 402 | public function addUser($name, $args = [])  | 
            ||
| 406 | |||
| 407 | /**  | 
            ||
| 408 | * @param string $name  | 
            ||
| 409 | * @param array $args field configuration  | 
            ||
| 410 | * @return $this  | 
            ||
| 411 | */  | 
            ||
| 412 | public function addDatePicker($name, $args = [])  | 
            ||
| 416 | |||
| 417 | /**  | 
            ||
| 418 | * @param string $name  | 
            ||
| 419 | * @param array $args field configuration  | 
            ||
| 420 | * @return $this  | 
            ||
| 421 | */  | 
            ||
| 422 | public function addTimePicker($name, $args = [])  | 
            ||
| 426 | |||
| 427 | /**  | 
            ||
| 428 | * @param string $name  | 
            ||
| 429 | * @param array $args field configuration  | 
            ||
| 430 | * @return $this  | 
            ||
| 431 | */  | 
            ||
| 432 | public function addDateTimePicker($name, $args = [])  | 
            ||
| 436 | |||
| 437 | /**  | 
            ||
| 438 | * @param string $name  | 
            ||
| 439 | * @param array $args field configuration  | 
            ||
| 440 | * @return $this  | 
            ||
| 441 | */  | 
            ||
| 442 | public function addColorPicker($name, $args = [])  | 
            ||
| 446 | |||
| 447 | /**  | 
            ||
| 448 | * All fields added after will appear under this tab, until another tab  | 
            ||
| 449 | * is added.  | 
            ||
| 450 | * @param string $label Tab label  | 
            ||
| 451 | * @param array $args field configuration  | 
            ||
| 452 | * @return $this  | 
            ||
| 453 | */  | 
            ||
| 454 | public function addTab($label, $args = [])  | 
            ||
| 458 | |||
| 459 | /**  | 
            ||
| 460 | * Addes a message field  | 
            ||
| 461 | * @param string $label  | 
            ||
| 462 | * @param string $message  | 
            ||
| 463 | * @param array $args field configuration  | 
            ||
| 464 | * @return $this  | 
            ||
| 465 | */  | 
            ||
| 466 | public function addMessage($label, $message, $args = [])  | 
            ||
| 476 | |||
| 477 | /**  | 
            ||
| 478 | * Add a repeater field. Any fields added after will be added to the repeater  | 
            ||
| 479 | * until `endRepeater` is called.  | 
            ||
| 480 | * @param string $name  | 
            ||
| 481 | * @param array $args field configuration  | 
            ||
| 482 | * @return RepeaterBuilder  | 
            ||
| 483 | */  | 
            ||
| 484 | public function addRepeater($name, $args = [])  | 
            ||
| 488 | |||
| 489 | /**  | 
            ||
| 490 | * Add a flexible content field. Once adding a layout with `addLayout`,  | 
            ||
| 491 | * any fields added after will be added to that layout until another  | 
            ||
| 492 | * `addLayout` call is made, or until `endFlexibleContent` is called.  | 
            ||
| 493 | * @param string $name  | 
            ||
| 494 | * @param array $args field configuration  | 
            ||
| 495 | * @return FlexibleContentBuilder  | 
            ||
| 496 | */  | 
            ||
| 497 | public function addFlexibleContent($name, $args = [])  | 
            ||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * @return FieldManager  | 
            ||
| 504 | */  | 
            ||
| 505 | protected function getFieldManager()  | 
            ||
| 509 | |||
| 510 | /**  | 
            ||
| 511 | * @return array  | 
            ||
| 512 | */  | 
            ||
| 513 | public function getFields()  | 
            ||
| 517 | |||
| 518 | /**  | 
            ||
| 519 | * @param string $name [description]  | 
            ||
| 520 | * @return FieldBuilder  | 
            ||
| 521 | */  | 
            ||
| 522 | public function getField($name)  | 
            ||
| 526 | |||
| 527 | /**  | 
            ||
| 528 | * Modify an already defined field  | 
            ||
| 529 | * @param string $name Name of the field  | 
            ||
| 530 | * @param array|\Closure $modify Array of field configs or a closure that accepts  | 
            ||
| 531 | * a FieldsBuilder and returns a FieldsBuilder.  | 
            ||
| 532 | * @throws ModifyFieldReturnTypeException if $modify is a closure and doesn't  | 
            ||
| 533 | * return a FieldsBuilder.  | 
            ||
| 534 | * @throws FieldNotFoundException if the field name doesn't exist.  | 
            ||
| 535 | * @return $this  | 
            ||
| 536 | */  | 
            ||
| 537 | public function modifyField($name, $modify)  | 
            ||
| 567 | |||
| 568 | /**  | 
            ||
| 569 | * Remove a field by name  | 
            ||
| 570 | * @param string $name Field to remove  | 
            ||
| 571 | * @return $this  | 
            ||
| 572 | */  | 
            ||
| 573 | public function removeField($name)  | 
            ||
| 579 | |||
| 580 | /**  | 
            ||
| 581 | * Set the location of the field group. See  | 
            ||
| 582 | * https://github.com/StoutLogic/acf-builder/wiki/location and  | 
            ||
| 583 | * https://www.advancedcustomfields.com/resources/custom-location-rules/  | 
            ||
| 584 | * for more details.  | 
            ||
| 585 | * @param string $param  | 
            ||
| 586 | * @param string $operator  | 
            ||
| 587 | * @param string $value  | 
            ||
| 588 | * @return LocationBuilder  | 
            ||
| 589 | */  | 
            ||
| 590 | public function setLocation($param, $operator, $value)  | 
            ||
| 601 | |||
| 602 | /**  | 
            ||
| 603 | * @return LocationBuilder  | 
            ||
| 604 | */  | 
            ||
| 605 | public function getLocation()  | 
            ||
| 609 | |||
| 610 | /**  | 
            ||
| 611 | * Create a field label based on the field's name. Generates title case.  | 
            ||
| 612 | * @param string $name  | 
            ||
| 613 | * @return string label  | 
            ||
| 614 | */  | 
            ||
| 615 | protected function generateLabel($name)  | 
            ||
| 619 | |||
| 620 | /**  | 
            ||
| 621 | * Generates a snaked cased name.  | 
            ||
| 622 | * @param string $name  | 
            ||
| 623 | * @return string  | 
            ||
| 624 | */  | 
            ||
| 625 | protected function generateName($name)  | 
            ||
| 629 | }  | 
            ||
| 630 | 
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: