Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Client |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var ServiceInterface $service |
||
| 19 | */ |
||
| 20 | protected $service; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Initiate this class with a subclass of ServiceInterface. There are two |
||
| 24 | * service subclasses available: |
||
| 25 | * - Service\REST: Service which makes calls to the live Strava API |
||
| 26 | * - Service\Stub: Service stub for test purposes (unit tests) |
||
| 27 | * |
||
| 28 | * @param ServiceInterface $service |
||
| 29 | */ |
||
| 30 | 79 | public function __construct(ServiceInterface $service) |
|
| 31 | { |
||
| 32 | 79 | $this->service = $service; |
|
| 33 | 79 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Retrieve current athlete |
||
| 37 | * |
||
| 38 | * @link https://strava.github.io/api/v3/athlete/#get-details, |
||
| 39 | * https://strava.github.io/api/v3/athlete/#get-another-details |
||
| 40 | * @param int $id |
||
| 41 | * @return array |
||
| 42 | * @throws Exception |
||
| 43 | */ |
||
| 44 | 2 | public function getAthlete($id = null) |
|
| 45 | { |
||
| 46 | try { |
||
| 47 | 2 | return $this->service->getAthlete($id); |
|
| 48 | 1 | } catch (ServiceException $e) { |
|
| 49 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Retrieve athlete stats |
||
| 55 | * |
||
| 56 | * Only available for the authenticated athlete. |
||
| 57 | * |
||
| 58 | * @link https://strava.github.io/api/v3/athlete/#stats |
||
| 59 | * @param int $id |
||
| 60 | * @return array |
||
| 61 | * @throws ClientException |
||
| 62 | */ |
||
| 63 | 1 | public function getAthleteStats($id) |
|
| 64 | { |
||
| 65 | try { |
||
| 66 | 1 | return $this->service->getAthleteStats($id); |
|
| 67 | } catch (ServiceException $e) { |
||
| 68 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Retrieve athlete routes |
||
| 74 | * |
||
| 75 | * @link https://strava.github.io/api/v3/routes/#list |
||
| 76 | * @param int $id |
||
| 77 | * @return array |
||
| 78 | * @throws ClientException |
||
| 79 | */ |
||
| 80 | 1 | public function getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null) |
|
| 81 | { |
||
| 82 | try { |
||
| 83 | 1 | return $this->service->getAthleteRoutes($id, $type, $after, $page, $per_page); |
|
| 84 | } catch (ServiceException $e) { |
||
| 85 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * List athlete clubs |
||
| 91 | * |
||
| 92 | * @link https://strava.github.io/api/v3/clubs/#get-athletes |
||
| 93 | * @return array |
||
| 94 | * @throws Exception |
||
| 95 | */ |
||
| 96 | 2 | public function getAthleteClubs() |
|
| 97 | { |
||
| 98 | try { |
||
| 99 | 2 | return $this->service->getAthleteClubs(); |
|
| 100 | 1 | } catch (ServiceException $e) { |
|
| 101 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * List athlete activities |
||
| 107 | * |
||
| 108 | * @link https://strava.github.io/api/v3/activities/#get-activities |
||
| 109 | * @param string $before |
||
| 110 | * @param string $after |
||
| 111 | * @param int $page |
||
| 112 | * @param int $per_page |
||
| 113 | * @return array |
||
| 114 | * @throws Exception |
||
| 115 | */ |
||
| 116 | 2 | public function getAthleteActivities($before = null, $after = null, $page = null, $per_page = null) |
|
| 117 | { |
||
| 118 | try { |
||
| 119 | 2 | return $this->service->getAthleteActivities($before, $after, $page, $per_page); |
|
| 120 | 1 | } catch (ServiceException $e) { |
|
| 121 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * List athlete friends |
||
| 127 | * |
||
| 128 | * @link https://strava.github.io/api/v3/follow/#friends |
||
| 129 | * @param int $id |
||
| 130 | * @param int $page |
||
| 131 | * @param int $per_page |
||
| 132 | * @return array |
||
| 133 | * @throws Exception |
||
| 134 | */ |
||
| 135 | 2 | public function getAthleteFriends($id = null, $page = null, $per_page = null) |
|
| 136 | { |
||
| 137 | try { |
||
| 138 | 2 | return $this->service->getAthleteFriends($id, $page, $per_page); |
|
| 139 | 1 | } catch (ServiceException $e) { |
|
| 140 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * List athlete followers |
||
| 146 | * |
||
| 147 | * @link https://strava.github.io/api/v3/follow/#followers |
||
| 148 | * @param int $id |
||
| 149 | * @param int $page |
||
| 150 | * @param int $per_page |
||
| 151 | * @return array |
||
| 152 | * @throws Exception |
||
| 153 | */ |
||
| 154 | 2 | public function getAthleteFollowers($id = null, $page = null, $per_page = null) |
|
| 155 | { |
||
| 156 | try { |
||
| 157 | 2 | return $this->service->getAthleteFollowers($id, $page, $per_page); |
|
| 158 | 1 | } catch (ServiceException $e) { |
|
| 159 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * List both following |
||
| 165 | * |
||
| 166 | * @link https://strava.github.io/api/v3/follow/#both |
||
| 167 | * @param int $id |
||
| 168 | * @param int $page |
||
| 169 | * @param int $per_page |
||
| 170 | * @return array |
||
| 171 | * @throws Exception |
||
| 172 | */ |
||
| 173 | 2 | public function getAthleteBothFollowing($id, $page = null, $per_page = null) |
|
| 174 | { |
||
| 175 | try { |
||
| 176 | 2 | return $this->service->getAthleteBothFollowing($id, $page, $per_page); |
|
| 177 | 1 | } catch (ServiceException $e) { |
|
| 178 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * List athlete K/QOMs/CRs |
||
| 184 | * |
||
| 185 | * @link https://strava.github.io/api/v3/athlete/#koms |
||
| 186 | * @param int $id |
||
| 187 | * @param int $page |
||
| 188 | * @param int $per_page |
||
| 189 | * @return array |
||
| 190 | * @throws Exception |
||
| 191 | */ |
||
| 192 | 2 | public function getAthleteKom($id, $page = null, $per_page = null) |
|
| 193 | { |
||
| 194 | try { |
||
| 195 | 2 | return $this->service->getAthleteKom($id, $page, $per_page); |
|
| 196 | 1 | } catch (ServiceException $e) { |
|
| 197 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * List athlete zones |
||
| 203 | * |
||
| 204 | * @link https://strava.github.io/api/v3/athlete/#zones |
||
| 205 | * @return array |
||
| 206 | * @throws Exception |
||
| 207 | */ |
||
| 208 | 2 | public function getAthleteZones() |
|
| 209 | { |
||
| 210 | try { |
||
| 211 | 2 | return $this->service->getAthleteZones(); |
|
| 212 | 1 | } catch (ServiceException $e) { |
|
| 213 | 1 | throw new ClientException('[SERVICE] ' . $e->getMessage()); |
|
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * List starred segment |
||
| 220 | * |
||
| 221 | * @link https://strava.github.io/api/v3/segments/#starred |
||
| 222 | * @param int $id |
||
| 223 | * @param int $page |
||
| 224 | * @param int $per_page |
||
| 225 | * @return array |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | 2 | public function getAthleteStarredSegments($id = null, $page = null, $per_page = null) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Update current athlete |
||
| 239 | * |
||
| 240 | * @link https://strava.github.io/api/v3/athlete/#update |
||
| 241 | * @param string $city |
||
| 242 | * @param string $state |
||
| 243 | * @param string $country |
||
| 244 | * @param string $sex |
||
| 245 | * @param float $weight |
||
| 246 | * @return array |
||
| 247 | * @throws Exception |
||
| 248 | */ |
||
| 249 | 2 | public function updateAthlete($city, $state, $country, $sex, $weight) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Retrieve an activity |
||
| 260 | * |
||
| 261 | * @link https://strava.github.io/api/v3/athlete/#get-details, |
||
| 262 | * https://strava.github.io/api/v3/athlete/#get-another-details |
||
| 263 | * @param int $id |
||
| 264 | * @param boolean $include_all_efforts |
||
| 265 | * @return array |
||
| 266 | * @throws Exception |
||
| 267 | */ |
||
| 268 | 2 | public function getActivity($id, $include_all_efforts = null) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * List activity comments |
||
| 279 | * |
||
| 280 | * @link https://strava.github.io/api/v3/comments/#list |
||
| 281 | * @param int $id |
||
| 282 | * @param boolean $markdown |
||
| 283 | * @param int $page |
||
| 284 | * @param int $per_page |
||
| 285 | * @return array |
||
| 286 | * @throws Exception |
||
| 287 | */ |
||
| 288 | 2 | public function getActivityComments($id, $markdown = null, $page = null, $per_page = null) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * List activity kudoers |
||
| 299 | * |
||
| 300 | * @link https://strava.github.io/api/v3/kudos/#list |
||
| 301 | * @param int $id |
||
| 302 | * @param int $page |
||
| 303 | * @param int $per_page |
||
| 304 | * @return array |
||
| 305 | * @throws Exception |
||
| 306 | */ |
||
| 307 | 2 | public function getActivityKudos($id, $page = null, $per_page = null) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * List activity photos |
||
| 318 | * |
||
| 319 | * @link https://strava.github.io/api/v3/photos/#list |
||
| 320 | * @param int $id |
||
| 321 | * @param int $size In pixels. |
||
| 322 | * @param string $photo_sources Must be "true". |
||
| 323 | * @return array |
||
| 324 | * @throws Exception |
||
| 325 | */ |
||
| 326 | 2 | public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true') |
|
| 334 | |||
| 335 | /** |
||
| 336 | * List activity zones |
||
| 337 | * |
||
| 338 | * @link https://strava.github.io/api/v3/activities/#zones |
||
| 339 | * @param int $id |
||
| 340 | * @return array |
||
| 341 | * @throws Exception |
||
| 342 | */ |
||
| 343 | 2 | public function getActivityZones($id) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * List activity laps |
||
| 354 | * |
||
| 355 | * @link https://strava.github.io/api/v3/activities/#laps |
||
| 356 | * @param int $id |
||
| 357 | * @return array |
||
| 358 | * @throws Exception |
||
| 359 | */ |
||
| 360 | 2 | public function getActivityLaps($id) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Check upload status |
||
| 371 | * |
||
| 372 | * @link https://strava.github.io/api/v3/uploads/#get-status |
||
| 373 | * @param int $id |
||
| 374 | * @return array |
||
| 375 | * @throws Exception |
||
| 376 | */ |
||
| 377 | 2 | public function getActivityUploadStatus($id) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Create an activity |
||
| 388 | * |
||
| 389 | * @link https://strava.github.io/api/v3/activities/#create |
||
| 390 | * @param string $name |
||
| 391 | * @param string $type |
||
| 392 | * @param string $start_date_local |
||
| 393 | * @param int $elapsed_time |
||
| 394 | * @param string $description |
||
| 395 | * @param float $distance |
||
| 396 | * @param int $private |
||
| 397 | * @param int $trainer |
||
| 398 | * @return array |
||
| 399 | * @throws Exception |
||
| 400 | */ |
||
| 401 | 2 | public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null, $private = null, $trainer = null) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Upload an activity |
||
| 412 | * |
||
| 413 | * @link https://strava.github.io/api/v3/uploads/#post-file |
||
| 414 | * @param mixed $file |
||
| 415 | * @param string $activity_type |
||
| 416 | * @param string $name |
||
| 417 | * @param string $description |
||
| 418 | * @param int $private |
||
| 419 | * @param int $trainer |
||
| 420 | * @param int $commute |
||
| 421 | * @param string $data_type |
||
| 422 | * @param string $external_id |
||
| 423 | * @return array |
||
| 424 | * @throws Exception |
||
| 425 | */ |
||
| 426 | 2 | public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $commute = null, $data_type = null, $external_id = null) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Update an activity |
||
| 437 | * |
||
| 438 | * @link https://strava.github.io/api/v3/activities/#put-updates |
||
| 439 | * @param int $id |
||
| 440 | * @param string $name |
||
| 441 | * @param string $type |
||
| 442 | * @param boolean $private |
||
| 443 | * @param boolean $commute |
||
| 444 | * @param boolean $trainer |
||
| 445 | * @param string $gear_id |
||
| 446 | * @param string $description |
||
| 447 | * @return array |
||
| 448 | * @throws Exception |
||
| 449 | */ |
||
| 450 | 2 | public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Delete an activity |
||
| 461 | * |
||
| 462 | * @link https://strava.github.io/api/v3/activities/#delete |
||
| 463 | * @param int $id |
||
| 464 | * @return array |
||
| 465 | * @throws Exception |
||
| 466 | */ |
||
| 467 | 2 | public function deleteActivity($id) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Retrieve gear |
||
| 478 | * |
||
| 479 | * @link https://strava.github.io/api/v3/gear/ |
||
| 480 | * @param int $id |
||
| 481 | * @return array |
||
| 482 | * @throws Exception |
||
| 483 | */ |
||
| 484 | 2 | public function getGear($id) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Retrieve a club |
||
| 495 | * |
||
| 496 | * @link https://strava.github.io/api/v3/clubs/#get-details |
||
| 497 | * @param int $id |
||
| 498 | * @return array |
||
| 499 | * @throws Exception |
||
| 500 | */ |
||
| 501 | 2 | public function getClub($id) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * List club members |
||
| 512 | * |
||
| 513 | * @link https://strava.github.io/api/v3/clubs/#get-members |
||
| 514 | * @param int $id |
||
| 515 | * @param int $page |
||
| 516 | * @param int $per_page |
||
| 517 | * @return array |
||
| 518 | * @throws Exception |
||
| 519 | */ |
||
| 520 | 2 | public function getClubMembers($id, $page = null, $per_page = null) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * List club activities |
||
| 531 | * |
||
| 532 | * @link https://strava.github.io/api/v3/clubs/#get-activities |
||
| 533 | * @param int $id |
||
| 534 | * @param int $page |
||
| 535 | * @param int $per_page |
||
| 536 | * @return array |
||
| 537 | * @throws Exception |
||
| 538 | */ |
||
| 539 | 2 | public function getClubActivities($id, $page = null, $per_page = null) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * List club announcements |
||
| 550 | * |
||
| 551 | * @link https://strava.github.io/api/v3/clubs/#get-announcements |
||
| 552 | * @param int $id |
||
| 553 | * @return array |
||
| 554 | * @throws Exception |
||
| 555 | */ |
||
| 556 | 2 | public function getClubAnnouncements($id) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * List club group events |
||
| 567 | * |
||
| 568 | * @link https://strava.github.io/api/v3/clubs/#get-group-events |
||
| 569 | * @param int $id |
||
| 570 | * @return array |
||
| 571 | * @throws Exception |
||
| 572 | */ |
||
| 573 | 2 | public function getClubGroupEvents($id) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Join a club |
||
| 584 | * |
||
| 585 | * @link https://strava.github.io/api/v3/clubs/#join |
||
| 586 | * @param int $id |
||
| 587 | * @return array |
||
| 588 | * @throws Exception |
||
| 589 | */ |
||
| 590 | 2 | public function joinClub($id) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Leave a club |
||
| 601 | * |
||
| 602 | * @link https://strava.github.io/api/v3/clubs/#leave |
||
| 603 | * @param int $id |
||
| 604 | * @return array |
||
| 605 | * @throws Exception |
||
| 606 | */ |
||
| 607 | 2 | public function leaveClub($id) |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Get route details |
||
| 618 | * |
||
| 619 | * @link https://strava.github.io/api/v3/routes/#list |
||
| 620 | * @param int $id |
||
| 621 | * @return array |
||
| 622 | * @throws Exception |
||
| 623 | */ |
||
| 624 | 2 | public function getRoute($id) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Retrieve a segment |
||
| 635 | * |
||
| 636 | * @link https://strava.github.io/api/v3/segments/#retrieve |
||
| 637 | * @param int $id |
||
| 638 | * @return array |
||
| 639 | * @throws Exception |
||
| 640 | */ |
||
| 641 | 2 | public function getSegment($id) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Segment leaderboards |
||
| 652 | * |
||
| 653 | * @link https://strava.github.io/api/v3/segments/#leaderboard |
||
| 654 | * @param int $id |
||
| 655 | * @param string $gender |
||
| 656 | * @param string $age_group |
||
| 657 | * @param string $weight_class |
||
| 658 | * @param boolean $following |
||
| 659 | * @param int $club_id |
||
| 660 | * @param string $date_range |
||
| 661 | * @param int $context_entries |
||
| 662 | * @param int $page |
||
| 663 | * @param int $per_page |
||
| 664 | * @return array |
||
| 665 | * @throws Exception |
||
| 666 | */ |
||
| 667 | 2 | public function getSegmentLeaderboard($id, $gender = null, $age_group = null, $weight_class = null, $following = null, $club_id = null, $date_range = null, $context_entries = null, $page = null, $per_page = null) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Segment explorer |
||
| 678 | * |
||
| 679 | * @link https://strava.github.io/api/v3/segments/#explore |
||
| 680 | * @param string $bounds |
||
| 681 | * @param string $activity_type |
||
| 682 | * @param int $min_cat |
||
| 683 | * @param int $max_cat |
||
| 684 | * @return array |
||
| 685 | * @throws Exception |
||
| 686 | */ |
||
| 687 | 2 | public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * List efforts filtered by athlete and/or a date range |
||
| 698 | * |
||
| 699 | * @link https://strava.github.io/api/v3/segments/#efforts |
||
| 700 | * @param int $id |
||
| 701 | * @param int $athlete_id |
||
| 702 | * @param string $start_date_local |
||
| 703 | * @param string $end_date_local |
||
| 704 | * @param int $page |
||
| 705 | * @param int $per_page |
||
| 706 | * @return array |
||
| 707 | * @throws Exception |
||
| 708 | */ |
||
| 709 | 2 | public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Retrieve activity streams |
||
| 720 | * |
||
| 721 | * @link https://strava.github.io/api/v3/streams/#activity |
||
| 722 | * @param int $id |
||
| 723 | * @param string $types |
||
| 724 | * @param string $resolution |
||
| 725 | * @param string $series_type |
||
| 726 | * @return array |
||
| 727 | * @throws Exception |
||
| 728 | */ |
||
| 729 | 2 | public function getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance') |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Retrieve effort streams |
||
| 740 | * |
||
| 741 | * @link https://strava.github.io/api/v3/streams/#effort |
||
| 742 | * @param int $id |
||
| 743 | * @param string $types |
||
| 744 | * @param string $resolution |
||
| 745 | * @param string $series_type |
||
| 746 | * @return array |
||
| 747 | * @throws Exception |
||
| 748 | */ |
||
| 749 | 2 | public function getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance') |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Retrieve segment streams |
||
| 760 | * @link https://strava.github.io/api/v3/streams/#segment |
||
| 761 | * @param int $id |
||
| 762 | * @param string $types |
||
| 763 | * @param string $resolution |
||
| 764 | * @param string $series_type |
||
| 765 | * @return array |
||
| 766 | * @throws Exception |
||
| 767 | */ |
||
| 768 | 2 | public function getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance') |
|
| 776 | |||
| 777 | /** |
||
| 778 | * Retrieve route streams |
||
| 779 | * |
||
| 780 | * @link https://strava.github.io/api/v3/streams/#routes |
||
| 781 | * @param int $id |
||
| 782 | * @return array |
||
| 783 | * @throws Exception |
||
| 784 | */ |
||
| 785 | 2 | public function getStreamsRoute($id) |
|
| 793 | } |
||
| 794 |