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 AbstractProvider 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 AbstractProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class AbstractProvider implements ProviderInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Provider name. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The HTTP request instance. |
||
| 38 | * |
||
| 39 | * @var \Symfony\Component\HttpFoundation\Request |
||
| 40 | */ |
||
| 41 | protected $request; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The client ID. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $clientId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The client secret. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $clientSecret; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Overtrue\Socialite\AccessTokenInterface |
||
| 59 | */ |
||
| 60 | protected $accessToken; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The redirect URL. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $redirectUrl; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The custom parameters to be sent with the request. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $parameters = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The scopes being requested. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $scopes = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The separating character for the requested scopes. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $scopeSeparator = ','; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The type of the encoding in the query. |
||
| 92 | * |
||
| 93 | * @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738 |
||
| 94 | */ |
||
| 95 | protected $encodingType = PHP_QUERY_RFC1738; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Indicates if the session state should be utilized. |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $stateless = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The options for guzzle\client. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected static $guzzleOptions = ['http_errors' => false]; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Create a new provider instance. |
||
| 113 | * |
||
| 114 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 115 | * @param string $clientId |
||
| 116 | * @param string $clientSecret |
||
| 117 | * @param string|null $redirectUrl |
||
| 118 | */ |
||
| 119 | public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl = null) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the authentication URL for the provider. |
||
| 129 | * |
||
| 130 | * @param string $state |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | abstract protected function getAuthUrl($state); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the token URL for the provider. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | abstract protected function getTokenUrl(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the raw user for the given access token. |
||
| 145 | * |
||
| 146 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | abstract protected function getUserByToken(AccessTokenInterface $token); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Map the raw user array to a Socialite User instance. |
||
| 154 | * |
||
| 155 | * @param array $user |
||
| 156 | * |
||
| 157 | * @return \Overtrue\Socialite\User |
||
| 158 | */ |
||
| 159 | abstract protected function mapUserToObject(array $user); |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Redirect the user of the application to the provider's authentication screen. |
||
| 163 | * |
||
| 164 | * @param string $redirectUrl |
||
| 165 | * |
||
| 166 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 167 | */ |
||
| 168 | public function redirect($redirectUrl = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function user(AccessTokenInterface $token = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set redirect url. |
||
| 203 | * |
||
| 204 | * @param string $redirectUrl |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setRedirectUrl($redirectUrl) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set redirect url. |
||
| 217 | * |
||
| 218 | * @param string $redirectUrl |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function withRedirectUrl($redirectUrl) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Return the redirect url. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getRedirectUrl() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param \Overtrue\Socialite\AccessTokenInterface $accessToken |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setAccessToken(AccessTokenInterface $accessToken) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the access token for the given code. |
||
| 253 | * |
||
| 254 | * @param string $code |
||
| 255 | * |
||
| 256 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 257 | */ |
||
| 258 | public function getAccessToken($code) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the scopes of the requested access. |
||
| 276 | * |
||
| 277 | * @param array $scopes |
||
| 278 | * |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function scopes(array $scopes) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set the request instance. |
||
| 290 | * |
||
| 291 | * @param Request $request |
||
| 292 | * |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | public function setRequest(Request $request) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the request instance. |
||
| 304 | * |
||
| 305 | * @return \Symfony\Component\HttpFoundation\Request |
||
| 306 | */ |
||
| 307 | public function getRequest() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Indicates that the provider should operate as stateless. |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function stateless() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set the custom parameters of the request. |
||
| 326 | * |
||
| 327 | * @param array $parameters |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function with(array $parameters) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getName() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the authentication URL for the provider. |
||
| 352 | * |
||
| 353 | * @param string $url |
||
| 354 | * @param string $state |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function buildAuthUrlFromBase($url, $state) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the GET parameters for the code request. |
||
| 365 | * |
||
| 366 | * @param string|null $state |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | protected function getCodeFields($state = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Format the given scopes. |
||
| 388 | * |
||
| 389 | * @param array $scopes |
||
| 390 | * @param string $scopeSeparator |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Determine if the current request / session has a mismatching "state". |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | protected function hasInvalidState() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the POST fields for the token request. |
||
| 417 | * |
||
| 418 | * @param string $code |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | protected function getTokenFields($code) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the access token from the token response body. |
||
| 434 | * |
||
| 435 | * @param \Psr\Http\Message\StreamInterface|array $body |
||
| 436 | * |
||
| 437 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 438 | */ |
||
| 439 | protected function parseAccessToken($body) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get the code from the request. |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | protected function getCode() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Get a fresh instance of the Guzzle HTTP client. |
||
| 464 | * |
||
| 465 | * @return \GuzzleHttp\Client |
||
| 466 | */ |
||
| 467 | protected function getHttpClient() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set options for Guzzle HTTP client. |
||
| 474 | * |
||
| 475 | * @param array $config |
||
| 476 | */ |
||
| 477 | public static function setGuzzleOptions($config = []) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Determine if the provider is operating with state. |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | protected function usesState() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Determine if the provider is operating as stateless. |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | protected function isStateless() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Return array item by key. |
||
| 504 | * |
||
| 505 | * @param array $array |
||
| 506 | * @param string $key |
||
| 507 | * @param mixed $default |
||
| 508 | * |
||
| 509 | * @return mixed |
||
| 510 | */ |
||
| 511 | View Code Duplication | protected function arrayItem(array $array, $key, $default = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Put state to session storage and return it. |
||
| 534 | * |
||
| 535 | * @return string|bool |
||
| 536 | */ |
||
| 537 | protected function makeState() |
||
| 552 | } |
||
| 553 |