Complex classes like CoreRequestBuilder 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 CoreRequestBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class CoreRequestBuilder { |
||
| 23 | |||
| 24 | const TABLE_FILE_SHARES = 'share'; |
||
| 25 | const SHARE_TYPE = 7; |
||
| 26 | |||
| 27 | const TABLE_CIRCLES = 'circles_circles'; |
||
| 28 | const TABLE_MEMBERS = 'circles_members'; |
||
| 29 | const TABLE_GROUPS = 'circles_groups'; |
||
| 30 | const TABLE_SHARES = 'circles_shares'; |
||
| 31 | const TABLE_LINKS = 'circles_links'; |
||
| 32 | const TABLE_TOKENS = 'circles_tokens'; |
||
| 33 | const TABLE_GSEVENTS = 'circles_gsevents'; |
||
| 34 | const TABLE_GSSHARES = 'circles_gsshares'; |
||
| 35 | |||
| 36 | const NC_TABLE_GROUP_USER = 'group_user'; |
||
| 37 | |||
| 38 | /** @var IDBConnection */ |
||
| 39 | protected $dbConnection; |
||
| 40 | |||
| 41 | /** @var IL10N */ |
||
| 42 | protected $l10n; |
||
| 43 | |||
| 44 | /** @var ConfigService */ |
||
| 45 | protected $configService; |
||
| 46 | |||
| 47 | /** @var TimezoneService */ |
||
| 48 | protected $timezoneService; |
||
| 49 | |||
| 50 | /** @var MiscService */ |
||
| 51 | protected $miscService; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | protected $default_select_alias; |
||
| 55 | |||
| 56 | /** @var bool */ |
||
| 57 | protected $leftJoinedNCGroupAndUser = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * RequestBuilder constructor. |
||
| 61 | * |
||
| 62 | * @param IL10N $l10n |
||
| 63 | * @param IDBConnection $connection |
||
| 64 | * @param ConfigService $configService |
||
| 65 | * @param TimezoneService $timezoneService |
||
| 66 | * @param MiscService $miscService |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Limit the request by its Id. |
||
| 82 | * |
||
| 83 | * @param IQueryBuilder $qb |
||
| 84 | * @param int $id |
||
| 85 | */ |
||
| 86 | protected function limitToId(IQueryBuilder &$qb, $id) { |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Limit the request by its UniqueId. |
||
| 93 | * |
||
| 94 | * @param IQueryBuilder $qb |
||
| 95 | * @param int $uniqueId |
||
| 96 | */ |
||
| 97 | protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Limit the request by its addressbookId. |
||
| 104 | * |
||
| 105 | * @param IQueryBuilder $qb |
||
| 106 | * @param int $bookId |
||
| 107 | */ |
||
| 108 | protected function limitToAddressBookId(IQueryBuilder &$qb, $bookId) { |
||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Limit the request by its addressbookId. |
||
| 116 | * |
||
| 117 | * @param IQueryBuilder $qb |
||
| 118 | * @param string $groupName |
||
| 119 | */ |
||
| 120 | protected function limitToContactGroup(IQueryBuilder &$qb, $groupName) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Limit the request to the Circle by its Id. |
||
| 127 | * |
||
| 128 | * @param IQueryBuilder $qb |
||
| 129 | * @param string $contactId |
||
| 130 | */ |
||
| 131 | protected function limitToContactId(IQueryBuilder &$qb, $contactId) { |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Limit the request by its Token. |
||
| 138 | * |
||
| 139 | * @param IQueryBuilder $qb |
||
| 140 | * @param string $token |
||
| 141 | */ |
||
| 142 | protected function limitToToken(IQueryBuilder &$qb, $token) { |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Limit the request to the User by its Id. |
||
| 149 | * |
||
| 150 | * @param IQueryBuilder $qb |
||
| 151 | * @param $userId |
||
| 152 | */ |
||
| 153 | protected function limitToUserId(IQueryBuilder &$qb, $userId) { |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Limit the request to the Type entry. |
||
| 160 | * |
||
| 161 | * @param IQueryBuilder $qb |
||
| 162 | * @param int $type |
||
| 163 | */ |
||
| 164 | protected function limitToUserType(IQueryBuilder &$qb, $type) { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Limit the request to the Instance. |
||
| 171 | * |
||
| 172 | * @param IQueryBuilder $qb |
||
| 173 | * @param string $instance |
||
| 174 | */ |
||
| 175 | protected function limitToInstance(IQueryBuilder &$qb, string $instance) { |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Limit the request to the Circle by its Id. |
||
| 182 | * |
||
| 183 | * @param IQueryBuilder $qb |
||
| 184 | * @param string $circleUniqueId |
||
| 185 | */ |
||
| 186 | protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Limit the request to the Circle by its Id. |
||
| 193 | * |
||
| 194 | * @param IQueryBuilder $qb |
||
| 195 | * @param int $shareId |
||
| 196 | */ |
||
| 197 | protected function limitToShareId(IQueryBuilder &$qb, int $shareId) { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Limit the request to the Circle by its Id. |
||
| 204 | * |
||
| 205 | * @param IQueryBuilder $qb |
||
| 206 | * @param string $memberId |
||
| 207 | */ |
||
| 208 | protected function limitToMemberId(IQueryBuilder &$qb, string $memberId) { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Limit the request to the Circle by its Shorten Unique Id. |
||
| 215 | * |
||
| 216 | * @param IQueryBuilder $qb |
||
| 217 | * @param string $circleUniqueId |
||
| 218 | * @param $length |
||
| 219 | */ |
||
| 220 | protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Limit the request to the Group by its Id. |
||
| 236 | * |
||
| 237 | * @param IQueryBuilder $qb |
||
| 238 | * @param int $groupId |
||
| 239 | */ |
||
| 240 | protected function limitToGroupId(IQueryBuilder &$qb, $groupId) { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Limit the search by its Name |
||
| 247 | * |
||
| 248 | * @param IQueryBuilder $qb |
||
| 249 | * @param string $name |
||
| 250 | */ |
||
| 251 | protected function limitToName(IQueryBuilder &$qb, $name) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Limit the search by its Status (or greater) |
||
| 258 | * |
||
| 259 | * @param IQueryBuilder $qb |
||
| 260 | * @param string $name |
||
| 261 | */ |
||
| 262 | protected function limitToStatus(IQueryBuilder &$qb, $name) { |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Limit the request by its Id. |
||
| 269 | * |
||
| 270 | * @param IQueryBuilder $qb |
||
| 271 | * @param string $type |
||
| 272 | */ |
||
| 273 | protected function limitToShareType(IQueryBuilder &$qb, string $type) { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Limit the request by its Id. |
||
| 280 | * |
||
| 281 | * @param IQueryBuilder $qb |
||
| 282 | * @param string $with |
||
| 283 | */ |
||
| 284 | protected function limitToShareWith(IQueryBuilder &$qb, string $with) { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Limit the request to a minimum member level. |
||
| 291 | * |
||
| 292 | * if $pf is an array, will generate an SQL OR request to limit level in multiple tables |
||
| 293 | * |
||
| 294 | * @param IQueryBuilder $qb |
||
| 295 | * @param int $level |
||
| 296 | * @param string|array $pf |
||
| 297 | */ |
||
| 298 | protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param IQueryBuilder $qb |
||
| 319 | * @param array $pf |
||
| 320 | * |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * Limit the search to Members and Almost members |
||
| 340 | * |
||
| 341 | * @param IQueryBuilder $qb |
||
| 342 | */ |
||
| 343 | protected function limitToMembersAndAlmost(IQueryBuilder &$qb) { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * @param IQueryBuilder $qb |
||
| 359 | * @param string $field |
||
| 360 | * @param string|integer $value |
||
| 361 | */ |
||
| 362 | private function limitToDBField(IQueryBuilder &$qb, $field, $value) { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param IQueryBuilder $qb |
||
| 371 | * @param string $field |
||
| 372 | * @param string|integer $value |
||
| 373 | */ |
||
| 374 | private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * link to the groupId/UserId of the NC DB. |
||
| 383 | * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id' |
||
| 384 | * alias |
||
| 385 | * |
||
| 386 | * @param IQueryBuilder $qb |
||
| 387 | * @param string $userId |
||
| 388 | */ |
||
| 389 | protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * Right Join the Circles table |
||
| 408 | * |
||
| 409 | * @param IQueryBuilder $qb |
||
| 410 | * |
||
| 411 | * @deprecated not used (14/07/17) |
||
| 412 | */ |
||
| 413 | protected function rightJoinCircles(IQueryBuilder &$qb) { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * Left Join circle table to get more information about the circle. |
||
| 431 | * |
||
| 432 | * @param IQueryBuilder $qb |
||
| 433 | */ |
||
| 434 | protected function leftJoinCircle(IQueryBuilder &$qb) { |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * link to the groupId/UserId of the NC DB. |
||
| 459 | * |
||
| 460 | * @param IQueryBuilder $qb |
||
| 461 | * @param string $userId |
||
| 462 | * @param string $field |
||
| 463 | */ |
||
| 464 | protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) { |
||
| 490 | } |
||
| 491 | |||
| 494 |