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 \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 53 | * @internal param $share |
||
| 54 | * |
||
| 55 | */ |
||
| 56 | protected function findShareParentSql($fileId, $circleId) { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Limit the request to a Circle. |
||
| 69 | * |
||
| 70 | * @param IQueryBuilder $qb |
||
| 71 | * @param integer $circleId |
||
| 72 | */ |
||
| 73 | protected function limitToCircle(& $qb, $circleId) { |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Limit the request to the Share by its Id. |
||
| 83 | * |
||
| 84 | * @param IQueryBuilder $qb |
||
| 85 | * @param $shareId |
||
| 86 | */ |
||
| 87 | protected function limitToShare(& $qb, $shareId) { |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Limit the request to the top share (no children) |
||
| 97 | * |
||
| 98 | * @param IQueryBuilder $qb |
||
| 99 | */ |
||
| 100 | protected function limitToShareParent(& $qb) { |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * limit the request to the children of a share |
||
| 109 | * |
||
| 110 | * @param IQueryBuilder $qb |
||
| 111 | * @param $userId |
||
| 112 | * @param int $parentId |
||
| 113 | */ |
||
| 114 | protected function limitToShareChildren(& $qb, $userId, $parentId = -1) { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * limit the request to the share itself AND its children. |
||
| 128 | * perfect if you want to delete everything related to a share |
||
| 129 | * |
||
| 130 | * @param IQueryBuilder $qb |
||
| 131 | * @param $circleId |
||
| 132 | */ |
||
| 133 | protected function limitToShareAndChildren(& $qb, $circleId) { |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * limit the request to a fileId. |
||
| 149 | * |
||
| 150 | * @param IQueryBuilder $qb |
||
| 151 | * @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 integer $shareId |
||
| 215 | */ |
||
| 216 | // TODO - put this as a leftjoin |
||
| 217 | protected function linkCircleField(& $qb, $shareId = -1) { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * @param IQueryBuilder $qb |
||
| 251 | */ |
||
| 252 | protected function linkToCircleOwner(& $qb) { |
||
| 253 | $expr = $qb->expr(); |
||
| 254 | |||
| 255 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 256 | $qb->leftJoin( |
||
| 257 | 'c', 'circles_members', 'mo', $expr->andX( |
||
| 258 | $expr->eq('c.id', 'mo.circle_id'), |
||
| 259 | $expr->eq('mo.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
||
| 260 | ) |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Link to member (userId) of circle |
||
| 267 | * |
||
| 268 | * @param IQueryBuilder $qb |
||
| 269 | * @param string $userId |
||
| 270 | */ |
||
| 271 | protected function linkToMember(& $qb, $userId) { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Link to all members of circle |
||
| 295 | * |
||
| 296 | * @param IQueryBuilder $qb |
||
| 297 | */ |
||
| 298 | protected function joinCircleMembers(& $qb) { |
||
| 299 | $expr = $qb->expr(); |
||
| 300 | |||
| 301 | $qb->from(MembersMapper::TABLENAME, 'm'); |
||
| 302 | |||
| 303 | // TODO - Remove this in 12.0.1 |
||
| 304 | View Code Duplication | if ($qb->getConnection() |
|
| 305 | ->getDatabasePlatform() instanceof PostgreSqlPlatform |
||
|
1 ignored issue
–
show
|
|||
| 306 | ) { |
||
| 307 | $qb->andWhere( |
||
| 308 | $expr->eq('s.share_with', $qb->createFunction('CAST(m.circle_id AS TEXT)')) |
||
| 309 | ); |
||
| 310 | } else { |
||
| 311 | |||
| 312 | $qb->andWhere( |
||
| 313 | $expr->eq( |
||
| 314 | 's.share_with', $expr->castColumn('m.circle_id', IQueryBuilder::PARAM_STR) |
||
| 315 | ) |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Link to storage/filecache |
||
| 323 | * |
||
| 324 | * @param IQueryBuilder $qb |
||
| 325 | * @param string $userId |
||
| 326 | */ |
||
| 327 | protected function linkToFileCache(& $qb, $userId) { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * add share to the database and return the ID |
||
| 344 | * |
||
| 345 | * @param IShare $share |
||
| 346 | * |
||
| 347 | * @return IQueryBuilder |
||
| 348 | */ |
||
| 349 | protected function getBaseInsertSql($share) { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * generate and return a base sql request. |
||
| 370 | * |
||
| 371 | * @param int $shareId |
||
| 372 | * |
||
| 373 | * @return IQueryBuilder |
||
| 374 | */ |
||
| 375 | protected function getBaseSelectSql($shareId = -1) { |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Generate and return a base sql request |
||
| 398 | * This one should be used to retrieve a complete list of users (ie. access list). |
||
| 399 | * |
||
| 400 | * @return IQueryBuilder |
||
| 401 | */ |
||
| 402 | protected function getAccessListBaseSelectSql() { |
||
| 403 | $qb = $this->dbConnection->getQueryBuilder(); |
||
| 404 | |||
| 405 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 406 | $qb->select( |
||
| 407 | 'm.user_id', 's.file_source', 's.file_target' |
||
| 408 | ); |
||
| 409 | $this->joinCircleMembers($qb); |
||
| 410 | $this->joinShare($qb); |
||
| 411 | |||
| 412 | return $qb; |
||
| 413 | } |
||
| 414 | |||
| 415 | |||
| 416 | protected function getCompleteSelectSql() { |
||
| 417 | $qb = $this->dbConnection->getQueryBuilder(); |
||
| 418 | |||
| 419 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 420 | $qb->select( |
||
| 421 | 's.*', 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', |
||
| 422 | 'f.path_hash', 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', |
||
| 423 | 'f.size', 'f.mtime', 'f.storage_mtime', 'f.encrypted', 'f.unencrypted_size', |
||
| 424 | 'f.etag', 'f.checksum', 'c.type AS circle_type', 'c.name AS circle_name', |
||
| 425 | 's2.id AS parent_id', 's2.file_target AS parent_target', |
||
| 426 | 's2.permissions AS parent_perms' |
||
| 427 | ) |
||
| 428 | ->selectAlias('st.id', 'storage_string_id'); |
||
| 429 | |||
| 430 | $this->joinShare($qb); |
||
| 431 | $this->linkCircleField($qb); |
||
| 432 | |||
| 433 | |||
| 434 | return $qb; |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * @param IQueryBuilder $qb |
||
| 440 | */ |
||
| 441 | private function joinShare(& $qb) { |
||
| 442 | $expr = $qb->expr(); |
||
| 443 | |||
| 444 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 445 | $qb->from('share', 's') |
||
| 446 | ->where($expr->eq('s.share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))) |
||
| 447 | ->andWhere( |
||
| 448 | $expr->orX( |
||
| 449 | $expr->eq('s.item_type', $qb->createNamedParameter('file')), |
||
| 450 | $expr->eq('s.item_type', $qb->createNamedParameter('folder')) |
||
| 451 | ) |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * generate and return a base sql request. |
||
| 458 | * |
||
| 459 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 460 | */ |
||
| 461 | View Code Duplication | protected function getBaseDeleteSql() { |
|
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * generate and return a base sql request. |
||
| 474 | * |
||
| 475 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 476 | */ |
||
| 477 | View Code Duplication | protected function getBaseUpdateSql() { |
|
| 486 | } |
||
| 487 |
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.