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 |
||
| 42 | class CirclesRequestBuilder extends CoreRequestBuilder { |
||
| 43 | |||
| 44 | |||
| 45 | /** @var MembersRequest */ |
||
| 46 | protected $membersRequest; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * CirclesRequestBuilder constructor. |
||
| 50 | * |
||
| 51 | * {@inheritdoc} |
||
| 52 | * @param MembersRequest $membersRequest |
||
| 53 | */ |
||
| 54 | public function __construct( |
||
| 55 | IL10N $l10n, IDBConnection $connection, MembersRequest $membersRequest, |
||
| 56 | ConfigService $configService, MiscService $miscService |
||
| 57 | ) { |
||
| 58 | parent::__construct($l10n, $connection, $configService, $miscService); |
||
| 59 | $this->membersRequest = $membersRequest; |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Left Join the Groups table |
||
| 65 | * |
||
| 66 | * @param IQueryBuilder $qb |
||
| 67 | * @param string $field |
||
| 68 | */ |
||
| 69 | protected function leftJoinGroups(IQueryBuilder &$qb, $field) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Limit the search to a non-personal circle |
||
| 80 | * |
||
| 81 | * @param IQueryBuilder $qb |
||
| 82 | */ |
||
| 83 | protected function limitToNonPersonalCircle(IQueryBuilder &$qb) { |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * @param IQueryBuilder $qb |
||
| 94 | * @param string $circleUniqueId |
||
| 95 | * @param $userId |
||
| 96 | * @param $type |
||
| 97 | * @param $name |
||
| 98 | * |
||
| 99 | * @throws ConfigNoCircleAvailableException |
||
| 100 | */ |
||
| 101 | protected function limitRegardingCircleType( |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @param IQueryBuilder $qb |
||
| 125 | * @param string $circleUniqueId |
||
| 126 | * @param $userId |
||
| 127 | * @param $type |
||
| 128 | * @param $name |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | private function generateLimit(IQueryBuilder &$qb, $circleUniqueId, $userId, $type, $name) { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param IQueryBuilder $qb |
||
| 145 | * @param int|string $userId |
||
| 146 | * @param int $type |
||
| 147 | * |
||
| 148 | * @return \OCP\DB\QueryBuilder\ICompositeExpression |
||
| 149 | */ |
||
| 150 | private function generateLimitPersonal(IQueryBuilder $qb, $userId, $type) { |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * @param IQueryBuilder $qb |
||
| 166 | * @param string $circleUniqueId |
||
| 167 | * @param int $type |
||
| 168 | * @param string $name |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | private function generateLimitSecret(IQueryBuilder $qb, $circleUniqueId, $type, $name) { |
||
| 173 | if (!(Circle::CIRCLES_SECRET & (int)$type)) { |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | $expr = $qb->expr(); |
||
| 177 | |||
| 178 | $orX = $expr->orX($expr->gte('u.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
||
| 179 | $orX->add($expr->eq('c.name', $qb->createNamedParameter($name))) |
||
| 180 | ->add( |
||
| 181 | $expr->eq( |
||
| 182 | $qb->createNamedParameter($circleUniqueId), |
||
| 183 | $qb->createFunction( |
||
| 184 | 'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')' |
||
| 185 | ) |
||
| 186 | ) |
||
| 187 | ); |
||
| 188 | |||
| 189 | if ($this->leftJoinedNCGroupAndUser) { |
||
| 190 | $orX->add($expr->gte('g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 194 | $sqb = $expr->andX( |
||
| 195 | $expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_SECRET)), |
||
| 196 | $expr->orX($orX) |
||
| 197 | ); |
||
| 198 | |||
| 199 | return $sqb; |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param IQueryBuilder $qb |
||
| 205 | * @param int $type |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | View Code Duplication | private function generateLimitClosed(IQueryBuilder $qb, $type) { |
|
| 210 | if (!(Circle::CIRCLES_CLOSED & (int)$type)) { |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | return $qb->expr() |
||
| 215 | ->eq( |
||
| 216 | 'c.type', |
||
| 217 | $qb->createNamedParameter(Circle::CIRCLES_CLOSED) |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param IQueryBuilder $qb |
||
| 224 | * @param int $type |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | View Code Duplication | private function generateLimitPublic(IQueryBuilder $qb, $type) { |
|
| 229 | if (!(Circle::CIRCLES_PUBLIC & (int)$type)) { |
||
| 230 | return null; |
||
| 231 | } |
||
| 232 | |||
| 233 | return $qb->expr() |
||
| 234 | ->eq( |
||
| 235 | 'c.type', |
||
| 236 | $qb->createNamedParameter(Circle::CIRCLES_PUBLIC) |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * add a request to the members list, using the current user ID. |
||
| 243 | * will returns level and stuff. |
||
| 244 | * |
||
| 245 | * @param IQueryBuilder $qb |
||
| 246 | * @param string $userId |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function leftJoinUserIdAsViewer(IQueryBuilder &$qb, $userId) { |
|
| 249 | |||
| 250 | if ($qb->getType() !== QueryBuilder::SELECT) { |
||
| 251 | return; |
||
| 252 | } |
||
| 253 | |||
| 254 | $expr = $qb->expr(); |
||
| 255 | $pf = '`' . $this->default_select_alias . '`.'; |
||
| 256 | |||
| 257 | /** @noinspection PhpMethodParametersCountMismatchInspection */ |
||
| 258 | $qb->selectAlias('u.user_id', 'viewer_userid') |
||
| 259 | ->selectAlias('u.status', 'viewer_status') |
||
| 260 | ->selectAlias('u.level', 'viewer_level') |
||
| 261 | ->leftJoin( |
||
| 262 | $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u', |
||
| 263 | $expr->andX( |
||
| 264 | $expr->eq( |
||
| 265 | 'u.circle_id', |
||
| 266 | $qb->createFunction( |
||
| 267 | 'SUBSTR(' . $pf . '`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH |
||
| 268 | . ')' |
||
| 269 | ) |
||
| 270 | ), |
||
| 271 | $expr->eq('u.user_id', $qb->createNamedParameter($userId)), |
||
| 272 | $expr->eq('u.user_type', $qb->createNamedParameter(Member::TYPE_USER)) |
||
| 273 | ) |
||
| 274 | ); |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Left Join members table to get the owner of the circle. |
||
| 280 | * |
||
| 281 | * @param IQueryBuilder $qb |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function leftJoinOwner(IQueryBuilder &$qb) { |
|
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Base of the Sql Insert request for Shares |
||
| 316 | * |
||
| 317 | * |
||
| 318 | * @return IQueryBuilder |
||
| 319 | */ |
||
| 320 | View Code Duplication | protected function getCirclesInsertSql() { |
|
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Base of the Sql Update request for Shares |
||
| 331 | * |
||
| 332 | * @param int $uniqueId |
||
| 333 | * |
||
| 334 | * @return IQueryBuilder |
||
| 335 | */ |
||
| 336 | View Code Duplication | protected function getCirclesUpdateSql($uniqueId) { |
|
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Base of the Sql Delete request |
||
| 350 | * |
||
| 351 | * @param string $circleUniqueId |
||
| 352 | * |
||
| 353 | * @return IQueryBuilder |
||
| 354 | */ |
||
| 355 | View Code Duplication | protected function getCirclesDeleteSql($circleUniqueId) { |
|
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @return IQueryBuilder |
||
| 374 | */ |
||
| 375 | protected function getCirclesSelectSql() { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param array $data |
||
| 392 | * |
||
| 393 | * @return Circle |
||
| 394 | */ |
||
| 395 | protected function parseCirclesSelectSql($data) { |
||
| 422 | |||
| 423 | |||
| 424 | } |
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.