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 CirclesService 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 CirclesService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class CirclesService { |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $userId; |
||
| 46 | |||
| 47 | /** @var IL10N */ |
||
| 48 | private $l10n; |
||
| 49 | |||
| 50 | /** @var ConfigService */ |
||
| 51 | private $configService; |
||
| 52 | |||
| 53 | /** @var CirclesRequest */ |
||
| 54 | private $circlesRequest; |
||
| 55 | |||
| 56 | /** @var MembersRequest */ |
||
| 57 | private $membersRequest; |
||
| 58 | |||
| 59 | /** @var CirclesMapper */ |
||
| 60 | private $dbCircles; |
||
| 61 | |||
| 62 | /** @var MembersMapper */ |
||
| 63 | private $dbMembers; |
||
| 64 | |||
| 65 | /** @var EventsService */ |
||
| 66 | private $eventsService; |
||
| 67 | |||
| 68 | /** @var MiscService */ |
||
| 69 | private $miscService; |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * CirclesService constructor. |
||
| 74 | * |
||
| 75 | * @param $userId |
||
| 76 | * @param IL10N $l10n |
||
| 77 | * @param ConfigService $configService |
||
| 78 | * @param CirclesRequest $circlesRequest |
||
| 79 | * @param MembersRequest $membersRequest |
||
| 80 | * @param DatabaseService $databaseService |
||
| 81 | * @param EventsService $eventsService |
||
| 82 | * @param MiscService $miscService |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function __construct( |
|
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Create circle using this->userId as owner |
||
| 109 | * |
||
| 110 | * @param int $type |
||
| 111 | * @param string $name |
||
| 112 | * |
||
| 113 | * @return Circle |
||
| 114 | * @throws CircleTypeDisabledException |
||
| 115 | * @throws \Exception |
||
| 116 | */ |
||
| 117 | public function createCircle($type, $name) { |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 157 | * |
||
| 158 | * @param $type |
||
| 159 | * @param string $name |
||
| 160 | * @param int $level |
||
| 161 | * |
||
| 162 | * @return Circle[] |
||
| 163 | * @throws CircleTypeDisabledException |
||
| 164 | */ |
||
| 165 | public function listCircles($type, $name = '', $level = 0) { |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * returns details on circle and its members if this->userId is a member itself. |
||
| 186 | * |
||
| 187 | * @param $circleId |
||
| 188 | * |
||
| 189 | * @return Circle |
||
| 190 | * @throws \Exception |
||
| 191 | ] */ |
||
| 192 | public function detailsCircle($circleId) { |
||
| 211 | |||
| 212 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 220 | |||
| 221 | |||
| 222 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * save new settings if current user is admin. |
||
| 239 | * |
||
| 240 | * @param $circleId |
||
| 241 | * @param array $settings |
||
| 242 | * |
||
| 243 | * @return Circle |
||
| 244 | * @throws \Exception |
||
| 245 | */ |
||
| 246 | public function settingsCircle($circleId, $settings) { |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Join a circle. |
||
| 269 | * |
||
| 270 | * @param $circleId |
||
| 271 | * |
||
| 272 | * @return null|Member |
||
| 273 | * @throws \Exception |
||
| 274 | */ |
||
| 275 | View Code Duplication | public function joinCircle($circleId) { |
|
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Leave a circle. |
||
| 301 | * |
||
| 302 | * @param $circleId |
||
| 303 | * |
||
| 304 | * @return null|Member |
||
| 305 | * @throws \Exception |
||
| 306 | */ |
||
| 307 | public function leaveCircle($circleId) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * destroy a circle. |
||
| 332 | * |
||
| 333 | * @param int $circleId |
||
| 334 | * |
||
| 335 | * @throws MemberIsNotOwnerException |
||
| 336 | */ |
||
| 337 | public function removeCircle($circleId) { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $circleName |
||
| 357 | * |
||
| 358 | * @return Circle|null |
||
| 359 | */ |
||
| 360 | public function infoCircleByName($circleName) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Convert a Type in String to its Bit Value |
||
| 366 | * |
||
| 367 | * @param $type |
||
| 368 | * |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public static function convertTypeStringToBitValue(& $type) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * getCircleIcon() |
||
| 394 | * |
||
| 395 | * Return the right imagePath for a type of circle. |
||
| 396 | * |
||
| 397 | * @param string $type |
||
| 398 | * @param bool $png |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public static function getCircleIcon($type, $png = false) { |
||
| 431 | |||
| 432 | } |
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.