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:
Complex classes like Circles often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Circles, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Circles { |
||
| 49 | |||
| 50 | const API_VERSION = [0, 10, 0]; |
||
| 51 | |||
| 52 | protected static function getContainer() { |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * Circles::version(); |
||
| 61 | * |
||
| 62 | * returns the current version of the API |
||
| 63 | * |
||
| 64 | * @return int[] |
||
| 65 | */ |
||
| 66 | public static function version() { |
||
| 69 | |||
| 70 | |||
| 71 | public static function addJavascriptAPI() { |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Circles::compareVersion(); |
||
| 81 | * |
||
| 82 | * Compare and return true if version is compatible. |
||
| 83 | * Exception otherwise. |
||
| 84 | * |
||
| 85 | * @param array $apiVersion |
||
| 86 | * |
||
| 87 | * @return bool |
||
| 88 | * @throws ApiVersionIncompatibleException |
||
| 89 | */ |
||
| 90 | public static function compareVersion($apiVersion) { |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Circles::createCircle(); |
||
| 102 | * |
||
| 103 | * Create a new circle and make the current user its owner. |
||
| 104 | * You must specify type and name. type is one of this value: |
||
| 105 | * |
||
| 106 | * CIRCLES_PERSONAL is 1 or 'personal' |
||
| 107 | * CIRCLES_SECRET is 2 or 'secret' |
||
| 108 | * CIRCLES_CLOSED is 4 or 'closed' |
||
| 109 | * CIRCLES_PUBLIC is 8 or 'public' |
||
| 110 | * |
||
| 111 | * @param mixed $type |
||
| 112 | * @param string $name |
||
| 113 | * |
||
| 114 | * @return Circle |
||
| 115 | * @throws QueryException |
||
| 116 | */ |
||
| 117 | public static function createCircle($type, $name) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Circles::joinCircle(); |
||
| 127 | * |
||
| 128 | * This function will make the current user joining a circle identified by its Id. |
||
| 129 | * |
||
| 130 | * @param string $circleUniqueId |
||
| 131 | * |
||
| 132 | * @return Member |
||
| 133 | * @throws QueryException |
||
| 134 | */ |
||
| 135 | public static function joinCircle($circleUniqueId) { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Circles::leaveCircle(); |
||
| 145 | * |
||
| 146 | * This function will make the current user leaving the circle identified by its Id. Will fail |
||
| 147 | * if user is the owner of the circle. |
||
| 148 | * |
||
| 149 | * @param string $circleUniqueId |
||
| 150 | * |
||
| 151 | * @return Member |
||
| 152 | * @throws QueryException |
||
| 153 | */ |
||
| 154 | public static function leaveCircle($circleUniqueId) { |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Circles::listCircles(); |
||
| 164 | * |
||
| 165 | * This function list all circles fitting a search regarding its name and the level and the |
||
| 166 | * rights from the current user. In case of Secret circle, name needs to be complete so the |
||
| 167 | * circle is included in the list (or if the current user is the owner) |
||
| 168 | * |
||
| 169 | * example: Circles::listCircles(Circle::CIRCLES_ALL, '', 8, callback); will returns all |
||
| 170 | * circles when the current user is at least an Admin. |
||
| 171 | * |
||
| 172 | * @param mixed $type |
||
| 173 | * @param string $name |
||
| 174 | * @param int $level |
||
| 175 | * @param string $userId |
||
| 176 | * |
||
| 177 | * @param bool $forceAll |
||
| 178 | * |
||
| 179 | * @return Circle[] |
||
| 180 | * @throws QueryException |
||
| 181 | */ |
||
| 182 | public static function listCircles($type, $name = '', $level = 0, $userId = '', $forceAll = false) { |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Circles::joinedCircles(); |
||
| 235 | * |
||
| 236 | * Return all the circle the current user is a member. |
||
| 237 | * |
||
| 238 | * @param string $userId |
||
| 239 | * @param bool $forceAll |
||
| 240 | * |
||
| 241 | * @return Circle[] |
||
| 242 | * @throws QueryException |
||
| 243 | */ |
||
| 244 | public static function joinedCircles($userId = '', $forceAll = false) { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Circles::joinedCircleIds(); |
||
| 251 | * |
||
| 252 | * Return all the circleIds the user is a member, if empty user, using current user. |
||
| 253 | * |
||
| 254 | * @param $userId |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | * @throws QueryException |
||
| 258 | */ |
||
| 259 | public static function joinedCircleIds($userId = '') { |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Circles::detailsCircle(); |
||
| 272 | * |
||
| 273 | * WARNING - This function is called by the core - WARNING |
||
| 274 | * Do not change it |
||
| 275 | * |
||
| 276 | * Returns details on the circle. If the current user is a member, the members list will be |
||
| 277 | * return as well. |
||
| 278 | * |
||
| 279 | * @param string $circleUniqueId |
||
| 280 | * @param bool $forceAll |
||
| 281 | * |
||
| 282 | * @return Circle |
||
| 283 | * @throws QueryException |
||
| 284 | */ |
||
| 285 | public static function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Circles::settingsCircle(); |
||
| 295 | * |
||
| 296 | * Save the settings. Settings is an array and current user need to be an admin |
||
| 297 | * |
||
| 298 | * @param string $circleUniqueId |
||
| 299 | * @param array $settings |
||
| 300 | * |
||
| 301 | * @return Circle |
||
| 302 | * @throws QueryException |
||
| 303 | */ |
||
| 304 | public static function settingsCircle($circleUniqueId, array $settings) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Circles::destroyCircle(); |
||
| 314 | * |
||
| 315 | * This function will destroy the circle if the current user is the Owner. |
||
| 316 | * |
||
| 317 | * @param string $circleUniqueId |
||
| 318 | * |
||
| 319 | * @return mixed |
||
| 320 | * @throws QueryException |
||
| 321 | */ |
||
| 322 | public static function destroyCircle($circleUniqueId) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Circles::addMember(); |
||
| 332 | * |
||
| 333 | * This function will add a user as member of the circle. Current user need at least to be |
||
| 334 | * Moderator. |
||
| 335 | * |
||
| 336 | * @param string $circleUniqueId |
||
| 337 | * @param string $ident |
||
| 338 | * @param int $type |
||
| 339 | * |
||
| 340 | * @return Member[] |
||
| 341 | * @throws QueryException |
||
| 342 | */ |
||
| 343 | public static function addMember($circleUniqueId, $ident, $type) { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Circles::getMember(); |
||
| 353 | * |
||
| 354 | * This function will return information on a member of the circle. Current user need at least |
||
| 355 | * to be Member. |
||
| 356 | * |
||
| 357 | * @param string $circleUniqueId |
||
| 358 | * @param string $ident |
||
| 359 | * @param int $type |
||
| 360 | * @param bool $forceAll |
||
| 361 | * |
||
| 362 | * @return Member |
||
| 363 | * @throws QueryException |
||
| 364 | */ |
||
| 365 | public static function getMember($circleUniqueId, $ident, $type, $forceAll = false) { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Circles::removeMember(); |
||
| 375 | * |
||
| 376 | * This function will remove a member from the circle. Current user needs to be at least |
||
| 377 | * Moderator and have a higher level that the targeted member. |
||
| 378 | * |
||
| 379 | * @param string $circleUniqueId |
||
| 380 | * @param string $ident |
||
| 381 | * @param int $type |
||
| 382 | * |
||
| 383 | * @return Member[] |
||
| 384 | * @throws QueryException |
||
| 385 | */ |
||
| 386 | public static function removeMember($circleUniqueId, $ident, $type) { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Circles::levelMember(); |
||
| 396 | * |
||
| 397 | * Edit the level of a member of the circle. The current level of the target needs to be lower |
||
| 398 | * than the user that initiate the process (ie. the current user). The new level of the target |
||
| 399 | * cannot be the same than the current level of the user that initiate the process (ie. the |
||
| 400 | * current user). |
||
| 401 | * |
||
| 402 | * @param string $circleUniqueId |
||
| 403 | * @param string $ident |
||
| 404 | * @param int $type |
||
| 405 | * @param int $level |
||
| 406 | * |
||
| 407 | * @return Member[] |
||
| 408 | * @throws QueryException |
||
| 409 | */ |
||
| 410 | public static function levelMember($circleUniqueId, $ident, $type, $level) { |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * Circles::shareToCircle(); |
||
| 420 | * |
||
| 421 | * This function will share an item (array) to the circle identified by its Id. |
||
| 422 | * Source is the app that is sharing the item and type can be used by the app to identified the |
||
| 423 | * payload. |
||
| 424 | * |
||
| 425 | * @param string $circleUniqueId |
||
| 426 | * @param string $source |
||
| 427 | * @param string $type |
||
| 428 | * @param array $payload |
||
| 429 | * @param string $broadcaster |
||
| 430 | * |
||
| 431 | * @return mixed |
||
| 432 | * @throws QueryException |
||
| 433 | */ |
||
| 434 | public static function shareToCircle( |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Circles::getSharesFromCircle(); |
||
| 449 | * |
||
| 450 | * This function will returns all item (array) shared to a specific circle identified by its Id, |
||
| 451 | * source and type. Limited to current user session. |
||
| 452 | * |
||
| 453 | * @param string $circleUniqueId |
||
| 454 | * |
||
| 455 | * @return mixed |
||
| 456 | * @throws QueryException |
||
| 457 | */ |
||
| 458 | public static function getSharesFromCircle($circleUniqueId) { |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Circles::linkCircle(); |
||
| 468 | * |
||
| 469 | * Initiate a link procedure. Current user must be at least Admin of the circle. |
||
| 470 | * circleId is the local circle and remote is the target for the link. |
||
| 471 | * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address. |
||
| 472 | * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address. |
||
| 473 | * |
||
| 474 | * @param string $circleUniqueId |
||
| 475 | * @param string $remote |
||
| 476 | * |
||
| 477 | * @return FederatedLink |
||
| 478 | * @throws QueryException |
||
| 479 | */ |
||
| 480 | public static function linkCircle($circleUniqueId, $remote) { |
||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * Circles::generateLink(); |
||
| 490 | * |
||
| 491 | * Returns the link to get access to a local circle. |
||
| 492 | * |
||
| 493 | * @param string $circleUniqueId |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public static function generateLink($circleUniqueId) { |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * Circles::generateAbsoluteLink(); |
||
| 505 | * |
||
| 506 | * Returns the absolute link to get access to a local circle. |
||
| 507 | * |
||
| 508 | * @param string $circleUniqueId |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public static function generateAbsoluteLink($circleUniqueId) { |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * Circles::generateRemoteLink(); |
||
| 520 | * |
||
| 521 | * Returns the link to get access to a remote circle. |
||
| 522 | * |
||
| 523 | * @param FederatedLink $link |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public static function generateRemoteLink(FederatedLink $link) { |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * @param SharingFrame $frame |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public static function generateUserParameter(SharingFrame $frame) { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * @param SharingFrame $frame |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | public static function generateCircleParameter(SharingFrame $frame) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get a list of objects which are shred with $circleUniqueId. |
||
| 576 | * |
||
| 577 | * @since 0.14.0 |
||
| 578 | * |
||
| 579 | * @param array $circleUniqueIds |
||
| 580 | * |
||
| 581 | * @return string[] array of object ids or empty array if none found |
||
| 582 | * @throws QueryException |
||
| 583 | */ |
||
| 584 | public static function getFilesForCircles($circleUniqueIds) { |
||
| 590 | } |
||
| 591 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.