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 |
||
| 37 | class CircleProviderRequestBuilder { |
||
| 38 | |||
| 39 | |||
| 40 | /** @var IDBConnection */ |
||
| 41 | protected $dbConnection; |
||
| 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 \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 51 | * @internal param $share |
||
| 52 | * |
||
| 53 | */ |
||
| 54 | protected function findShareParentSql($fileId, $circleId) { |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Limit the request to a Circle. |
||
| 67 | * |
||
| 68 | * @param $qb |
||
| 69 | * @param $circleId |
||
| 70 | */ |
||
| 71 | View Code Duplication | protected function limitToCircle(& $qb, $circleId) { |
|
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Limit the request to the Share by its Id. |
||
| 83 | * |
||
| 84 | * @param $qb |
||
| 85 | * @param $shareId |
||
| 86 | */ |
||
| 87 | View Code Duplication | protected function limitToShare(& $qb, $shareId) { |
|
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Limit the request to the top share (no children) |
||
| 99 | * |
||
| 100 | * @param $qb |
||
| 101 | */ |
||
| 102 | protected function limitToShareParent(& $qb) { |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * limit the request to the children of a share |
||
| 111 | * |
||
| 112 | * @param $qb |
||
| 113 | * @param $userId |
||
| 114 | * @param int $parentId |
||
| 115 | */ |
||
| 116 | protected function limitToShareChildren(& $qb, $userId, $parentId = -1) { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * limit the request to the share itself AND its children. |
||
| 130 | * perfect if you want to delete everything related to a share |
||
| 131 | * |
||
| 132 | * @param $qb |
||
| 133 | * @param $circleId |
||
| 134 | */ |
||
| 135 | protected function limitToShareAndChildren(& $qb, $circleId) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * limit the request to a fileId. |
||
| 150 | * |
||
| 151 | * @param IQueryBuilder $qb |
||
| 152 | * @param $fileId |
||
| 153 | */ |
||
| 154 | View Code Duplication | protected function limitToFile(& $qb, $fileId) { |
|
| 162 | |||
| 163 | |||
| 164 | protected function limitToPage(& $qb, $limit = -1, $offset = 0) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * limit the request to a userId |
||
| 174 | * |
||
| 175 | * @param $qb |
||
| 176 | * @param $userId |
||
| 177 | * @param bool $reShares |
||
| 178 | */ |
||
| 179 | protected function limitToOwner(& $qb, $userId, $reShares = false) { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * link circle field |
||
| 199 | * |
||
| 200 | * @deprecated |
||
| 201 | * |
||
| 202 | * @param $qb |
||
| 203 | * @param $shareId |
||
| 204 | */ |
||
| 205 | protected function linkCircleField(& $qb, $shareId) { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Link to members (userId) of circle |
||
| 221 | * |
||
| 222 | * @param $qb |
||
| 223 | * @param $userId |
||
| 224 | */ |
||
| 225 | protected function linkToMember(& $qb, $userId) { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Link to storage/filecache |
||
| 237 | * |
||
| 238 | * @param $qb |
||
| 239 | * @param $userId |
||
| 240 | */ |
||
| 241 | protected function linkToFileCache(& $qb, $userId) { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * add share to the database and return the ID |
||
| 257 | * |
||
| 258 | * @param $share |
||
| 259 | * |
||
| 260 | * @return IQueryBuilder |
||
| 261 | */ |
||
| 262 | protected function getBaseInsertSql($share) { |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * generate and return a base sql request. |
||
| 283 | * |
||
| 284 | * @param int $shareId |
||
| 285 | * |
||
| 286 | * @return IQueryBuilder |
||
| 287 | */ |
||
| 288 | protected function getBaseSelectSql($shareId = -1) { |
||
| 304 | |||
| 305 | |||
| 306 | protected function getCompleteSelectSql() { |
||
| 322 | |||
| 323 | |||
| 324 | private function linkToShare(& $qb) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * generate and return a base sql request. |
||
| 340 | * |
||
| 341 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 342 | */ |
||
| 343 | View Code Duplication | protected function getBaseDeleteSql() { |
|
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * generate and return a base sql request. |
||
| 356 | * |
||
| 357 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 358 | */ |
||
| 359 | View Code Duplication | protected function getBaseUpdateSql() { |
|
| 368 | } |
||
| 369 |
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.