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 GridService 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 GridService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class GridService extends AbstractTca |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $tca; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $tableName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * All fields available in the Grid. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $fields; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * All fields regardless whether they have been excluded or not. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $allFields; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $instances; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $facets; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * __construct |
||
| 70 | * |
||
| 71 | * @throws InvalidKeyInArrayException |
||
| 72 | * @param string $tableName |
||
| 73 | * @return \Fab\Vidi\Tca\GridService |
||
|
|
|||
| 74 | */ |
||
| 75 | public function __construct($tableName) |
||
| 76 | { |
||
| 77 | |||
| 78 | $this->tableName = $tableName; |
||
| 79 | |||
| 80 | if (empty($GLOBALS['TCA'][$this->tableName])) { |
||
| 81 | throw new InvalidKeyInArrayException('No TCA existence for table name: ' . $this->tableName, 1356945108); |
||
| 82 | } |
||
| 83 | |||
| 84 | $this->tca = $GLOBALS['TCA'][$this->tableName]['grid']; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns an array containing column names. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getFieldNames() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns an array containing column names. |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function getAllFieldNames() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the label key. |
||
| 111 | * |
||
| 112 | * @param string $fieldNameAndPath |
||
| 113 | * @return string |
||
| 114 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 115 | */ |
||
| 116 | public function getLabelKey($fieldNameAndPath) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the translation of a label given a column name. |
||
| 143 | * |
||
| 144 | * @param string $fieldNameAndPath |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getLabel($fieldNameAndPath) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the field name given its position. |
||
| 173 | * |
||
| 174 | * @param string $position the position of the field in the grid |
||
| 175 | * @throws InvalidKeyInArrayException |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getFieldNameByPosition($position) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a field name. |
||
| 190 | * |
||
| 191 | * @param string $fieldName |
||
| 192 | * @return array |
||
| 193 | * @throws InvalidKeyInArrayException |
||
| 194 | */ |
||
| 195 | public function getField($fieldName) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns an array containing column names for the Grid. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | * @throws \Exception |
||
| 206 | */ |
||
| 207 | public function getFields() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Remove fields according to Grid configuration. |
||
| 231 | * |
||
| 232 | * @param $fields |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function filterByIncludedFields($fields) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Remove fields according to BE User permission. |
||
| 253 | * |
||
| 254 | * @param $fields |
||
| 255 | * @return array |
||
| 256 | * @throws \Exception |
||
| 257 | */ |
||
| 258 | View Code Duplication | protected function filterByBackendUser($fields) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Remove fields according to Grid configuration. |
||
| 272 | * |
||
| 273 | * @param $fields |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function filterByExcludedFields($fields) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Returns an array containing column names for the Grid. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getAllFields() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Tell whether the field exists in the grid or not. |
||
| 348 | * |
||
| 349 | * @param string $fieldName |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function hasField($fieldName) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Tell whether the facet exists in the grid or not. |
||
| 360 | * |
||
| 361 | * @param string $facetName |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function hasFacet($facetName) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns an array containing facets fields. |
||
| 372 | * |
||
| 373 | * @return FacetInterface[] |
||
| 374 | */ |
||
| 375 | public function getFacets() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the "sortable" value of the column. |
||
| 395 | * |
||
| 396 | * @param string $fieldName |
||
| 397 | * @return int|string |
||
| 398 | */ |
||
| 399 | public function isSortable($fieldName) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Returns the "canBeHidden" value of the column. |
||
| 413 | * |
||
| 414 | * @param string $fieldName |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function canBeHidden($fieldName) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Returns the "width" value of the column. |
||
| 425 | * |
||
| 426 | * @param string $fieldName |
||
| 427 | * @return int|string |
||
| 428 | */ |
||
| 429 | public function getWidth($fieldName) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the "visible" value of the column. |
||
| 437 | * |
||
| 438 | * @param string $fieldName |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public function isVisible($fieldName) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns the "editable" value of the column. |
||
| 449 | * |
||
| 450 | * @param string $columnName |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function isEditable($columnName) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the "localized" value of the column. |
||
| 461 | * |
||
| 462 | * @param string $columnName |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function isLocalized($columnName) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * |
||
| 473 | * Returns the "html" value of the column. |
||
| 474 | * |
||
| 475 | * @param string $fieldName |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function getHeader($fieldName) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Fetch a possible from a Grid Renderer. If no value is found, returns NULL |
||
| 486 | * |
||
| 487 | * @param string $fieldName |
||
| 488 | * @param string $key |
||
| 489 | * @param mixed $defaultValue |
||
| 490 | * @return NULL|mixed |
||
| 491 | */ |
||
| 492 | public function get($fieldName, $key, $defaultValue = NULL) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns whether the column has a renderer. |
||
| 512 | * |
||
| 513 | * @param string $fieldName |
||
| 514 | * @return bool |
||
| 515 | */ |
||
| 516 | public function hasRenderers($fieldName) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Returns a renderer. |
||
| 524 | * |
||
| 525 | * @param string $fieldName |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public function getRenderers($fieldName) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param string|GenericColumn $renderer |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | public function convertRendererToArray($renderer) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns the class names applied to a cell |
||
| 562 | * |
||
| 563 | * @param string $fieldName |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | public function getClass($fieldName) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Returns whether the column has a label. |
||
| 574 | * |
||
| 575 | * @param string $fieldNameAndPath |
||
| 576 | * @return bool |
||
| 577 | */ |
||
| 578 | public function hasLabel($fieldNameAndPath) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @return array |
||
| 599 | */ |
||
| 600 | public function getTca() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | public function getIncludedFields() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Return excluded fields from configuration + preferences. |
||
| 615 | * |
||
| 616 | * @return array |
||
| 617 | */ |
||
| 618 | public function getExcludedFields() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Fetch excluded fields from configuration. |
||
| 628 | * |
||
| 629 | * @return array |
||
| 630 | */ |
||
| 631 | protected function getExcludedFieldsFromConfiguration() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Fetch excluded fields from preferences. |
||
| 645 | * |
||
| 646 | * @return array |
||
| 647 | */ |
||
| 648 | protected function getExcludedFieldsFromPreferences() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return array |
||
| 656 | */ |
||
| 657 | public function areFilesIncludedInExport() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Returns a "facet" service instance. |
||
| 669 | * |
||
| 670 | * @param string|FacetInterface $facetName |
||
| 671 | * @return StandardFacet |
||
| 672 | */ |
||
| 673 | protected function instantiateStandardFacet($facetName) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Returns a "facet" service instance. |
||
| 688 | * |
||
| 689 | * @param string|FacetInterface $facetName |
||
| 690 | * @return FacetInterface |
||
| 691 | */ |
||
| 692 | public function facet($facetName = '') |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @return \Fab\Vidi\Resolver\FieldPathResolver |
||
| 700 | */ |
||
| 701 | protected function getFieldPathResolver() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @return ModulePreferences |
||
| 708 | */ |
||
| 709 | protected function getModulePreferences() |
||
| 713 | |||
| 714 | } |
||
| 715 |
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.