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 |
||
| 43 | class CirclesRequestBuilder extends CoreRequestBuilder { |
||
| 44 | |||
| 45 | |||
| 46 | /** @var MembersRequest */ |
||
| 47 | protected $membersRequest; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * CirclesRequestBuilder constructor. |
||
| 51 | * |
||
| 52 | * {@inheritdoc} |
||
| 53 | * @param MembersRequest $membersRequest |
||
| 54 | */ |
||
| 55 | public function __construct( |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Left Join the Groups table |
||
| 66 | * |
||
| 67 | * @param IQueryBuilder $qb |
||
| 68 | * @param string $field |
||
| 69 | */ |
||
| 70 | protected function leftJoinGroups(IQueryBuilder &$qb, $field) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Limit the search to a non-personal circle |
||
| 81 | * |
||
| 82 | * @param IQueryBuilder $qb |
||
| 83 | */ |
||
| 84 | protected function limitToNonPersonalCircle(IQueryBuilder &$qb) { |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * @param IQueryBuilder $qb |
||
| 95 | * @param string $circleUniqueId |
||
| 96 | * @param $userId |
||
| 97 | * @param $type |
||
| 98 | * @param $name |
||
| 99 | * |
||
| 100 | * @throws ConfigNoCircleAvailableException |
||
| 101 | */ |
||
| 102 | protected function limitRegardingCircleType( |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param IQueryBuilder $qb |
||
| 126 | * @param string $circleUniqueId |
||
| 127 | * @param $userId |
||
| 128 | * @param $type |
||
| 129 | * @param $name |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | private function generateLimit(IQueryBuilder &$qb, $circleUniqueId, $userId, $type, $name) { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * @param IQueryBuilder $qb |
||
| 146 | * @param int|string $userId |
||
| 147 | * @param int $type |
||
| 148 | * |
||
| 149 | * @return \OCP\DB\QueryBuilder\ICompositeExpression |
||
| 150 | */ |
||
| 151 | private function generateLimitPersonal(IQueryBuilder $qb, $userId, $type) { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @param IQueryBuilder $qb |
||
| 167 | * @param string $circleUniqueId |
||
| 168 | * @param int $type |
||
| 169 | * @param string $name |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function generateLimitSecret(IQueryBuilder $qb, $circleUniqueId, $type, $name) { |
||
| 174 | if (!(Circle::CIRCLES_SECRET & (int)$type)) { |
||
| 175 | return null; |
||
| 176 | } |
||
| 177 | $expr = $qb->expr(); |
||
| 178 | |||
| 179 | $orX = $expr->orX($expr->gte('u.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
||
| 180 | $orX->add($expr->eq('c.name', $qb->createNamedParameter($name))) |
||
| 181 | ->add( |
||
| 182 | $expr->eq( |
||
| 183 | $qb->createNamedParameter($circleUniqueId), |
||
| 184 | $qb->createFunction('LEFT(c.unique_id, ' . Circle::UNIQUEID_SHORT_LENGTH . ')') |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | |||
| 188 | if ($this->leftJoinedNCGroupAndUser) { |
||
| 189 | $orX->add($expr->gte('g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 193 | $sqb = $expr->andX( |
||
| 194 | $expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_SECRET)), |
||
| 195 | $expr->orX($orX) |
||
| 196 | ); |
||
| 197 | |||
| 198 | return $sqb; |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @param IQueryBuilder $qb |
||
| 204 | * @param int $type |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | View Code Duplication | private function generateLimitClosed(IQueryBuilder $qb, $type) { |
|
| 209 | if (!(Circle::CIRCLES_CLOSED & (int)$type)) { |
||
| 210 | return null; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $qb->expr() |
||
| 214 | ->eq( |
||
| 215 | 'c.type', |
||
| 216 | $qb->createNamedParameter(Circle::CIRCLES_CLOSED) |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @param IQueryBuilder $qb |
||
| 223 | * @param int $type |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | View Code Duplication | private function generateLimitPublic(IQueryBuilder $qb, $type) { |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * add a request to the members list, using the current user ID. |
||
| 242 | * will returns level and stuff. |
||
| 243 | * |
||
| 244 | * @param IQueryBuilder $qb |
||
| 245 | * @param string $userId |
||
| 246 | */ |
||
| 247 | View Code Duplication | protected function leftJoinUserIdAsViewer(IQueryBuilder &$qb, $userId) { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Left Join members table to get the owner of the circle. |
||
| 276 | * |
||
| 277 | * @param IQueryBuilder $qb |
||
| 278 | */ |
||
| 279 | View Code Duplication | protected function leftJoinOwner(IQueryBuilder &$qb) { |
|
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Base of the Sql Select request for Shares |
||
| 309 | * |
||
| 310 | * @return IQueryBuilder |
||
| 311 | */ |
||
| 312 | View Code Duplication | protected function getLinksSelectSql() { |
|
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * Base of the Sql Select request for Shares |
||
| 327 | * |
||
| 328 | * @return IQueryBuilder |
||
| 329 | */ |
||
| 330 | View Code Duplication | protected function getSharesSelectSql() { |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Base of the Sql Insert request for Shares |
||
| 347 | * |
||
| 348 | * @return IQueryBuilder |
||
| 349 | */ |
||
| 350 | View Code Duplication | protected function getSharesInsertSql() { |
|
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Base of the Sql Update request for Shares |
||
| 361 | * |
||
| 362 | * @param string $uniqueId |
||
| 363 | * |
||
| 364 | * @return IQueryBuilder |
||
| 365 | */ |
||
| 366 | View Code Duplication | protected function getSharesUpdateSql($uniqueId) { |
|
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Base of the Sql Insert request for Shares |
||
| 380 | * |
||
| 381 | * |
||
| 382 | * @return IQueryBuilder |
||
| 383 | */ |
||
| 384 | View Code Duplication | protected function getCirclesInsertSql() { |
|
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Base of the Sql Update request for Shares |
||
| 395 | * |
||
| 396 | * @param int $uniqueId |
||
| 397 | * |
||
| 398 | * @return IQueryBuilder |
||
| 399 | */ |
||
| 400 | View Code Duplication | protected function getCirclesUpdateSql($uniqueId) { |
|
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Base of the Sql Delete request |
||
| 414 | * |
||
| 415 | * @param string $circleUniqueId |
||
| 416 | * |
||
| 417 | * @return IQueryBuilder |
||
| 418 | */ |
||
| 419 | View Code Duplication | protected function getCirclesDeleteSql($circleUniqueId) { |
|
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * @return IQueryBuilder |
||
| 436 | */ |
||
| 437 | protected function getCirclesSelectSql() { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * @param array $data |
||
| 454 | * |
||
| 455 | * @return Circle |
||
| 456 | */ |
||
| 457 | protected function parseCirclesSelectSql($data) { |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * @param array $data |
||
| 492 | * |
||
| 493 | * @return SharingFrame |
||
| 494 | */ |
||
| 495 | protected function parseSharesSelectSql($data) { |
||
| 507 | |||
| 508 | |||
| 509 | /** |
||
| 510 | * @param array $data |
||
| 511 | * |
||
| 512 | * @return FederatedLink |
||
| 513 | */ |
||
| 514 | protected function parseLinksSelectSql($data) { |
||
| 526 | |||
| 527 | |||
| 528 | } |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.