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:
| 1 | <?php |
||
| 41 | class CircleProviderRequestBuilder extends CoreRequestBuilder { |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * returns the SQL request to get a specific share from the fileId and circleId |
||
| 46 | * |
||
| 47 | * @param int $fileId |
||
| 48 | * @param int $circleId |
||
| 49 | * |
||
| 50 | * @return IQueryBuilder |
||
| 51 | */ |
||
| 52 | protected function findShareParentSql($fileId, $circleId) { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Limit the request to the given Circles. |
||
| 65 | * |
||
| 66 | * @param IQueryBuilder $qb |
||
| 67 | * @param array $circleUniqueIds |
||
| 68 | */ |
||
| 69 | View Code Duplication | protected function limitToCircles(IQueryBuilder &$qb, $circleUniqueIds) { |
|
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Limit the request to the Share by its Id. |
||
| 88 | * |
||
| 89 | * @param IQueryBuilder $qb |
||
| 90 | * @param $shareId |
||
| 91 | */ |
||
| 92 | protected function limitToShare(IQueryBuilder &$qb, $shareId) { |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Limit the request to the top share (no children) |
||
| 102 | * |
||
| 103 | * @param IQueryBuilder $qb |
||
| 104 | */ |
||
| 105 | protected function limitToShareParent(IQueryBuilder &$qb) { |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * limit the request to the children of a share |
||
| 114 | * |
||
| 115 | * @param IQueryBuilder $qb |
||
| 116 | * @param $userId |
||
| 117 | * @param int $parentId |
||
| 118 | */ |
||
| 119 | protected function limitToShareChildren(IQueryBuilder &$qb, $userId, $parentId = -1) { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * limit the request to the share itself AND its children. |
||
| 133 | * perfect if you want to delete everything related to a share |
||
| 134 | * |
||
| 135 | * @param IQueryBuilder $qb |
||
| 136 | * @param $circleId |
||
| 137 | */ |
||
| 138 | protected function limitToShareAndChildren(IQueryBuilder &$qb, $circleId) { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * limit the request to a fileId. |
||
| 154 | * |
||
| 155 | * @param IQueryBuilder $qb |
||
| 156 | * @param $files |
||
| 157 | */ |
||
| 158 | View Code Duplication | protected function limitToFiles(IQueryBuilder &$qb, $files) { |
|
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @param IQueryBuilder $qb |
||
| 177 | * @param int $limit |
||
| 178 | * @param int $offset |
||
| 179 | */ |
||
| 180 | protected function limitToPage(IQueryBuilder &$qb, $limit = -1, $offset = 0) { |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * limit the request to a userId |
||
| 191 | * |
||
| 192 | * @param IQueryBuilder $qb |
||
| 193 | * @param string $userId |
||
| 194 | * @param bool $reShares |
||
| 195 | */ |
||
| 196 | protected function limitToShareOwner(IQueryBuilder &$qb, $userId, $reShares = false) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * link circle field |
||
| 216 | * |
||
| 217 | * @deprecated |
||
| 218 | * |
||
| 219 | * @param IQueryBuilder $qb |
||
| 220 | * @param int $shareId |
||
| 221 | */ |
||
| 222 | protected function linkCircleField(IQueryBuilder &$qb, $shareId = -1) { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param IQueryBuilder $qb |
||
| 250 | */ |
||
| 251 | View Code Duplication | protected function linkToCircleOwner(IQueryBuilder &$qb) { |
|
| 252 | $expr = $qb->expr(); |
||
| 253 | |||
| 254 | $qb->selectAlias('mo.user_id', 'circle_owner'); |
||
| 255 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 256 | $qb->leftJoin( |
||
| 257 | 'c', CoreRequestBuilder::TABLE_MEMBERS, 'mo', $expr->andX( |
||
| 258 | $expr->eq( |
||
| 259 | 'mo.circle_id', |
||
| 260 | $qb->createFunction('SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')') |
||
| 261 | ), $expr->eq('mo.user_type', $qb->createNamedParameter(Member::TYPE_USER)), |
||
| 262 | $expr->eq('mo.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Link to member (userId) of circle |
||
| 270 | * |
||
| 271 | * @param IQueryBuilder $qb |
||
| 272 | * @param string $userId |
||
| 273 | * @param bool $groupMemberAllowed |
||
| 274 | */ |
||
| 275 | protected function linkToMember(IQueryBuilder &$qb, $userId, $groupMemberAllowed) { |
||
| 276 | $expr = $qb->expr(); |
||
| 277 | |||
| 278 | $qb->from(CoreRequestBuilder::TABLE_MEMBERS, 'm'); |
||
| 279 | |||
| 280 | $orX = $expr->orX(); |
||
| 281 | $orX->add($this->exprLinkToMemberAsCircleMember($qb, $userId)); |
||
| 282 | if ($groupMemberAllowed === true) { |
||
| 283 | $orX->add($this->exprLinkToMemberAsGroupMember($qb, $userId)); |
||
| 284 | } |
||
| 285 | |||
| 286 | $qb->andWhere($orX); |
||
| 287 | |||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * generate CompositeExpression to link to a Member as a Real Circle Member |
||
| 293 | * |
||
| 294 | * @param IQueryBuilder $qb |
||
| 295 | * @param string $userId |
||
| 296 | * |
||
| 297 | * @return \OCP\DB\QueryBuilder\ICompositeExpression |
||
| 298 | */ |
||
| 299 | private function exprLinkToMemberAsCircleMember(IQueryBuilder &$qb, $userId) { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * generate CompositeExpression to link to a Member as a Group Member (core NC) |
||
| 322 | * |
||
| 323 | * @param IQueryBuilder $qb |
||
| 324 | * @param string $userId |
||
| 325 | * |
||
| 326 | * @return \OCP\DB\QueryBuilder\ICompositeExpression |
||
| 327 | */ |
||
| 328 | View Code Duplication | private function exprLinkToMemberAsGroupMember(IQueryBuilder &$qb, $userId) { |
|
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Link to all members of circle |
||
| 353 | * |
||
| 354 | * @param IQueryBuilder $qb |
||
| 355 | */ |
||
| 356 | protected function joinCircleMembers(IQueryBuilder &$qb) { |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Link to storage/filecache |
||
| 373 | * |
||
| 374 | * @param IQueryBuilder $qb |
||
| 375 | * @param string $userId |
||
| 376 | */ |
||
| 377 | protected function linkToFileCache(IQueryBuilder &$qb, $userId) { |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * add share to the database and return the ID |
||
| 399 | * |
||
| 400 | * @param IShare $share |
||
| 401 | * |
||
| 402 | * @return IQueryBuilder |
||
| 403 | */ |
||
| 404 | protected function getBaseInsertSql($share) { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * generate and return a base sql request. |
||
| 425 | * |
||
| 426 | * @param int $shareId |
||
| 427 | * |
||
| 428 | * @return IQueryBuilder |
||
| 429 | */ |
||
| 430 | protected function getBaseSelectSql($shareId = -1) { |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * Generate and return a base sql request |
||
| 452 | * This one should be used to retrieve a complete list of users (ie. access list). |
||
| 453 | * |
||
| 454 | * @return IQueryBuilder |
||
| 455 | */ |
||
| 456 | protected function getAccessListBaseSelectSql() { |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @return IQueryBuilder |
||
| 469 | */ |
||
| 470 | protected function getCompleteSelectSql() { |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * @param IQueryBuilder $qb |
||
| 495 | */ |
||
| 496 | private function joinShare(IQueryBuilder &$qb) { |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * generate and return a base sql request. |
||
| 515 | * |
||
| 516 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 517 | */ |
||
| 518 | View Code Duplication | protected function getBaseDeleteSql() { |
|
| 527 | |||
| 528 | |||
| 529 | /** |
||
| 530 | * generate and return a base sql request. |
||
| 531 | * |
||
| 532 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 533 | */ |
||
| 534 | View Code Duplication | protected function getBaseUpdateSql() { |
|
| 543 | } |
||
| 544 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.