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 AbstractApi 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 AbstractApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class AbstractApi |
||
| 14 | { |
||
| 15 | |||
| 16 | /** API version */ |
||
| 17 | const API_VERSION = 'v3'; |
||
| 18 | |||
| 19 | /** API constants */ |
||
| 20 | const API_URL = 'https://api.github.com'; |
||
| 21 | const API_UPLOADS = 'https://uploads.github.com'; |
||
| 22 | const API_RAW_URL = 'https://raw.github.com'; |
||
| 23 | const CONTENT_TYPE = 'application/json'; |
||
| 24 | const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json'; |
||
| 25 | const USER_AGENT = 'FlexyProject-GitHubAPI'; |
||
| 26 | |||
| 27 | /** Archive constants */ |
||
| 28 | const ARCHIVE_TARBALL = 'tarball'; |
||
| 29 | const ARCHIVE_ZIPBALL = 'zipball'; |
||
| 30 | |||
| 31 | /** Authentication constants */ |
||
| 32 | const OAUTH_AUTH = 0; |
||
| 33 | const OAUTH2_HEADER_AUTH = 1; |
||
| 34 | const OAUTH2_PARAMETERS_AUTH = 2; |
||
| 35 | |||
| 36 | /** Branch constants */ |
||
| 37 | const BRANCH_MASTER = 'master'; |
||
| 38 | const BRANCH_DEVELOP = 'develop'; |
||
| 39 | |||
| 40 | /** Direction constants */ |
||
| 41 | const DIRECTION_ASC = 'asc'; |
||
| 42 | const DIRECTION_DESC = 'desc'; |
||
| 43 | |||
| 44 | /** Environment constants */ |
||
| 45 | const ENVIRONMENT_PRODUCTION = 'production'; |
||
| 46 | const ENVIRONMENT_STAGING = 'staging'; |
||
| 47 | const ENVIRONMENT_QA = 'qa'; |
||
| 48 | |||
| 49 | /** Events constants */ |
||
| 50 | const EVENTS_PULL = 'pull'; |
||
| 51 | const EVENTS_PULL_REQUEST = 'pull_request'; |
||
| 52 | const EVENTS_PUSH = 'push'; |
||
| 53 | |||
| 54 | /** Filter constants */ |
||
| 55 | const FILTER_ALL = 'all'; |
||
| 56 | const FILTER_ASSIGNED = 'assigned'; |
||
| 57 | const FILTER_CREATED = 'created'; |
||
| 58 | const FILTER_MENTIONED = 'mentioned'; |
||
| 59 | const FILTER_SUBSCRIBED = 'subscribed'; |
||
| 60 | |||
| 61 | /** Media types constants */ |
||
| 62 | const MEDIA_TYPE_JSON = 'json'; |
||
| 63 | const MEDIA_TYPE_RAW = 'raw'; |
||
| 64 | const MEDIA_TYPE_FULL = 'full'; |
||
| 65 | const MEDIA_TYPE_TEXT = 'text'; |
||
| 66 | |||
| 67 | /** Modes constants */ |
||
| 68 | const MODE_MARKDOWN = 'markdown'; |
||
| 69 | const MODE_GFM = 'gfm'; |
||
| 70 | |||
| 71 | /** Permissions constants */ |
||
| 72 | const PERMISSION_ADMIN = 'admin'; |
||
| 73 | const PERMISSION_PULL = 'pull'; |
||
| 74 | const PERMISSION_PUSH = 'push'; |
||
| 75 | |||
| 76 | /** Sort constants */ |
||
| 77 | const SORT_COMPLETENESS = 'completeness'; |
||
| 78 | const SORT_CREATED = 'created'; |
||
| 79 | const SORT_DUE_DATE = 'due_date'; |
||
| 80 | const SORT_FULL_NAME = 'full_name'; |
||
| 81 | const SORT_NEWEST = 'newest'; |
||
| 82 | const SORT_OLDEST = 'oldest'; |
||
| 83 | const SORT_PUSHED = 'pushed'; |
||
| 84 | const SORT_STARGAZERS = 'stargazers'; |
||
| 85 | const SORT_UPDATED = 'updated'; |
||
| 86 | |||
| 87 | /** State constants */ |
||
| 88 | const STATE_ACTIVE = 'active'; |
||
| 89 | const STATE_ALL = 'all'; |
||
| 90 | const STATE_CLOSED = 'closed'; |
||
| 91 | const STATE_ERROR = 'error'; |
||
| 92 | const STATE_FAILURE = 'failure'; |
||
| 93 | const STATE_OPEN = 'open'; |
||
| 94 | const STATE_PENDING = 'pending'; |
||
| 95 | const STATE_SUCCESS = 'success'; |
||
| 96 | |||
| 97 | /** Task constants */ |
||
| 98 | const TASK_DEPLOY = 'deploy'; |
||
| 99 | const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations'; |
||
| 100 | |||
| 101 | /** Type constants */ |
||
| 102 | const TYPE_ALL = 'all'; |
||
| 103 | const TYPE_COMMENTS = 'comments'; |
||
| 104 | const TYPE_GISTS = 'gists'; |
||
| 105 | const TYPE_HOOKS = 'hooks'; |
||
| 106 | const TYPE_ISSUES = 'issues'; |
||
| 107 | const TYPE_MEMBER = 'member'; |
||
| 108 | const TYPE_MILESTONES = 'milestones'; |
||
| 109 | const TYPE_ORGS = 'orgs'; |
||
| 110 | const TYPE_OWNER = 'owner'; |
||
| 111 | const TYPE_PAGES = 'pages'; |
||
| 112 | const TYPE_PUBLIC = 'public'; |
||
| 113 | const TYPE_PULLS = 'pulls'; |
||
| 114 | const TYPE_PRIVATE = 'private'; |
||
| 115 | const TYPE_REPOS = 'repos'; |
||
| 116 | const TYPE_USERS = 'users'; |
||
| 117 | |||
| 118 | /** Properties */ |
||
| 119 | protected $accept = self::DEFAULT_ACCEPT; |
||
| 120 | protected $apiUrl = self::API_URL; |
||
| 121 | protected $authentication = self::OAUTH_AUTH; |
||
| 122 | protected $clientId; |
||
| 123 | protected $clientSecret; |
||
| 124 | protected $contentType = self::CONTENT_TYPE; |
||
| 125 | protected $failure; |
||
| 126 | protected $headers = []; |
||
| 127 | protected $httpAuth = ['username' => '', 'password' => '']; |
||
| 128 | protected $pagination; |
||
| 129 | protected $request; |
||
| 130 | protected $success; |
||
| 131 | protected $timeout = 240; |
||
| 132 | protected $token = ''; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Constructor |
||
| 136 | */ |
||
| 137 | public function __construct() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get request |
||
| 144 | * |
||
| 145 | * @return Request |
||
| 146 | */ |
||
| 147 | public function getRequest(): Request |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get accept |
||
| 154 | * |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | public function getAccept() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set accept |
||
| 164 | * |
||
| 165 | * @param array|string $accept |
||
| 166 | * |
||
| 167 | * @return AbstractApi |
||
| 168 | */ |
||
| 169 | public function setAccept($accept): AbstractApi |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get authentication |
||
| 178 | * |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function getAuthentication(): int |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set authentication |
||
| 188 | * |
||
| 189 | * @param int $authentication |
||
| 190 | * |
||
| 191 | * @return AbstractApi |
||
| 192 | */ |
||
| 193 | public function setAuthentication(int $authentication): AbstractApi |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get apiUrl |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getApiUrl(): string |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set apiUrl |
||
| 212 | * |
||
| 213 | * @param mixed $apiUrl |
||
| 214 | * |
||
| 215 | * @return AbstractApi |
||
| 216 | */ |
||
| 217 | public function setApiUrl($apiUrl): AbstractApi |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get clientId |
||
| 226 | * |
||
| 227 | * @return null|int |
||
| 228 | */ |
||
| 229 | public function getClientId() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set clientId |
||
| 236 | * |
||
| 237 | * @param mixed $clientId |
||
| 238 | * |
||
| 239 | * @return AbstractApi |
||
| 240 | */ |
||
| 241 | public function setClientId($clientId): AbstractApi |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get clientSecret |
||
| 250 | * |
||
| 251 | * @return null|string |
||
| 252 | */ |
||
| 253 | public function getClientSecret() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Set clientSecret |
||
| 260 | * |
||
| 261 | * @param mixed $clientSecret |
||
| 262 | * |
||
| 263 | * @return AbstractApi |
||
| 264 | */ |
||
| 265 | public function setClientSecret($clientSecret): AbstractApi |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get httpAuth |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function getHttpAuth(): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set httpAuth |
||
| 284 | * |
||
| 285 | * @param string $username |
||
| 286 | * @param string $password |
||
| 287 | * |
||
| 288 | * @return AbstractApi |
||
| 289 | */ |
||
| 290 | public function setHttpAuth(string $username, string $password = ''): AbstractApi |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get token |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getToken(): string |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set token |
||
| 310 | * |
||
| 311 | * @param string $token |
||
| 312 | * @param int $authentication |
||
| 313 | * |
||
| 314 | * @return AbstractApi |
||
| 315 | */ |
||
| 316 | public function setToken(string $token, int $authentication = self::OAUTH_AUTH): AbstractApi |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get timeout |
||
| 326 | * |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function getTimeout(): int |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set timeout |
||
| 336 | * |
||
| 337 | * @param int $timeout |
||
| 338 | * |
||
| 339 | * @return AbstractApi |
||
| 340 | */ |
||
| 341 | public function setTimeout(int $timeout): AbstractApi |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get contentType |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getContentType(): string |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set contentType |
||
| 360 | * |
||
| 361 | * @param string $contentType |
||
| 362 | * |
||
| 363 | * @return AbstractApi |
||
| 364 | */ |
||
| 365 | public function setContentType(string $contentType): AbstractApi |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get headers |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function getHeaders(): array |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get pagination |
||
| 384 | * |
||
| 385 | * @return Pagination|null |
||
| 386 | */ |
||
| 387 | public function getPagination() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set pagination |
||
| 394 | * |
||
| 395 | * @param Pagination $pagination |
||
| 396 | * |
||
| 397 | * @return AbstractApi |
||
| 398 | */ |
||
| 399 | public function setPagination(Pagination $pagination): AbstractApi |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Curl request |
||
| 408 | * |
||
| 409 | * @param string $url |
||
| 410 | * @param string $method |
||
| 411 | * @param array $postFields |
||
| 412 | * @param null|string $apiUrl |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [], |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Return a formatted string. Modified version of sprintf using colon(:) |
||
| 598 | * |
||
| 599 | * @param string $string |
||
| 600 | * @param array $params |
||
| 601 | * |
||
| 602 | * @return String |
||
| 603 | * @throws Exception |
||
| 604 | */ |
||
| 605 | public function sprintf(string $string, ...$params): string |
||
| 634 | } |