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 |
||
| 28 | abstract class AbstractProvider implements ProviderInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Provider name. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $name; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The HTTP request instance. |
||
| 39 | * |
||
| 40 | * @var \Symfony\Component\HttpFoundation\Request |
||
| 41 | */ |
||
| 42 | protected $request; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Driver config. |
||
| 46 | * |
||
| 47 | * @var Config |
||
| 48 | */ |
||
| 49 | protected $config; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The client ID. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $clientId; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The client secret. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $clientSecret; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Overtrue\Socialite\AccessTokenInterface |
||
| 67 | */ |
||
| 68 | protected $accessToken; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The redirect URL. |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $redirectUrl; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The custom parameters to be sent with the request. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $parameters = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The scopes being requested. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $scopes = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The separating character for the requested scopes. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $scopeSeparator = ','; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The type of the encoding in the query. |
||
| 100 | * |
||
| 101 | * @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738 |
||
| 102 | */ |
||
| 103 | protected $encodingType = PHP_QUERY_RFC1738; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Indicates if the session state should be utilized. |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $stateless = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The options for guzzle\client. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected static $guzzleOptions = ['http_errors' => false]; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create a new provider instance. |
||
| 121 | * |
||
| 122 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 123 | * @param array $config |
||
| 124 | */ |
||
| 125 | public function __construct(Request $request, $config) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the authentication URL for the provider. |
||
| 134 | * |
||
| 135 | * @param string $state |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | abstract protected function getAuthUrl($state); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the token URL for the provider. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | abstract protected function getTokenUrl(); |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the raw user for the given access token. |
||
| 150 | * |
||
| 151 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | abstract protected function getUserByToken(AccessTokenInterface $token); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Map the raw user array to a Socialite User instance. |
||
| 159 | * |
||
| 160 | * @param array $user |
||
| 161 | * |
||
| 162 | * @return \Overtrue\Socialite\User |
||
| 163 | */ |
||
| 164 | abstract protected function mapUserToObject(array $user); |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Redirect the user of the application to the provider's authentication screen. |
||
| 168 | * |
||
| 169 | * @param string $redirectUrl |
||
| 170 | * |
||
| 171 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 172 | */ |
||
| 173 | public function redirect($redirectUrl = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function user(AccessTokenInterface $token = null) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Set redirect url. |
||
| 208 | * |
||
| 209 | * @param string $redirectUrl |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setRedirectUrl($redirectUrl) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set redirect url. |
||
| 222 | * |
||
| 223 | * @param string $redirectUrl |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function withRedirectUrl($redirectUrl) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the redirect url. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getRedirectUrl() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param \Overtrue\Socialite\AccessTokenInterface $accessToken |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function setAccessToken(AccessTokenInterface $accessToken) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the access token for the given code. |
||
| 258 | * |
||
| 259 | * @param string $code |
||
| 260 | * |
||
| 261 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 262 | */ |
||
| 263 | public function getAccessToken($code) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set the scopes of the requested access. |
||
| 281 | * |
||
| 282 | * @param array $scopes |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function scopes(array $scopes) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set the request instance. |
||
| 295 | * |
||
| 296 | * @param Request $request |
||
| 297 | * |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function setRequest(Request $request) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the request instance. |
||
| 309 | * |
||
| 310 | * @return \Symfony\Component\HttpFoundation\Request |
||
| 311 | */ |
||
| 312 | public function getRequest() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Indicates that the provider should operate as stateless. |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function stateless() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set the custom parameters of the request. |
||
| 331 | * |
||
| 332 | * @param array $parameters |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function with(array $parameters) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @throws \ReflectionException |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getName() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getConfig() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get the authentication URL for the provider. |
||
| 367 | * |
||
| 368 | * @param string $url |
||
| 369 | * @param string $state |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | protected function buildAuthUrlFromBase($url, $state) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the GET parameters for the code request. |
||
| 380 | * |
||
| 381 | * @param string|null $state |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | protected function getCodeFields($state = null) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Format the given scopes. |
||
| 403 | * |
||
| 404 | * @param array $scopes |
||
| 405 | * @param string $scopeSeparator |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Determine if the current request / session has a mismatching "state". |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | protected function hasInvalidState() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get the POST fields for the token request. |
||
| 432 | * |
||
| 433 | * @param string $code |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | protected function getTokenFields($code) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the access token from the token response body. |
||
| 449 | * |
||
| 450 | * @param \Psr\Http\Message\StreamInterface|array $body |
||
| 451 | * |
||
| 452 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 453 | */ |
||
| 454 | protected function parseAccessToken($body) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get the code from the request. |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | protected function getCode() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get a fresh instance of the Guzzle HTTP client. |
||
| 479 | * |
||
| 480 | * @return \GuzzleHttp\Client |
||
| 481 | */ |
||
| 482 | protected function getHttpClient() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set options for Guzzle HTTP client. |
||
| 489 | * |
||
| 490 | * @param array $config |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | public static function setGuzzleOptions($config = []) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Determine if the provider is operating with state. |
||
| 501 | * |
||
| 502 | * @return bool |
||
| 503 | */ |
||
| 504 | protected function usesState() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Determine if the provider is operating as stateless. |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | protected function isStateless() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return array item by key. |
||
| 521 | * |
||
| 522 | * @param array $array |
||
| 523 | * @param string $key |
||
| 524 | * @param mixed $default |
||
| 525 | * |
||
| 526 | * @return mixed |
||
| 527 | */ |
||
| 528 | View Code Duplication | protected function arrayItem(array $array, $key, $default = null) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Put state to session storage and return it. |
||
| 551 | * |
||
| 552 | * @return string|bool |
||
| 553 | */ |
||
| 554 | protected function makeState() |
||
| 573 | } |
||
| 574 |