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 | const TABLE_GSSHARES_MOUNTPOINT = 'circles_gsshares_mp'; |
||
| 36 | |||
| 37 | const NC_TABLE_GROUP_USER = 'group_user'; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | private $tables = [ |
||
| 41 | self::TABLE_CIRCLES, |
||
| 42 | self::TABLE_MEMBERS, |
||
| 43 | self::TABLE_GROUPS, |
||
| 44 | self::TABLE_SHARES, |
||
| 45 | self::TABLE_LINKS, |
||
| 46 | self::TABLE_TOKENS, |
||
| 47 | self::TABLE_GSEVENTS, |
||
| 48 | self::TABLE_GSSHARES, |
||
| 49 | self::TABLE_GSSHARES_MOUNTPOINT |
||
| 50 | ]; |
||
| 51 | |||
| 52 | |||
| 53 | /** @var IDBConnection */ |
||
| 54 | protected $dbConnection; |
||
| 55 | |||
| 56 | /** @var IL10N */ |
||
| 57 | protected $l10n; |
||
| 58 | |||
| 59 | /** @var ConfigService */ |
||
| 60 | protected $configService; |
||
| 61 | |||
| 62 | /** @var TimezoneService */ |
||
| 63 | protected $timezoneService; |
||
| 64 | |||
| 65 | /** @var MiscService */ |
||
| 66 | protected $miscService; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | protected $default_select_alias; |
||
| 70 | |||
| 71 | /** @var bool */ |
||
| 72 | protected $leftJoinedNCGroupAndUser = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * RequestBuilder constructor. |
||
| 76 | * |
||
| 77 | * @param IL10N $l10n |
||
| 78 | * @param IDBConnection $connection |
||
| 79 | * @param ConfigService $configService |
||
| 80 | * @param TimezoneService $timezoneService |
||
| 81 | * @param MiscService $miscService |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Limit the request by its Id. |
||
| 97 | * |
||
| 98 | * @param IQueryBuilder $qb |
||
| 99 | * @param int $id |
||
| 100 | */ |
||
| 101 | protected function limitToId(IQueryBuilder &$qb, $id) { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Limit the request by its UniqueId. |
||
| 108 | * |
||
| 109 | * @param IQueryBuilder $qb |
||
| 110 | * @param int $uniqueId |
||
| 111 | */ |
||
| 112 | protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Limit the request by its addressbookId. |
||
| 119 | * |
||
| 120 | * @param IQueryBuilder $qb |
||
| 121 | * @param int $bookId |
||
| 122 | */ |
||
| 123 | protected function limitToAddressBookId(IQueryBuilder &$qb, $bookId) { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Limit the request by its addressbookId. |
||
| 130 | * |
||
| 131 | * @param IQueryBuilder $qb |
||
| 132 | * @param string $groupName |
||
| 133 | */ |
||
| 134 | protected function limitToContactGroup(IQueryBuilder &$qb, $groupName) { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Limit the request to the Circle by its Id. |
||
| 141 | * |
||
| 142 | * @param IQueryBuilder $qb |
||
| 143 | * @param string $contactId |
||
| 144 | */ |
||
| 145 | protected function limitToContactId(IQueryBuilder &$qb, $contactId) { |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Limit the request by its Token. |
||
| 152 | * |
||
| 153 | * @param IQueryBuilder $qb |
||
| 154 | * @param string $token |
||
| 155 | */ |
||
| 156 | protected function limitToToken(IQueryBuilder &$qb, $token) { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Limit the request to the User by its Id. |
||
| 163 | * |
||
| 164 | * @param IQueryBuilder $qb |
||
| 165 | * @param $userId |
||
| 166 | */ |
||
| 167 | protected function limitToUserId(IQueryBuilder &$qb, $userId) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Limit the request to the owner |
||
| 174 | * |
||
| 175 | * @param IQueryBuilder $qb |
||
| 176 | * @param $owner |
||
| 177 | */ |
||
| 178 | protected function limitToOwner(IQueryBuilder &$qb, $owner) { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Limit the request to the Member by its Id. |
||
| 185 | * |
||
| 186 | * @param IQueryBuilder $qb |
||
| 187 | * @param string $memberId |
||
| 188 | */ |
||
| 189 | protected function limitToMemberId(IQueryBuilder &$qb, string $memberId) { |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Limit the request to the Type entry. |
||
| 196 | * |
||
| 197 | * @param IQueryBuilder $qb |
||
| 198 | * @param int $type |
||
| 199 | */ |
||
| 200 | protected function limitToUserType(IQueryBuilder &$qb, $type) { |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Limit the request to the Instance. |
||
| 207 | * |
||
| 208 | * @param IQueryBuilder $qb |
||
| 209 | * @param string $instance |
||
| 210 | */ |
||
| 211 | protected function limitToInstance(IQueryBuilder &$qb, string $instance) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Limit the request to the Circle by its Id. |
||
| 218 | * |
||
| 219 | * @param IQueryBuilder $qb |
||
| 220 | * @param string $circleUniqueId |
||
| 221 | */ |
||
| 222 | protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) { |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Limit the request to the Circle by its Id. |
||
| 229 | * |
||
| 230 | * @param IQueryBuilder $qb |
||
| 231 | * @param int $shareId |
||
| 232 | */ |
||
| 233 | protected function limitToShareId(IQueryBuilder &$qb, int $shareId) { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Limit the request to the Circle by its Id. |
||
| 240 | * |
||
| 241 | * @param IQueryBuilder $qb |
||
| 242 | * @param string $mountpoint |
||
| 243 | */ |
||
| 244 | protected function limitToMountpoint(IQueryBuilder &$qb, string $mountpoint) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Limit the request to the Circle by its Id. |
||
| 250 | * |
||
| 251 | * @param IQueryBuilder $qb |
||
| 252 | * @param string $hash |
||
| 253 | */ |
||
| 254 | protected function limitToMountpointHash(IQueryBuilder &$qb, string $hash) { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Limit the request to the Circle by its Shorten Unique Id. |
||
| 261 | * |
||
| 262 | * @param IQueryBuilder $qb |
||
| 263 | * @param string $circleUniqueId |
||
| 264 | * @param $length |
||
| 265 | */ |
||
| 266 | protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Limit the request to the Group by its Id. |
||
| 282 | * |
||
| 283 | * @param IQueryBuilder $qb |
||
| 284 | * @param int $groupId |
||
| 285 | */ |
||
| 286 | protected function limitToGroupId(IQueryBuilder &$qb, $groupId) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Limit the search by its Name |
||
| 293 | * |
||
| 294 | * @param IQueryBuilder $qb |
||
| 295 | * @param string $name |
||
| 296 | */ |
||
| 297 | protected function limitToName(IQueryBuilder &$qb, $name) { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Limit the search by its Status (or greater) |
||
| 304 | * |
||
| 305 | * @param IQueryBuilder $qb |
||
| 306 | * @param string $name |
||
| 307 | */ |
||
| 308 | protected function limitToStatus(IQueryBuilder &$qb, $name) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Limit the request by its Id. |
||
| 315 | * |
||
| 316 | * @param IQueryBuilder $qb |
||
| 317 | * @param string $type |
||
| 318 | */ |
||
| 319 | protected function limitToShareType(IQueryBuilder &$qb, string $type) { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Limit the request by its Id. |
||
| 326 | * |
||
| 327 | * @param IQueryBuilder $qb |
||
| 328 | * @param string $with |
||
| 329 | */ |
||
| 330 | protected function limitToShareWith(IQueryBuilder &$qb, string $with) { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Limit the request to a minimum member level. |
||
| 337 | * |
||
| 338 | * if $pf is an array, will generate an SQL OR request to limit level in multiple tables |
||
| 339 | * |
||
| 340 | * @param IQueryBuilder $qb |
||
| 341 | * @param int $level |
||
| 342 | * @param string|array $pf |
||
| 343 | */ |
||
| 344 | protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') { |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * @param IQueryBuilder $qb |
||
| 365 | * @param array $pf |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Limit the search to Members and Almost members |
||
| 386 | * |
||
| 387 | * @param IQueryBuilder $qb |
||
| 388 | */ |
||
| 389 | protected function limitToMembersAndAlmost(IQueryBuilder &$qb) { |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @param IQueryBuilder $qb |
||
| 405 | * @param string $field |
||
| 406 | * @param string|integer $value |
||
| 407 | */ |
||
| 408 | private function limitToDBField(IQueryBuilder &$qb, $field, $value) { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * @param IQueryBuilder $qb |
||
| 417 | * @param string $field |
||
| 418 | * @param string|integer $value |
||
| 419 | */ |
||
| 420 | private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * link to the groupId/UserId of the NC DB. |
||
| 429 | * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id' |
||
| 430 | * alias |
||
| 431 | * |
||
| 432 | * @param IQueryBuilder $qb |
||
| 433 | * @param string $userId |
||
| 434 | */ |
||
| 435 | protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Right Join the Circles table |
||
| 454 | * |
||
| 455 | * @param IQueryBuilder $qb |
||
| 456 | * |
||
| 457 | * @deprecated not used (14/07/17) |
||
| 458 | */ |
||
| 459 | protected function rightJoinCircles(IQueryBuilder &$qb) { |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Left Join circle table to get more information about the circle. |
||
| 477 | * |
||
| 478 | * @param IQueryBuilder $qb |
||
| 479 | */ |
||
| 480 | protected function leftJoinCircle(IQueryBuilder &$qb) { |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * link to the groupId/UserId of the NC DB. |
||
| 505 | * |
||
| 506 | * @param IQueryBuilder $qb |
||
| 507 | * @param string $userId |
||
| 508 | * @param string $field |
||
| 509 | */ |
||
| 510 | protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) { |
||
| 536 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * |
||
| 540 | */ |
||
| 541 | public function cleanDatabase(): void { |
||
| 554 | |||
| 555 | } |
||
| 556 | |||
| 559 |