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 array */ |
||
| 39 | private $tables = [ |
||
| 40 | self::TABLE_CIRCLES, |
||
| 41 | self::TABLE_MEMBERS, |
||
| 42 | self::TABLE_GROUPS, |
||
| 43 | self::TABLE_SHARES, |
||
| 44 | self::TABLE_LINKS, |
||
| 45 | self::TABLE_TOKENS, |
||
| 46 | self::TABLE_GSEVENTS, |
||
| 47 | self::TABLE_GSSHARES |
||
| 48 | ]; |
||
| 49 | |||
| 50 | |||
| 51 | /** @var IDBConnection */ |
||
| 52 | protected $dbConnection; |
||
| 53 | |||
| 54 | /** @var IL10N */ |
||
| 55 | protected $l10n; |
||
| 56 | |||
| 57 | /** @var ConfigService */ |
||
| 58 | protected $configService; |
||
| 59 | |||
| 60 | /** @var TimezoneService */ |
||
| 61 | protected $timezoneService; |
||
| 62 | |||
| 63 | /** @var MiscService */ |
||
| 64 | protected $miscService; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | protected $default_select_alias; |
||
| 68 | |||
| 69 | /** @var bool */ |
||
| 70 | protected $leftJoinedNCGroupAndUser = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * RequestBuilder constructor. |
||
| 74 | * |
||
| 75 | * @param IL10N $l10n |
||
| 76 | * @param IDBConnection $connection |
||
| 77 | * @param ConfigService $configService |
||
| 78 | * @param TimezoneService $timezoneService |
||
| 79 | * @param MiscService $miscService |
||
| 80 | */ |
||
| 81 | public function __construct( |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Limit the request by its Id. |
||
| 95 | * |
||
| 96 | * @param IQueryBuilder $qb |
||
| 97 | * @param int $id |
||
| 98 | */ |
||
| 99 | protected function limitToId(IQueryBuilder &$qb, $id) { |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Limit the request by its UniqueId. |
||
| 106 | * |
||
| 107 | * @param IQueryBuilder $qb |
||
| 108 | * @param int $uniqueId |
||
| 109 | */ |
||
| 110 | protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Limit the request by its addressbookId. |
||
| 117 | * |
||
| 118 | * @param IQueryBuilder $qb |
||
| 119 | * @param int $bookId |
||
| 120 | */ |
||
| 121 | protected function limitToAddressBookId(IQueryBuilder &$qb, $bookId) { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Limit the request by its addressbookId. |
||
| 128 | * |
||
| 129 | * @param IQueryBuilder $qb |
||
| 130 | * @param string $groupName |
||
| 131 | */ |
||
| 132 | protected function limitToContactGroup(IQueryBuilder &$qb, $groupName) { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Limit the request to the Circle by its Id. |
||
| 139 | * |
||
| 140 | * @param IQueryBuilder $qb |
||
| 141 | * @param string $contactId |
||
| 142 | */ |
||
| 143 | protected function limitToContactId(IQueryBuilder &$qb, $contactId) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Limit the request by its Token. |
||
| 150 | * |
||
| 151 | * @param IQueryBuilder $qb |
||
| 152 | * @param string $token |
||
| 153 | */ |
||
| 154 | protected function limitToToken(IQueryBuilder &$qb, $token) { |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Limit the request to the User by its Id. |
||
| 161 | * |
||
| 162 | * @param IQueryBuilder $qb |
||
| 163 | * @param $userId |
||
| 164 | */ |
||
| 165 | protected function limitToUserId(IQueryBuilder &$qb, $userId) { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Limit the request to the owner |
||
| 172 | * |
||
| 173 | * @param IQueryBuilder $qb |
||
| 174 | * @param $owner |
||
| 175 | */ |
||
| 176 | protected function limitToOwner(IQueryBuilder &$qb, $owner) { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Limit the request to the Member by its Id. |
||
| 183 | * |
||
| 184 | * @param IQueryBuilder $qb |
||
| 185 | * @param string $memberId |
||
| 186 | */ |
||
| 187 | protected function limitToMemberId(IQueryBuilder &$qb, string $memberId) { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Limit the request to the Type entry. |
||
| 194 | * |
||
| 195 | * @param IQueryBuilder $qb |
||
| 196 | * @param int $type |
||
| 197 | */ |
||
| 198 | protected function limitToUserType(IQueryBuilder &$qb, $type) { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Limit the request to the Instance. |
||
| 205 | * |
||
| 206 | * @param IQueryBuilder $qb |
||
| 207 | * @param string $instance |
||
| 208 | */ |
||
| 209 | protected function limitToInstance(IQueryBuilder &$qb, string $instance) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Limit the request to the Circle by its Id. |
||
| 216 | * |
||
| 217 | * @param IQueryBuilder $qb |
||
| 218 | * @param string $circleUniqueId |
||
| 219 | */ |
||
| 220 | protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Limit the request to the Circle by its Id. |
||
| 227 | * |
||
| 228 | * @param IQueryBuilder $qb |
||
| 229 | * @param int $shareId |
||
| 230 | */ |
||
| 231 | protected function limitToShareId(IQueryBuilder &$qb, int $shareId) { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Limit the request to the Circle by its Shorten Unique Id. |
||
| 238 | * |
||
| 239 | * @param IQueryBuilder $qb |
||
| 240 | * @param string $circleUniqueId |
||
| 241 | * @param $length |
||
| 242 | */ |
||
| 243 | protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) { |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Limit the request to the Group by its Id. |
||
| 259 | * |
||
| 260 | * @param IQueryBuilder $qb |
||
| 261 | * @param int $groupId |
||
| 262 | */ |
||
| 263 | protected function limitToGroupId(IQueryBuilder &$qb, $groupId) { |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Limit the search by its Name |
||
| 270 | * |
||
| 271 | * @param IQueryBuilder $qb |
||
| 272 | * @param string $name |
||
| 273 | */ |
||
| 274 | protected function limitToName(IQueryBuilder &$qb, $name) { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Limit the search by its Status (or greater) |
||
| 281 | * |
||
| 282 | * @param IQueryBuilder $qb |
||
| 283 | * @param string $name |
||
| 284 | */ |
||
| 285 | protected function limitToStatus(IQueryBuilder &$qb, $name) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Limit the request by its Id. |
||
| 292 | * |
||
| 293 | * @param IQueryBuilder $qb |
||
| 294 | * @param string $type |
||
| 295 | */ |
||
| 296 | protected function limitToShareType(IQueryBuilder &$qb, string $type) { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Limit the request by its Id. |
||
| 303 | * |
||
| 304 | * @param IQueryBuilder $qb |
||
| 305 | * @param string $with |
||
| 306 | */ |
||
| 307 | protected function limitToShareWith(IQueryBuilder &$qb, string $with) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Limit the request to a minimum member level. |
||
| 314 | * |
||
| 315 | * if $pf is an array, will generate an SQL OR request to limit level in multiple tables |
||
| 316 | * |
||
| 317 | * @param IQueryBuilder $qb |
||
| 318 | * @param int $level |
||
| 319 | * @param string|array $pf |
||
| 320 | */ |
||
| 321 | protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') { |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * @param IQueryBuilder $qb |
||
| 342 | * @param array $pf |
||
| 343 | * |
||
| 344 | * @return mixed |
||
| 345 | */ |
||
| 346 | private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Limit the search to Members and Almost members |
||
| 363 | * |
||
| 364 | * @param IQueryBuilder $qb |
||
| 365 | */ |
||
| 366 | protected function limitToMembersAndAlmost(IQueryBuilder &$qb) { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @param IQueryBuilder $qb |
||
| 382 | * @param string $field |
||
| 383 | * @param string|integer $value |
||
| 384 | */ |
||
| 385 | private function limitToDBField(IQueryBuilder &$qb, $field, $value) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param IQueryBuilder $qb |
||
| 394 | * @param string $field |
||
| 395 | * @param string|integer $value |
||
| 396 | */ |
||
| 397 | private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) { |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * link to the groupId/UserId of the NC DB. |
||
| 406 | * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id' |
||
| 407 | * alias |
||
| 408 | * |
||
| 409 | * @param IQueryBuilder $qb |
||
| 410 | * @param string $userId |
||
| 411 | */ |
||
| 412 | protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * Right Join the Circles table |
||
| 431 | * |
||
| 432 | * @param IQueryBuilder $qb |
||
| 433 | * |
||
| 434 | * @deprecated not used (14/07/17) |
||
| 435 | */ |
||
| 436 | protected function rightJoinCircles(IQueryBuilder &$qb) { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Left Join circle table to get more information about the circle. |
||
| 454 | * |
||
| 455 | * @param IQueryBuilder $qb |
||
| 456 | */ |
||
| 457 | protected function leftJoinCircle(IQueryBuilder &$qb) { |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * link to the groupId/UserId of the NC DB. |
||
| 482 | * |
||
| 483 | * @param IQueryBuilder $qb |
||
| 484 | * @param string $userId |
||
| 485 | * @param string $field |
||
| 486 | */ |
||
| 487 | protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * |
||
| 517 | */ |
||
| 518 | public function cleanDatabase(): void { |
||
| 531 | |||
| 532 | } |
||
| 533 | |||
| 536 |