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 |
||
| 37 | abstract class AbstractRouteRepository extends SortableRepository |
||
| 38 | { |
||
| 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 | /** |
||
| 52 | * Retrieves an array with all fields which can be used for sorting instances. |
||
| 53 | * |
||
| 54 | * @return array Sorting fields array |
||
| 55 | */ |
||
| 56 | public function getAllowedSortingFields() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the default sorting field. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getDefaultSortingField() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Sets the default sorting field. |
||
| 94 | * |
||
| 95 | * @param string $defaultSortingField |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function setDefaultSortingField($defaultSortingField) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Returns the request. |
||
| 106 | * |
||
| 107 | * @return Request |
||
| 108 | */ |
||
| 109 | public function getRequest() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the request. |
||
| 116 | * |
||
| 117 | * @param Request $request |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function setRequest($request) |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns name of the field used as title / name for entities of this repository. |
||
| 129 | * |
||
| 130 | * @return string Name of field to be used as title |
||
| 131 | */ |
||
| 132 | public function getTitleFieldName() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns name of the field used for describing entities of this repository. |
||
| 141 | * |
||
| 142 | * @return string Name of field to be used as description |
||
| 143 | */ |
||
| 144 | public function getDescriptionFieldName() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns name of first upload field which is capable for handling images. |
||
| 153 | * |
||
| 154 | * @return string Name of field to be used for preview images |
||
| 155 | */ |
||
| 156 | public function getPreviewFieldName() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns name of the date(time) field to be used for representing the start |
||
| 165 | * of this object. Used for providing meta data to the tag module. |
||
| 166 | * |
||
| 167 | * @return string Name of field to be used as date |
||
| 168 | */ |
||
| 169 | public function getStartDateFieldName() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns an array of additional template variables which are specific to the object type treated by this repository. |
||
| 178 | * |
||
| 179 | * @param ImageHelper $imageHelper ImageHelper service instance |
||
| 180 | * @param string $context Usage context (allowed values: controllerAction, api, actionHandler, block, contentType) |
||
| 181 | * @param array $args Additional arguments |
||
| 182 | * |
||
| 183 | * @return array List of template variables to be assigned |
||
| 184 | */ |
||
| 185 | public function getAdditionalTemplateParameters($context = '', $args = []) |
||
| 209 | /** |
||
| 210 | * Returns an array of additional template variables for view quick navigation forms. |
||
| 211 | * |
||
| 212 | * @param string $context Usage context (allowed values: controllerAction, api, actionHandler, block, contentType) |
||
| 213 | * @param array $args Additional arguments |
||
| 214 | * |
||
| 215 | * @return array List of template variables to be assigned |
||
| 216 | */ |
||
| 217 | protected function getViewQuickNavParameters($context = '', $args = []) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Helper method for truncating the table. |
||
| 243 | * Used during installation when inserting default data. |
||
| 244 | * |
||
| 245 | * @param LoggerInterface $logger Logger service instance |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function truncateTable(LoggerInterface $logger) |
||
| 260 | /** |
||
| 261 | * Updates the creator of all objects created by a certain user. |
||
| 262 | * |
||
| 263 | * @param integer $userId The userid of the creator to be replaced |
||
| 264 | * @param integer $newUserId The new userid of the creator as replacement |
||
| 265 | * @param TranslatorInterface $translator Translator service instance |
||
| 266 | * @param LoggerInterface $logger Logger service instance |
||
| 267 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | * |
||
| 271 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 272 | */ |
||
| 273 | View Code Duplication | public function updateCreator($userId, $newUserId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Updates the last editor of all objects updated by a certain user. |
||
| 295 | * |
||
| 296 | * @param integer $userId The userid of the last editor to be replaced |
||
| 297 | * @param integer $newUserId The new userid of the last editor as replacement |
||
| 298 | * @param TranslatorInterface $translator Translator service instance |
||
| 299 | * @param LoggerInterface $logger Logger service instance |
||
| 300 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | * |
||
| 304 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function updateLastEditor($userId, $newUserId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Deletes all objects created by a certain user. |
||
| 328 | * |
||
| 329 | * @param integer $userId The userid of the creator to be removed |
||
| 330 | * @param TranslatorInterface $translator Translator service instance |
||
| 331 | * @param LoggerInterface $logger Logger service instance |
||
| 332 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 333 | * |
||
| 334 | * @return void |
||
| 335 | * |
||
| 336 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function deleteByCreator($userId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Deletes all objects updated by a certain user. |
||
| 359 | * |
||
| 360 | * @param integer $userId The userid of the last editor to be removed |
||
| 361 | * @param TranslatorInterface $translator Translator service instance |
||
| 362 | * @param LoggerInterface $logger Logger service instance |
||
| 363 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | * |
||
| 367 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 368 | */ |
||
| 369 | View Code Duplication | public function deleteByLastEditor($userId, TranslatorInterface $translator, LoggerInterface $logger, CurrentUserApi $currentUserApi) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Adds an array of id filters to given query instance. |
||
| 390 | * |
||
| 391 | * @param mixed $idList The array of ids to use to retrieve the object |
||
| 392 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 393 | * |
||
| 394 | * @return QueryBuilder Enriched query builder instance |
||
| 395 | */ |
||
| 396 | protected function addIdListFilter($idList, QueryBuilder $qb) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Selects an object from the database. |
||
| 424 | * |
||
| 425 | * @param mixed $id The id (or array of ids) to use to retrieve the object (optional) (default=0) |
||
| 426 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 427 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 428 | * |
||
| 429 | * @return array|routeEntity retrieved data array or routeEntity instance |
||
| 430 | * |
||
| 431 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 432 | */ |
||
| 433 | public function selectById($id = 0, $useJoins = true, $slimMode = false) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Selects a list of objects with an array of ids |
||
| 442 | * |
||
| 443 | * @param mixed $idList The array of ids to use to retrieve the objects (optional) (default=0) |
||
| 444 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 445 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 446 | * |
||
| 447 | * @return ArrayCollection collection containing retrieved routeEntity instances |
||
| 448 | * |
||
| 449 | * @throws InvalidArgumentException Thrown if invalid parameters are received |
||
| 450 | */ |
||
| 451 | View Code Duplication | public function selectByIdList($idList = [0], $useJoins = true, $slimMode = false) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Adds where clauses excluding desired identifiers from selection. |
||
| 465 | * |
||
| 466 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 467 | * @param integer $excludeId The id to be excluded from selection |
||
| 468 | * |
||
| 469 | * @return QueryBuilder Enriched query builder instance |
||
| 470 | */ |
||
| 471 | protected function addExclusion(QueryBuilder $qb, $excludeId) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns query builder for selecting a list of objects with a given where clause. |
||
| 483 | * |
||
| 484 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 485 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 486 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 487 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 488 | * |
||
| 489 | * @return QueryBuilder query builder for the given arguments |
||
| 490 | */ |
||
| 491 | public function getListQueryBuilder($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Selects a list of objects with a given where clause. |
||
| 503 | * |
||
| 504 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 505 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 506 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 507 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 508 | * |
||
| 509 | * @return ArrayCollection collection containing retrieved routeEntity instances |
||
| 510 | */ |
||
| 511 | public function selectWhere($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns query builder instance for retrieving a list of objects with a given where clause and pagination parameters. |
||
| 522 | * |
||
| 523 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 524 | * @param integer $currentPage Where to start selection |
||
| 525 | * @param integer $resultsPerPage Amount of items to select |
||
| 526 | * |
||
| 527 | * @return Query Created query instance |
||
| 528 | */ |
||
| 529 | 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 | View Code Duplication | public function selectWherePaginated($where = '', $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true, $slimMode = false) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Adds quick navigation related filter options as where clauses. |
||
| 567 | * |
||
| 568 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 569 | * |
||
| 570 | * @return QueryBuilder Enriched query builder instance |
||
| 571 | */ |
||
| 572 | public function addCommonViewFilters(QueryBuilder $qb) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Adds default filters as where clauses. |
||
| 622 | * |
||
| 623 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 624 | * @param array $parameters List of determined filter options |
||
| 625 | * |
||
| 626 | * @return QueryBuilder Enriched query builder instance |
||
| 627 | */ |
||
| 628 | protected function applyDefaultFilters(QueryBuilder $qb, $parameters = []) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Selects entities by a given search fragment. |
||
| 636 | * |
||
| 637 | * @param string $fragment The fragment to search for |
||
| 638 | * @param array $exclude List with identifiers to be excluded from search |
||
| 639 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 640 | * @param integer $currentPage Where to start selection |
||
| 641 | * @param integer $resultsPerPage Amount of items to select |
||
| 642 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 643 | * |
||
| 644 | * @return array with retrieved collection and amount of total records affected by this query |
||
| 645 | */ |
||
| 646 | public function selectSearch($fragment = '', $exclude = [], $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Adds where clause for search query. |
||
| 662 | * |
||
| 663 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 664 | * @param string $fragment The fragment to search for |
||
| 665 | * |
||
| 666 | * @return QueryBuilder Enriched query builder instance |
||
| 667 | */ |
||
| 668 | protected function addSearchFilter(QueryBuilder $qb, $fragment = '') |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Performs a given database selection and post-processed the results. |
||
| 744 | * |
||
| 745 | * @param Query $query The Query instance to be executed |
||
| 746 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 747 | * @param boolean $isPaginated Whether the given query uses a paginator or not (optional) (default=false) |
||
| 748 | * |
||
| 749 | * @return array with retrieved collection and (for paginated queries) the amount of total records affected |
||
| 750 | */ |
||
| 751 | public function retrieveCollectionResult(Query $query, $orderBy = '', $isPaginated = false) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Returns query builder instance for a count query. |
||
| 772 | * |
||
| 773 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 774 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 775 | * |
||
| 776 | * @return QueryBuilder Created query builder instance |
||
| 777 | * @TODO fix usage of joins; please remove the first line and test |
||
| 778 | */ |
||
| 779 | protected function getCountQuery($where = '', $useJoins = true) |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Selects entity count with a given where clause. |
||
| 803 | * |
||
| 804 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 805 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 806 | * @param array $parameters List of determined filter options |
||
| 807 | * |
||
| 808 | * @return integer amount of affected records |
||
| 809 | */ |
||
| 810 | public function selectCount($where = '', $useJoins = true, $parameters = []) |
||
| 820 | |||
| 821 | |||
| 822 | /** |
||
| 823 | * Checks for unique values. |
||
| 824 | * |
||
| 825 | * @param string $fieldName The name of the property to be checked |
||
| 826 | * @param string $fieldValue The value of the property to be checked |
||
| 827 | * @param int $excludeId Id of routes to exclude (optional) |
||
| 828 | * |
||
| 829 | * @return boolean result of this check, true if the given route does not already exist |
||
| 830 | */ |
||
| 831 | public function detectUniqueState($fieldName, $fieldValue, $excludeId = 0) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Builds a generic Doctrine query supporting WHERE and ORDER BY. |
||
| 848 | * |
||
| 849 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 850 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 851 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 852 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 853 | * |
||
| 854 | * @return QueryBuilder query builder instance to be further processed |
||
| 855 | */ |
||
| 856 | public function genericBaseQuery($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Adds WHERE clause to given query builder. |
||
| 895 | * |
||
| 896 | * @param QueryBuilder $qb Given query builder instance |
||
| 897 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 898 | * |
||
| 899 | * @return QueryBuilder query builder instance to be further processed |
||
| 900 | */ |
||
| 901 | protected function genericBaseQueryAddWhere(QueryBuilder $qb, $where = '') |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Adds ORDER BY clause to given query builder. |
||
| 967 | * |
||
| 968 | * @param QueryBuilder $qb Given query builder instance |
||
| 969 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 970 | * |
||
| 971 | * @return QueryBuilder query builder instance to be further processed |
||
| 972 | */ |
||
| 973 | protected function genericBaseQueryAddOrderBy(QueryBuilder $qb, $orderBy = '') |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Retrieves Doctrine query from query builder, applying FilterUtil and other common actions. |
||
| 997 | * |
||
| 998 | * @param QueryBuilder $qb Query builder instance |
||
| 999 | * |
||
| 1000 | * @return Query query instance to be further processed |
||
| 1001 | */ |
||
| 1002 | public function getQueryFromBuilder(QueryBuilder $qb) |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Helper method to add join selections. |
||
| 1011 | * |
||
| 1012 | * @return String Enhancement for select clause |
||
| 1013 | */ |
||
| 1014 | protected function addJoinsToSelection() |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Helper method to add joins to from clause. |
||
| 1023 | * |
||
| 1024 | * @param QueryBuilder $qb Query builder instance used to create the query |
||
| 1025 | * |
||
| 1026 | * @return QueryBuilder The query builder enriched by additional joins |
||
| 1027 | */ |
||
| 1028 | protected function addJoinsToFrom(QueryBuilder $qb) |
||
| 1033 | } |
||
| 1034 |