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 | |||
| 34 | const NC_TABLE_GROUP_USER = 'group_user'; |
||
| 35 | |||
| 36 | /** @var IDBConnection */ |
||
| 37 | protected $dbConnection; |
||
| 38 | |||
| 39 | /** @var IL10N */ |
||
| 40 | protected $l10n; |
||
| 41 | |||
| 42 | /** @var ConfigService */ |
||
| 43 | protected $configService; |
||
| 44 | |||
| 45 | /** @var TimezoneService */ |
||
| 46 | protected $timezoneService; |
||
| 47 | |||
| 48 | /** @var MiscService */ |
||
| 49 | protected $miscService; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | protected $default_select_alias; |
||
| 53 | |||
| 54 | /** @var bool */ |
||
| 55 | protected $leftJoinedNCGroupAndUser = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * RequestBuilder constructor. |
||
| 59 | * |
||
| 60 | * @param IL10N $l10n |
||
| 61 | * @param IDBConnection $connection |
||
| 62 | * @param ConfigService $configService |
||
| 63 | * @param TimezoneService $timezoneService |
||
| 64 | * @param MiscService $miscService |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Limit the request by its Id. |
||
| 80 | * |
||
| 81 | * @param IQueryBuilder $qb |
||
| 82 | * @param int $id |
||
| 83 | */ |
||
| 84 | protected function limitToId(IQueryBuilder &$qb, $id) { |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Limit the request by its UniqueId. |
||
| 91 | * |
||
| 92 | * @param IQueryBuilder $qb |
||
| 93 | * @param int $uniqueId |
||
| 94 | */ |
||
| 95 | protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Limit the request by its addressbookId. |
||
| 102 | * |
||
| 103 | * @param IQueryBuilder $qb |
||
| 104 | * @param int $bookId |
||
| 105 | */ |
||
| 106 | protected function limitToAddressBookId(IQueryBuilder &$qb, $bookId) { |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Limit the request by its addressbookId. |
||
| 114 | * |
||
| 115 | * @param IQueryBuilder $qb |
||
| 116 | * @param string $groupName |
||
| 117 | */ |
||
| 118 | protected function limitToContactGroup(IQueryBuilder &$qb, $groupName) { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Limit the request to the Circle by its Id. |
||
| 125 | * |
||
| 126 | * @param IQueryBuilder $qb |
||
| 127 | * @param string $contactId |
||
| 128 | */ |
||
| 129 | protected function limitToContactId(IQueryBuilder &$qb, $contactId) { |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Limit the request by its Token. |
||
| 136 | * |
||
| 137 | * @param IQueryBuilder $qb |
||
| 138 | * @param string $token |
||
| 139 | */ |
||
| 140 | protected function limitToToken(IQueryBuilder &$qb, $token) { |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Limit the request to the User by its Id. |
||
| 147 | * |
||
| 148 | * @param IQueryBuilder $qb |
||
| 149 | * @param $userId |
||
| 150 | */ |
||
| 151 | protected function limitToUserId(IQueryBuilder &$qb, $userId) { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Limit the request to the Type entry. |
||
| 158 | * |
||
| 159 | * @param IQueryBuilder $qb |
||
| 160 | * @param int $type |
||
| 161 | */ |
||
| 162 | protected function limitToUserType(IQueryBuilder &$qb, $type) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Limit the request to the Circle by its Id. |
||
| 169 | * |
||
| 170 | * @param IQueryBuilder $qb |
||
| 171 | * @param string $circleUniqueId |
||
| 172 | */ |
||
| 173 | protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) { |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Limit the request to the Circle by its Id. |
||
| 180 | * |
||
| 181 | * @param IQueryBuilder $qb |
||
| 182 | * @param int $shareId |
||
| 183 | */ |
||
| 184 | protected function limitToShareId(IQueryBuilder &$qb, int $shareId) { |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Limit the request to the Circle by its Shorten Unique Id. |
||
| 191 | * |
||
| 192 | * @param IQueryBuilder $qb |
||
| 193 | * @param string $circleUniqueId |
||
| 194 | * @param $length |
||
| 195 | */ |
||
| 196 | protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Limit the request to the Group by its Id. |
||
| 212 | * |
||
| 213 | * @param IQueryBuilder $qb |
||
| 214 | * @param int $groupId |
||
| 215 | */ |
||
| 216 | protected function limitToGroupId(IQueryBuilder &$qb, $groupId) { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Limit the search by its Name |
||
| 223 | * |
||
| 224 | * @param IQueryBuilder $qb |
||
| 225 | * @param string $name |
||
| 226 | */ |
||
| 227 | protected function limitToName(IQueryBuilder &$qb, $name) { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * Limit the search by its Status (or greater) |
||
| 234 | * |
||
| 235 | * @param IQueryBuilder $qb |
||
| 236 | * @param string $name |
||
| 237 | */ |
||
| 238 | protected function limitToStatus(IQueryBuilder &$qb, $name) { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Limit the request by its Id. |
||
| 245 | * |
||
| 246 | * @param IQueryBuilder $qb |
||
| 247 | * @param string $type |
||
| 248 | */ |
||
| 249 | protected function limitToShareType(IQueryBuilder &$qb, string $type) { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Limit the request by its Id. |
||
| 256 | * |
||
| 257 | * @param IQueryBuilder $qb |
||
| 258 | * @param string $with |
||
| 259 | */ |
||
| 260 | protected function limitToShareWith(IQueryBuilder &$qb, string $with) { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Limit the request to a minimum member level. |
||
| 267 | * |
||
| 268 | * if $pf is an array, will generate an SQL OR request to limit level in multiple tables |
||
| 269 | * |
||
| 270 | * @param IQueryBuilder $qb |
||
| 271 | * @param int $level |
||
| 272 | * @param string|array $pf |
||
| 273 | */ |
||
| 274 | protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param IQueryBuilder $qb |
||
| 295 | * @param array $pf |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Limit the search to Members and Almost members |
||
| 316 | * |
||
| 317 | * @param IQueryBuilder $qb |
||
| 318 | */ |
||
| 319 | protected function limitToMembersAndAlmost(IQueryBuilder &$qb) { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * @param IQueryBuilder $qb |
||
| 335 | * @param string $field |
||
| 336 | * @param string|integer $value |
||
| 337 | */ |
||
| 338 | private function limitToDBField(IQueryBuilder &$qb, $field, $value) { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * @param IQueryBuilder $qb |
||
| 347 | * @param string $field |
||
| 348 | * @param string|integer $value |
||
| 349 | */ |
||
| 350 | private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * link to the groupId/UserId of the NC DB. |
||
| 359 | * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id' |
||
| 360 | * alias |
||
| 361 | * |
||
| 362 | * @param IQueryBuilder $qb |
||
| 363 | * @param string $userId |
||
| 364 | */ |
||
| 365 | protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') { |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Right Join the Circles table |
||
| 384 | * |
||
| 385 | * @param IQueryBuilder $qb |
||
| 386 | * |
||
| 387 | * @deprecated not used (14/07/17) |
||
| 388 | */ |
||
| 389 | protected function rightJoinCircles(IQueryBuilder &$qb) { |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Left Join circle table to get more information about the circle. |
||
| 407 | * |
||
| 408 | * @param IQueryBuilder $qb |
||
| 409 | */ |
||
| 410 | protected function leftJoinCircle(IQueryBuilder &$qb) { |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * link to the groupId/UserId of the NC DB. |
||
| 435 | * |
||
| 436 | * @param IQueryBuilder $qb |
||
| 437 | * @param string $userId |
||
| 438 | * @param string $field |
||
| 439 | */ |
||
| 440 | protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) { |
||
| 466 | } |
||
| 467 | |||
| 470 |