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 |
||
| 39 | class CircleProviderRequestBuilder { |
||
| 40 | |||
| 41 | |||
| 42 | /** @var IDBConnection */ |
||
| 43 | protected $dbConnection; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * returns the SQL request to get a specific share from the fileId and circleId |
||
| 48 | * |
||
| 49 | * @param int $fileId |
||
| 50 | * @param int $circleId |
||
| 51 | * |
||
| 52 | * @return IQueryBuilder |
||
| 53 | */ |
||
| 54 | protected function findShareParentSql($fileId, $circleId) { |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Limit the request to a Circle. |
||
| 67 | * |
||
| 68 | * @param IQueryBuilder $qb |
||
| 69 | * @param int $circleId |
||
| 70 | */ |
||
| 71 | protected function limitToCircle(& $qb, $circleId) { |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Limit the request to the Share by its Id. |
||
| 81 | * |
||
| 82 | * @param IQueryBuilder $qb |
||
| 83 | * @param $shareId |
||
| 84 | */ |
||
| 85 | protected function limitToShare(& $qb, $shareId) { |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Limit the request to the top share (no children) |
||
| 95 | * |
||
| 96 | * @param IQueryBuilder $qb |
||
| 97 | */ |
||
| 98 | protected function limitToShareParent(& $qb) { |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * limit the request to the children of a share |
||
| 107 | * |
||
| 108 | * @param IQueryBuilder $qb |
||
| 109 | * @param $userId |
||
| 110 | * @param int $parentId |
||
| 111 | */ |
||
| 112 | protected function limitToShareChildren(& $qb, $userId, $parentId = -1) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * limit the request to the share itself AND its children. |
||
| 126 | * perfect if you want to delete everything related to a share |
||
| 127 | * |
||
| 128 | * @param IQueryBuilder $qb |
||
| 129 | * @param $circleId |
||
| 130 | */ |
||
| 131 | protected function limitToShareAndChildren(& $qb, $circleId) { |
||
| 132 | $expr = $qb->expr(); |
||
| 133 | $pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
||
| 134 | |||
| 135 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 136 | $qb->andWhere( |
||
| 137 | $expr->orX( |
||
| 138 | $expr->eq($pf . 'parent', $qb->createNamedParameter($circleId)), |
||
| 139 | $expr->eq($pf . 'id', $qb->createNamedParameter($circleId)) |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * limit the request to a fileId. |
||
| 147 | * |
||
| 148 | * @param IQueryBuilder $qb |
||
| 149 | * @param $files |
||
| 150 | * |
||
| 151 | * @internal param $fileId |
||
| 152 | */ |
||
| 153 | protected function limitToFiles(& $qb, $files) { |
||
| 154 | |||
| 155 | if (!is_array($files)) { |
||
| 156 | $files = array($files); |
||
| 157 | } |
||
| 158 | |||
| 159 | $expr = $qb->expr(); |
||
| 160 | $pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
||
| 161 | $qb->andWhere( |
||
| 162 | $expr->in( |
||
| 163 | $pf . 'file_source', |
||
| 164 | $qb->createNamedParameter($files, IQueryBuilder::PARAM_INT_ARRAY) |
||
| 165 | ) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * @param IQueryBuilder $qb |
||
| 172 | * @param int $limit |
||
| 173 | * @param int $offset |
||
| 174 | */ |
||
| 175 | protected function limitToPage(& $qb, $limit = -1, $offset = 0) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * limit the request to a userId |
||
| 185 | * |
||
| 186 | * @param IQueryBuilder $qb |
||
| 187 | * @param string $userId |
||
| 188 | * @param bool $reShares |
||
| 189 | */ |
||
| 190 | protected function limitToShareOwner(& $qb, $userId, $reShares = false) { |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * link circle field |
||
| 210 | * |
||
| 211 | * @deprecated |
||
| 212 | * |
||
| 213 | * @param IQueryBuilder $qb |
||
| 214 | * @param int $shareId |
||
| 215 | */ |
||
| 216 | protected function linkCircleField(& $qb, $shareId = -1) { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param IQueryBuilder $qb |
||
| 250 | */ |
||
| 251 | protected function linkToCircleOwner(& $qb) { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Link to member (userId) of circle |
||
| 266 | * |
||
| 267 | * @param IQueryBuilder $qb |
||
| 268 | * @param string $userId |
||
| 269 | */ |
||
| 270 | protected function linkToMember(& $qb, $userId) { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * left join to get more data about the initiator of the share |
||
| 312 | * |
||
| 313 | * @param IQueryBuilder $qb |
||
| 314 | */ |
||
| 315 | protected function leftJoinShareInitiator(IQueryBuilder &$qb) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * Link to all members of circle |
||
| 340 | * |
||
| 341 | * @param IQueryBuilder $qb |
||
| 342 | */ |
||
| 343 | protected function joinCircleMembers(& $qb) { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Link to storage/filecache |
||
| 368 | * |
||
| 369 | * @param IQueryBuilder $qb |
||
| 370 | * @param string $userId |
||
| 371 | */ |
||
| 372 | protected function linkToFileCache(& $qb, $userId) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * add share to the database and return the ID |
||
| 394 | * |
||
| 395 | * @param IShare $share |
||
| 396 | * |
||
| 397 | * @return IQueryBuilder |
||
| 398 | */ |
||
| 399 | protected function getBaseInsertSql($share) { |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * generate and return a base sql request. |
||
| 420 | * |
||
| 421 | * @param int $shareId |
||
| 422 | * |
||
| 423 | * @return IQueryBuilder |
||
| 424 | */ |
||
| 425 | View Code Duplication | protected function getBaseSelectSql($shareId = -1) { |
|
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Generate and return a base sql request |
||
| 448 | * This one should be used to retrieve a complete list of users (ie. access list). |
||
| 449 | * |
||
| 450 | * @return IQueryBuilder |
||
| 451 | */ |
||
| 452 | protected function getAccessListBaseSelectSql() { |
||
| 464 | |||
| 465 | |||
| 466 | View Code Duplication | protected function getCompleteSelectSql() { |
|
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * @param IQueryBuilder $qb |
||
| 490 | */ |
||
| 491 | private function joinShare(& $qb) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * generate and return a base sql request. |
||
| 508 | * |
||
| 509 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 510 | */ |
||
| 511 | View Code Duplication | protected function getBaseDeleteSql() { |
|
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * generate and return a base sql request. |
||
| 524 | * |
||
| 525 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 526 | */ |
||
| 527 | View Code Duplication | protected function getBaseUpdateSql() { |
|
| 536 | } |
||
| 537 |
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.