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 AbstractRouteRepository 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 AbstractRouteRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class AbstractRouteRepository extends SortableRepository |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string The default sorting field/expression |
||
| 42 | */ |
||
| 43 | protected $defaultSortingField = 'sort'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Request The request object given by the calling controller |
||
| 47 | */ |
||
| 48 | protected $request; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Retrieves an array with all fields which can be used for sorting instances. |
||
| 52 | * |
||
| 53 | * @return array Sorting fields array |
||
| 54 | */ |
||
| 55 | public function getAllowedSortingFields() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the default sorting field. |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getDefaultSortingField() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Sets the default sorting field. |
||
| 93 | * |
||
| 94 | * @param string $defaultSortingField |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | public function setDefaultSortingField($defaultSortingField) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the request. |
||
| 105 | * |
||
| 106 | * @return Request |
||
| 107 | */ |
||
| 108 | public function getRequest() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets the request. |
||
| 115 | * |
||
| 116 | * @param Request $request |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function setRequest($request) |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns name of the field used as title / name for entities of this repository. |
||
| 128 | * |
||
| 129 | * @return string Name of field to be used as title |
||
| 130 | */ |
||
| 131 | public function getTitleFieldName() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns name of the field used for describing entities of this repository. |
||
| 140 | * |
||
| 141 | * @return string Name of field to be used as description |
||
| 142 | */ |
||
| 143 | public function getDescriptionFieldName() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns name of first upload field which is capable for handling images. |
||
| 152 | * |
||
| 153 | * @return string Name of field to be used for preview images |
||
| 154 | */ |
||
| 155 | public function getPreviewFieldName() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns name of the date(time) field to be used for representing the start |
||
| 164 | * of this object. Used for providing meta data to the tag module. |
||
| 165 | * |
||
| 166 | * @return string Name of field to be used as date |
||
| 167 | */ |
||
| 168 | public function getStartDateFieldName() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns an array of additional template variables which are specific to the object type treated by this repository. |
||
| 177 | * |
||
| 178 | * @param string $context Usage context (allowed values: controllerAction, api, actionHandler, block, contentType) |
||
| 179 | * @param array $args Additional arguments |
||
| 180 | * |
||
| 181 | * @return array List of template variables to be assigned |
||
| 182 | */ |
||
| 183 | public function getAdditionalTemplateParameters($context = '', $args = []) |
||
| 207 | /** |
||
| 208 | * Returns an array of additional template variables for view quick navigation forms. |
||
| 209 | * |
||
| 210 | * @param string $context Usage context (allowed values: controllerAction, api, actionHandler, block, contentType) |
||
| 211 | * @param array $args Additional arguments |
||
| 212 | * |
||
| 213 | * @return array List of template variables to be assigned |
||
| 214 | */ |
||
| 215 | protected function getViewQuickNavParameters($context = '', $args = []) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Helper method for truncating the table. |
||
| 241 | * Used during installation when inserting default data. |
||
| 242 | * |
||
| 243 | * @param LoggerInterface $logger Logger service instance |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function truncateTable(LoggerInterface $logger) |
||
| 258 | /** |
||
| 259 | * Updates the creator of all objects created by a certain user. |
||
| 260 | * |
||
| 261 | * @param integer $userId The userid of the creator to be replaced |
||
| 262 | * @param integer $newUserId The new userid of the creator as replacement |
||
| 263 | * @param TranslatorInterface $translator Translator service instance |
||
| 264 | * @param LoggerInterface $logger Logger service instance |
||
| 265 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | * |
||
| 269 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function updateCreator($userId, $newUserId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Updates the last editor of all objects updated by a certain user. |
||
| 293 | * |
||
| 294 | * @param integer $userId The userid of the last editor to be replaced |
||
| 295 | * @param integer $newUserId The new userid of the last editor as replacement |
||
| 296 | * @param TranslatorInterface $translator Translator service instance |
||
| 297 | * @param LoggerInterface $logger Logger service instance |
||
| 298 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | * |
||
| 302 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function updateLastEditor($userId, $newUserId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Deletes all objects created by a certain user. |
||
| 326 | * |
||
| 327 | * @param integer $userId The userid of the creator to be removed |
||
| 328 | * @param TranslatorInterface $translator Translator service instance |
||
| 329 | * @param LoggerInterface $logger Logger service instance |
||
| 330 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | * |
||
| 334 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 335 | */ |
||
| 336 | View Code Duplication | public function deleteByCreator($userId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Deletes all objects updated by a certain user. |
||
| 357 | * |
||
| 358 | * @param integer $userId The userid of the last editor to be removed |
||
| 359 | * @param TranslatorInterface $translator Translator service instance |
||
| 360 | * @param LoggerInterface $logger Logger service instance |
||
| 361 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | * |
||
| 365 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 366 | */ |
||
| 367 | View Code Duplication | public function deleteByLastEditor($userId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Adds an array of id filters to given query instance. |
||
| 388 | * |
||
| 389 | * @param mixed $idList The array of ids to use to retrieve the object |
||
| 390 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 391 | * |
||
| 392 | * @return QueryBuilder Enriched query builder instance |
||
| 393 | */ |
||
| 394 | protected function addIdListFilter($idList, QueryBuilder $qb) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Selects an object from the database. |
||
| 422 | * |
||
| 423 | * @param mixed $id The id (or array of ids) to use to retrieve the object (optional) (default=0) |
||
| 424 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 425 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 426 | * |
||
| 427 | * @return array|routeEntity retrieved data array or routeEntity instance |
||
| 428 | * |
||
| 429 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 430 | */ |
||
| 431 | public function selectById($id = 0, $useJoins = true, $slimMode = false) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Selects a list of objects with an array of ids |
||
| 440 | * |
||
| 441 | * @param mixed $idList The array of ids to use to retrieve the objects (optional) (default=0) |
||
| 442 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 443 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 444 | * |
||
| 445 | * @return ArrayCollection collection containing retrieved routeEntity instances |
||
| 446 | * |
||
| 447 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 448 | */ |
||
| 449 | public function selectByIdList($idList = [0], $useJoins = true, $slimMode = false) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Adds where clauses excluding desired identifiers from selection. |
||
| 463 | * |
||
| 464 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 465 | * @param integer $excludeId The id (or array of ids) to be excluded from selection |
||
| 466 | * |
||
| 467 | * @return QueryBuilder Enriched query builder instance |
||
| 468 | */ |
||
| 469 | protected function addExclusion(QueryBuilder $qb, $excludeId) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns query builder for selecting a list of objects with a given where clause. |
||
| 481 | * |
||
| 482 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 483 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 484 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 485 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 486 | * |
||
| 487 | * @return QueryBuilder query builder for the given arguments |
||
| 488 | */ |
||
| 489 | public function getListQueryBuilder($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Selects a list of objects with a given where clause. |
||
| 501 | * |
||
| 502 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 503 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 504 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 505 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 506 | * |
||
| 507 | * @return ArrayCollection collection containing retrieved routeEntity instances |
||
| 508 | */ |
||
| 509 | public function selectWhere($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns query builder instance for retrieving a list of objects with a given where clause and pagination parameters. |
||
| 520 | * |
||
| 521 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 522 | * @param integer $currentPage Where to start selection |
||
| 523 | * @param integer $resultsPerPage Amount of items to select |
||
| 524 | * |
||
| 525 | * @return array Created query instance and amount of affected items |
||
| 526 | */ |
||
| 527 | public function getSelectWherePaginatedQuery(QueryBuilder $qb, $currentPage = 1, $resultsPerPage = 25) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Selects a list of objects with a given where clause and pagination parameters. |
||
| 544 | * |
||
| 545 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 546 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 547 | * @param integer $currentPage Where to start selection |
||
| 548 | * @param integer $resultsPerPage Amount of items to select |
||
| 549 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 550 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 551 | * |
||
| 552 | * @return array with retrieved collection and amount of total records affected by this query |
||
| 553 | */ |
||
| 554 | public function selectWherePaginated($where = '', $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true, $slimMode = false) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Adds quick navigation related filter options as where clauses. |
||
| 578 | * |
||
| 579 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 580 | * |
||
| 581 | * @return QueryBuilder Enriched query builder instance |
||
| 582 | */ |
||
| 583 | public function addCommonViewFilters(QueryBuilder $qb) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Adds default filters as where clauses. |
||
| 633 | * |
||
| 634 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 635 | * @param array $parameters List of determined filter options |
||
| 636 | * |
||
| 637 | * @return QueryBuilder Enriched query builder instance |
||
| 638 | */ |
||
| 639 | protected function applyDefaultFilters(QueryBuilder $qb, $parameters = []) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Selects entities by a given search fragment. |
||
| 647 | * |
||
| 648 | * @param string $fragment The fragment to search for |
||
| 649 | * @param array $exclude Comma separated list with ids to be excluded from search |
||
| 650 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 651 | * @param integer $currentPage Where to start selection |
||
| 652 | * @param integer $resultsPerPage Amount of items to select |
||
| 653 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 654 | * |
||
| 655 | * @return array with retrieved collection and amount of total records affected by this query |
||
| 656 | */ |
||
| 657 | public function selectSearch($fragment = '', $exclude = [], $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Adds where clause for search query. |
||
| 674 | * |
||
| 675 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 676 | * @param string $fragment The fragment to search for |
||
| 677 | * |
||
| 678 | * @return QueryBuilder Enriched query builder instance |
||
| 679 | */ |
||
| 680 | protected function addSearchFilter(QueryBuilder $qb, $fragment = '') |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Performs a given database selection and post-processed the results. |
||
| 756 | * |
||
| 757 | * @param Query $query The Query instance to be executed |
||
| 758 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 759 | * @param boolean $isPaginated Whether the given query uses a paginator or not (optional) (default=false) |
||
| 760 | * |
||
| 761 | * @return array with retrieved collection and (for paginated queries) the amount of total records affected |
||
| 762 | */ |
||
| 763 | public function retrieveCollectionResult(Query $query, $orderBy = '', $isPaginated = false) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Returns query builder instance for a count query. |
||
| 784 | * |
||
| 785 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 786 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 787 | * |
||
| 788 | * @return QueryBuilder Created query builder instance |
||
| 789 | * @TODO fix usage of joins; please remove the first line and test |
||
| 790 | */ |
||
| 791 | protected function getCountQuery($where = '', $useJoins = true) |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Selects entity count with a given where clause. |
||
| 815 | * |
||
| 816 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 817 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 818 | * @param array $parameters List of determined filter options |
||
| 819 | * |
||
| 820 | * @return integer amount of affected records |
||
| 821 | */ |
||
| 822 | public function selectCount($where = '', $useJoins = true, $parameters = []) |
||
| 832 | |||
| 833 | |||
| 834 | /** |
||
| 835 | * Checks for unique values. |
||
| 836 | * |
||
| 837 | * @param string $fieldName The name of the property to be checked |
||
| 838 | * @param string $fieldValue The value of the property to be checked |
||
| 839 | * @param int $excludeId Id of routes to exclude (optional) |
||
| 840 | * |
||
| 841 | * @return boolean result of this check, true if the given route does not already exist |
||
| 842 | */ |
||
| 843 | public function detectUniqueState($fieldName, $fieldValue, $excludeId = 0) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Builds a generic Doctrine query supporting WHERE and ORDER BY. |
||
| 860 | * |
||
| 861 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 862 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 863 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 864 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 865 | * |
||
| 866 | * @return QueryBuilder query builder instance to be further processed |
||
| 867 | */ |
||
| 868 | public function genericBaseQuery($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Adds WHERE clause to given query builder. |
||
| 907 | * |
||
| 908 | * @param QueryBuilder $qb Given query builder instance |
||
| 909 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 910 | * |
||
| 911 | * @return QueryBuilder query builder instance to be further processed |
||
| 912 | */ |
||
| 913 | protected function genericBaseQueryAddWhere(QueryBuilder $qb, $where = '') |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Adds ORDER BY clause to given query builder. |
||
| 981 | * |
||
| 982 | * @param QueryBuilder $qb Given query builder instance |
||
| 983 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 984 | * |
||
| 985 | * @return QueryBuilder query builder instance to be further processed |
||
| 986 | */ |
||
| 987 | protected function genericBaseQueryAddOrderBy(QueryBuilder $qb, $orderBy = '') |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Retrieves Doctrine query from query builder, applying FilterUtil and other common actions. |
||
| 1011 | * |
||
| 1012 | * @param QueryBuilder $qb Query builder instance |
||
| 1013 | * |
||
| 1014 | * @return Query query instance to be further processed |
||
| 1015 | */ |
||
| 1016 | public function getQueryFromBuilder(QueryBuilder $qb) |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Helper method to add join selections. |
||
| 1025 | * |
||
| 1026 | * @return String Enhancement for select clause |
||
| 1027 | */ |
||
| 1028 | protected function addJoinsToSelection() |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Helper method to add joins to from clause. |
||
| 1037 | * |
||
| 1038 | * @param QueryBuilder $qb query builder instance used to create the query |
||
| 1039 | * |
||
| 1040 | * @return String Enhancement for from clause |
||
| 1041 | */ |
||
| 1042 | protected function addJoinsToFrom(QueryBuilder $qb) |
||
| 1047 | } |
||
| 1048 |