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 | * Adds a filter for the createdBy field. |
||
| 483 | * |
||
| 484 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 485 | * @param integer $userId The user identifier used for filtering (optional) |
||
| 486 | * |
||
| 487 | * @return QueryBuilder Enriched query builder instance |
||
| 488 | */ |
||
| 489 | public function addCreatorFilter(QueryBuilder $qb, $userId = null) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns query builder for selecting a list of objects with a given where clause. |
||
| 509 | * |
||
| 510 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 511 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 512 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 513 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 514 | * |
||
| 515 | * @return QueryBuilder query builder for the given arguments |
||
| 516 | */ |
||
| 517 | public function getListQueryBuilder($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Selects a list of objects with a given where clause. |
||
| 529 | * |
||
| 530 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 531 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 532 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 533 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 534 | * |
||
| 535 | * @return ArrayCollection collection containing retrieved routeEntity instances |
||
| 536 | */ |
||
| 537 | public function selectWhere($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns query builder instance for retrieving a list of objects with a given where clause and pagination parameters. |
||
| 548 | * |
||
| 549 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 550 | * @param integer $currentPage Where to start selection |
||
| 551 | * @param integer $resultsPerPage Amount of items to select |
||
| 552 | * |
||
| 553 | * @return Query Created query instance |
||
| 554 | */ |
||
| 555 | public function getSelectWherePaginatedQuery(QueryBuilder $qb, $currentPage = 1, $resultsPerPage = 25) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Selects a list of objects with a given where clause and pagination parameters. |
||
| 570 | * |
||
| 571 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 572 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 573 | * @param integer $currentPage Where to start selection |
||
| 574 | * @param integer $resultsPerPage Amount of items to select |
||
| 575 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 576 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 577 | * |
||
| 578 | * @return array with retrieved collection and amount of total records affected by this query |
||
| 579 | */ |
||
| 580 | View Code Duplication | public function selectWherePaginated($where = '', $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true, $slimMode = false) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Adds quick navigation related filter options as where clauses. |
||
| 593 | * |
||
| 594 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 595 | * |
||
| 596 | * @return QueryBuilder Enriched query builder instance |
||
| 597 | */ |
||
| 598 | public function addCommonViewFilters(QueryBuilder $qb) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Adds default filters as where clauses. |
||
| 648 | * |
||
| 649 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 650 | * @param array $parameters List of determined filter options |
||
| 651 | * |
||
| 652 | * @return QueryBuilder Enriched query builder instance |
||
| 653 | */ |
||
| 654 | protected function applyDefaultFilters(QueryBuilder $qb, $parameters = []) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Selects entities by a given search fragment. |
||
| 662 | * |
||
| 663 | * @param string $fragment The fragment to search for |
||
| 664 | * @param array $exclude List with identifiers to be excluded from search |
||
| 665 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 666 | * @param integer $currentPage Where to start selection |
||
| 667 | * @param integer $resultsPerPage Amount of items to select |
||
| 668 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 669 | * |
||
| 670 | * @return array with retrieved collection and amount of total records affected by this query |
||
| 671 | */ |
||
| 672 | public function selectSearch($fragment = '', $exclude = [], $orderBy = '', $currentPage = 1, $resultsPerPage = 25, $useJoins = true) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Adds where clause for search query. |
||
| 688 | * |
||
| 689 | * @param QueryBuilder $qb Query builder to be enhanced |
||
| 690 | * @param string $fragment The fragment to search for |
||
| 691 | * |
||
| 692 | * @return QueryBuilder Enriched query builder instance |
||
| 693 | */ |
||
| 694 | protected function addSearchFilter(QueryBuilder $qb, $fragment = '') |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Performs a given database selection and post-processed the results. |
||
| 770 | * |
||
| 771 | * @param Query $query The Query instance to be executed |
||
| 772 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 773 | * @param boolean $isPaginated Whether the given query uses a paginator or not (optional) (default=false) |
||
| 774 | * |
||
| 775 | * @return array with retrieved collection and (for paginated queries) the amount of total records affected |
||
| 776 | */ |
||
| 777 | public function retrieveCollectionResult(Query $query, $orderBy = '', $isPaginated = false) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Returns query builder instance for a count query. |
||
| 798 | * |
||
| 799 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 800 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 801 | * |
||
| 802 | * @return QueryBuilder Created query builder instance |
||
| 803 | * @TODO fix usage of joins; please remove the first line and test |
||
| 804 | */ |
||
| 805 | protected function getCountQuery($where = '', $useJoins = true) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Selects entity count with a given where clause. |
||
| 829 | * |
||
| 830 | * @param string $where The where clause to use when retrieving the object count (optional) (default='') |
||
| 831 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 832 | * @param array $parameters List of determined filter options |
||
| 833 | * |
||
| 834 | * @return integer amount of affected records |
||
| 835 | */ |
||
| 836 | public function selectCount($where = '', $useJoins = true, $parameters = []) |
||
| 846 | |||
| 847 | |||
| 848 | /** |
||
| 849 | * Checks for unique values. |
||
| 850 | * |
||
| 851 | * @param string $fieldName The name of the property to be checked |
||
| 852 | * @param string $fieldValue The value of the property to be checked |
||
| 853 | * @param int $excludeId Id of routes to exclude (optional) |
||
| 854 | * |
||
| 855 | * @return boolean result of this check, true if the given route does not already exist |
||
| 856 | */ |
||
| 857 | public function detectUniqueState($fieldName, $fieldValue, $excludeId = 0) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Builds a generic Doctrine query supporting WHERE and ORDER BY. |
||
| 874 | * |
||
| 875 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 876 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 877 | * @param boolean $useJoins Whether to include joining related objects (optional) (default=true) |
||
| 878 | * @param boolean $slimMode If activated only some basic fields are selected without using any joins (optional) (default=false) |
||
| 879 | * |
||
| 880 | * @return QueryBuilder query builder instance to be further processed |
||
| 881 | */ |
||
| 882 | public function genericBaseQuery($where = '', $orderBy = '', $useJoins = true, $slimMode = false) |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Adds WHERE clause to given query builder. |
||
| 921 | * |
||
| 922 | * @param QueryBuilder $qb Given query builder instance |
||
| 923 | * @param string $where The where clause to use when retrieving the collection (optional) (default='') |
||
| 924 | * |
||
| 925 | * @return QueryBuilder query builder instance to be further processed |
||
| 926 | */ |
||
| 927 | protected function genericBaseQueryAddWhere(QueryBuilder $qb, $where = '') |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Adds ORDER BY clause to given query builder. |
||
| 993 | * |
||
| 994 | * @param QueryBuilder $qb Given query builder instance |
||
| 995 | * @param string $orderBy The order-by clause to use when retrieving the collection (optional) (default='') |
||
| 996 | * |
||
| 997 | * @return QueryBuilder query builder instance to be further processed |
||
| 998 | */ |
||
| 999 | protected function genericBaseQueryAddOrderBy(QueryBuilder $qb, $orderBy = '') |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Retrieves Doctrine query from query builder, applying FilterUtil and other common actions. |
||
| 1033 | * |
||
| 1034 | * @param QueryBuilder $qb Query builder instance |
||
| 1035 | * |
||
| 1036 | * @return Query query instance to be further processed |
||
| 1037 | */ |
||
| 1038 | public function getQueryFromBuilder(QueryBuilder $qb) |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Helper method to add join selections. |
||
| 1047 | * |
||
| 1048 | * @return String Enhancement for select clause |
||
| 1049 | */ |
||
| 1050 | protected function addJoinsToSelection() |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Helper method to add joins to from clause. |
||
| 1059 | * |
||
| 1060 | * @param QueryBuilder $qb Query builder instance used to create the query |
||
| 1061 | * |
||
| 1062 | * @return QueryBuilder The query builder enriched by additional joins |
||
| 1063 | */ |
||
| 1064 | protected function addJoinsToFrom(QueryBuilder $qb) |
||
| 1069 | } |
||
| 1070 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.