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 |
||
| 38 | class CircleProviderRequestBuilder { |
||
| 39 | |||
| 40 | |||
| 41 | /** @var IDBConnection */ |
||
| 42 | protected $dbConnection; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * returns the SQL request to get a specific share from the fileId and circleId |
||
| 47 | * |
||
| 48 | * @param int $fileId |
||
| 49 | * @param int $circleId |
||
| 50 | * |
||
| 51 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 52 | * @internal param $share |
||
| 53 | * |
||
| 54 | */ |
||
| 55 | protected function findShareParentSql($fileId, $circleId) { |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Limit the request to a Circle. |
||
| 68 | * |
||
| 69 | * @param IQueryBuilder $qb |
||
| 70 | * @param integer $circleId |
||
| 71 | */ |
||
| 72 | protected function limitToCircle(& $qb, $circleId) { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Limit the request to the Share by its Id. |
||
| 82 | * |
||
| 83 | * @param IQueryBuilder $qb |
||
| 84 | * @param $shareId |
||
| 85 | */ |
||
| 86 | protected function limitToShare(& $qb, $shareId) { |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Limit the request to the top share (no children) |
||
| 96 | * |
||
| 97 | * @param IQueryBuilder $qb |
||
| 98 | */ |
||
| 99 | protected function limitToShareParent(& $qb) { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * limit the request to the children of a share |
||
| 108 | * |
||
| 109 | * @param IQueryBuilder $qb |
||
| 110 | * @param $userId |
||
| 111 | * @param int $parentId |
||
| 112 | */ |
||
| 113 | protected function limitToShareChildren(& $qb, $userId, $parentId = -1) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * limit the request to the share itself AND its children. |
||
| 127 | * perfect if you want to delete everything related to a share |
||
| 128 | * |
||
| 129 | * @param IQueryBuilder $qb |
||
| 130 | * @param $circleId |
||
| 131 | */ |
||
| 132 | protected function limitToShareAndChildren(& $qb, $circleId) { |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * limit the request to a fileId. |
||
| 148 | * |
||
| 149 | * @param IQueryBuilder $qb |
||
| 150 | * @param $fileId |
||
| 151 | */ |
||
| 152 | protected function limitToFiles(& $qb, $files) { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @param IQueryBuilder $qb |
||
| 171 | * @param int $limit |
||
| 172 | * @param int $offset |
||
| 173 | */ |
||
| 174 | protected function limitToPage(& $qb, $limit = -1, $offset = 0) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * limit the request to a userId |
||
| 184 | * |
||
| 185 | * @param IQueryBuilder $qb |
||
| 186 | * @param string $userId |
||
| 187 | * @param bool $reShares |
||
| 188 | */ |
||
| 189 | protected function limitToShareOwner(& $qb, $userId, $reShares = false) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * link circle field |
||
| 209 | * |
||
| 210 | * @deprecated |
||
| 211 | * |
||
| 212 | * @param IQueryBuilder $qb |
||
| 213 | * @param integer $shareId |
||
| 214 | */ |
||
| 215 | // TODO - put this as a leftjoin |
||
| 216 | protected function linkCircleField(& $qb, $shareId) { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * @param IQueryBuilder $qb |
||
| 233 | */ |
||
| 234 | protected function linkToCircleOwner(& $qb) { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Link to member (userId) of circle |
||
| 249 | * |
||
| 250 | * @param IQueryBuilder $qb |
||
| 251 | * @param string $userId |
||
| 252 | */ |
||
| 253 | protected function linkToMember(& $qb, $userId) { |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Link to all members of circle |
||
| 265 | * |
||
| 266 | * @param IQueryBuilder $qb |
||
| 267 | */ |
||
| 268 | protected function joinCircleMembers(& $qb) { |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Link to storage/filecache |
||
| 278 | * |
||
| 279 | * @param IQueryBuilder $qb |
||
| 280 | * @param string $userId |
||
| 281 | */ |
||
| 282 | protected function linkToFileCache(& $qb, $userId) { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * add share to the database and return the ID |
||
| 299 | * |
||
| 300 | * @param IShare $share |
||
| 301 | * |
||
| 302 | * @return IQueryBuilder |
||
| 303 | */ |
||
| 304 | protected function getBaseInsertSql($share) { |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * generate and return a base sql request. |
||
| 325 | * |
||
| 326 | * @param int $shareId |
||
| 327 | * |
||
| 328 | * @return IQueryBuilder |
||
| 329 | */ |
||
| 330 | protected function getBaseSelectSql($shareId = -1) { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Generate and return a base sql request |
||
| 353 | * This one should be used to retrieve a complete list of users (ie. access list). |
||
| 354 | * |
||
| 355 | * @return IQueryBuilder |
||
| 356 | */ |
||
| 357 | protected function getAccessListBaseSelectSql() { |
||
| 369 | |||
| 370 | |||
| 371 | protected function getCompleteSelectSql() { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param IQueryBuilder $qb |
||
| 392 | */ |
||
| 393 | private function joinShare(& $qb) { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * generate and return a base sql request. |
||
| 410 | * |
||
| 411 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 412 | */ |
||
| 413 | View Code Duplication | protected function getBaseDeleteSql() { |
|
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * generate and return a base sql request. |
||
| 426 | * |
||
| 427 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 428 | */ |
||
| 429 | View Code Duplication | protected function getBaseUpdateSql() { |
|
| 438 | } |
||
| 439 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.