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 FieldService 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 FieldService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class FieldService extends AbstractTca |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $fieldName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $compositeField; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $tableName; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $tca; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $fieldName |
||
| 42 | * @param array $tca |
||
| 43 | * @param string $tableName |
||
| 44 | * @param string $compositeField |
||
| 45 | * @return \Fab\Vidi\Tca\FieldService |
||
|
|
|||
| 46 | */ |
||
| 47 | public function __construct($fieldName, array $tca, $tableName, $compositeField = '') |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Tells whether the field is considered as system field, e.g. uid, crdate, tstamp, etc... |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function isSystem() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Tells the opposition of isSystem() |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function isNotSystem() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the configuration for a $field |
||
| 77 | * |
||
| 78 | * @throws \Exception |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function getConfiguration() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns a key of the configuration. |
||
| 88 | * If the key can not to be found, returns null. |
||
| 89 | * |
||
| 90 | * @param string $key |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function get($key) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the foreign field of a given field (opposite relational field). |
||
| 101 | * If no relation exists, returns null. |
||
| 102 | * |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function getForeignField() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the foreign table of a given field (opposite relational table). |
||
| 133 | * If no relation exists, returns null. |
||
| 134 | * |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function getForeignTable() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the foreign clause. |
||
| 153 | * If no foreign order exists, returns empty string. |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function getForeignClause() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Substitute some known markers from the where clause in the Frontend Context. |
||
| 175 | * |
||
| 176 | * @param string $clause |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected function substituteKnownMarkers($clause) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the foreign order of the current field. |
||
| 200 | * If no foreign order exists, returns empty string. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function getForeignOrder() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the MM table of a field. |
||
| 220 | * If no relation exists, returns null. |
||
| 221 | * |
||
| 222 | * @return string|null |
||
| 223 | */ |
||
| 224 | public function getManyToManyTable() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns a possible additional table name used in MM relations. |
||
| 232 | * If no table name exists, returns null. |
||
| 233 | * |
||
| 234 | * @return string|null |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function getAdditionalTableNameCondition() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Returns a possible additional conditions for MM tables such as "tablenames", "fieldname", etc... |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getAdditionalMMCondition() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns whether the field name is the opposite in MM relation. |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function isOppositeRelation() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the configuration for a $field. |
||
| 292 | * |
||
| 293 | * @throws \Exception |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getType() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return the default value. |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function getDefaultValue() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the translation of a label given a column. |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getLabel() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get the translation of a label given a column. |
||
| 372 | * |
||
| 373 | * @param string $itemValue the item value to search for. |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getLabelForItem($itemValue) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Retrieve items from User Function. |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | protected function fetchItemsFromUserFunction() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get a possible icon given a field name an an item. |
||
| 444 | * |
||
| 445 | * @param string $itemValue the item value to search for. |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getIconForItem($itemValue) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns whether the field has a label. |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public function hasLabel() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Tell whether the current BE User has access to this field. |
||
| 475 | * |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public function hasAccess() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns whether the field is numerical. |
||
| 493 | * |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | public function isNumerical() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns whether the field is of type text area. |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | public function isTextArea() |
||
| 519 | /** |
||
| 520 | * Returns whether the field is of type text area. |
||
| 521 | * |
||
| 522 | * @return bool |
||
| 523 | */ |
||
| 524 | public function isText() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns whether the field is displayed as a tree. |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function isRenderModeTree() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns whether the field is of type select. |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | public function isSelect() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Returns whether the field is of type select. |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public function isMultipleSelect() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns whether the field is of type select. |
||
| 562 | * |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | public function isCheckBox() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Returns whether the field is of type db. |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public function isGroup() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Returns whether the field is language aware. |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | public function isLocalized() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Returns whether the field is required. |
||
| 599 | * |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function isRequired() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Returns an array containing the configuration of a column. |
||
| 619 | * |
||
| 620 | * @return array |
||
| 621 | */ |
||
| 622 | public function getField() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Returns the relation type |
||
| 629 | * |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | public function relationDataType() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Returns whether the field has relation (one to many, many to many) |
||
| 640 | * |
||
| 641 | * @return bool |
||
| 642 | */ |
||
| 643 | public function hasRelation() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Returns whether the field has no relation (one to many, many to many) |
||
| 650 | * |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | public function hasNoRelation() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Returns whether the field has a "many" objects connected including "many-to-many" or "one-to-many". |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function hasMany() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Returns whether the field has relation "one" object connected including of "one-to-one" or "many-to-one". |
||
| 671 | * |
||
| 672 | * @return bool |
||
| 673 | */ |
||
| 674 | public function hasOne() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Returns whether the field has many-to-one relation. |
||
| 682 | * |
||
| 683 | * @return bool |
||
| 684 | */ |
||
| 685 | View Code Duplication | public function hasRelationManyToOne() |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Returns whether the field has one-to-many relation. |
||
| 701 | * |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | View Code Duplication | public function hasRelationOneToMany() |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Returns whether the field has one-to-one relation. |
||
| 720 | * |
||
| 721 | * @return bool |
||
| 722 | */ |
||
| 723 | View Code Duplication | public function hasRelationOneToOne() |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Returns whether the field has many to many relation. |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | public function hasRelationManyToMany() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Returns whether the field has many to many relation using comma separated values (legacy). |
||
| 750 | * |
||
| 751 | * @return bool |
||
| 752 | */ |
||
| 753 | public function hasRelationWithCommaSeparatedValues() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @return array |
||
| 761 | */ |
||
| 762 | public function getTca() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @return string |
||
| 769 | */ |
||
| 770 | public function getCompositeField() |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @param string $compositeField |
||
| 777 | */ |
||
| 778 | public function setCompositeField($compositeField) |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Returns an instance of the Frontend object. |
||
| 785 | * |
||
| 786 | * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController |
||
| 787 | */ |
||
| 788 | protected function getFrontendObject() |
||
| 792 | |||
| 793 | } |
||
| 794 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.