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 | * Indicates if the state should be save to session. |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $autoSaveState = true; |
||
| 110 | /** |
||
| 111 | * The options for guzzle\client. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected static $guzzleOptions = ['http_errors' => false]; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Create a new provider instance. |
||
| 119 | * |
||
| 120 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 121 | * @param string $clientId |
||
| 122 | * @param string $clientSecret |
||
| 123 | * @param string|null $redirectUrl |
||
| 124 | */ |
||
| 125 | public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the authentication URL for the provider. |
||
| 135 | * |
||
| 136 | * @param string $state |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | abstract public function getAuthUrl($state); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the token URL for the provider. |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | abstract protected function getTokenUrl(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the raw user for the given access token. |
||
| 151 | * |
||
| 152 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | abstract protected function getUserByToken(AccessTokenInterface $token); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Map the raw user array to a Socialite User instance. |
||
| 160 | * |
||
| 161 | * @param array $user |
||
| 162 | * |
||
| 163 | * @return \Overtrue\Socialite\User |
||
| 164 | */ |
||
| 165 | abstract protected function mapUserToObject(array $user); |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Redirect the user of the application to the provider's authentication screen. |
||
| 169 | * |
||
| 170 | * @param string $redirectUrl |
||
| 171 | * |
||
| 172 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 173 | */ |
||
| 174 | public function redirect($redirectUrl = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function user(AccessTokenInterface $token = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set redirect url. |
||
| 209 | * |
||
| 210 | * @param string $redirectUrl |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function setRedirectUrl($redirectUrl) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set redirect url. |
||
| 223 | * |
||
| 224 | * @param string $redirectUrl |
||
| 225 | * |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function withRedirectUrl($redirectUrl) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return the redirect url. |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getRedirectUrl() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set autoSaveState . |
||
| 247 | * |
||
| 248 | * @param bool $autoSaveState |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function setAutoSaveState(bool $autoSaveState) |
||
| 257 | /** |
||
| 258 | * @param \Overtrue\Socialite\AccessTokenInterface $accessToken |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function setAccessToken(AccessTokenInterface $accessToken) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the access token for the given code. |
||
| 271 | * |
||
| 272 | * @param string $code |
||
| 273 | * |
||
| 274 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 275 | */ |
||
| 276 | public function getAccessToken($code) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set the scopes of the requested access. |
||
| 294 | * |
||
| 295 | * @param array $scopes |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function scopes(array $scopes) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set the request instance. |
||
| 308 | * |
||
| 309 | * @param Request $request |
||
| 310 | * |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function setRequest(Request $request) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the request instance. |
||
| 322 | * |
||
| 323 | * @return \Symfony\Component\HttpFoundation\Request |
||
| 324 | */ |
||
| 325 | public function getRequest() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Indicates that the provider should operate as stateless. |
||
| 332 | * |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function stateless() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set the custom parameters of the request. |
||
| 344 | * |
||
| 345 | * @param array $parameters |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function with(array $parameters) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @throws \ReflectionException |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getName() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get the authentication URL for the provider. |
||
| 372 | * |
||
| 373 | * @param string $url |
||
| 374 | * @param string $state |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | protected function buildAuthUrlFromBase($url, $state) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the GET parameters for the code request. |
||
| 385 | * |
||
| 386 | * @param string|null $state |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | View Code Duplication | protected function getCodeFields($state = null) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Format the given scopes. |
||
| 408 | * |
||
| 409 | * @param array $scopes |
||
| 410 | * @param string $scopeSeparator |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Determine if the current request / session has a mismatching "state". |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | protected function hasInvalidState() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the POST fields for the token request. |
||
| 436 | * |
||
| 437 | * @param string $code |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function getTokenFields($code) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get the access token from the token response body. |
||
| 453 | * |
||
| 454 | * @param \Psr\Http\Message\StreamInterface|array $body |
||
| 455 | * |
||
| 456 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 457 | */ |
||
| 458 | View Code Duplication | protected function parseAccessToken($body) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get the code from the request. |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | protected function getCode() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get a fresh instance of the Guzzle HTTP client. |
||
| 483 | * |
||
| 484 | * @return \GuzzleHttp\Client |
||
| 485 | */ |
||
| 486 | protected function getHttpClient() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Set options for Guzzle HTTP client. |
||
| 493 | * |
||
| 494 | * @param array $config |
||
| 495 | * |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | public static function setGuzzleOptions($config = []) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Determine if the provider is operating with state. |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | protected function usesState() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Determine if the provider is operating as stateless. |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | protected function isStateless() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Return array item by key. |
||
| 525 | * |
||
| 526 | * @param array $array |
||
| 527 | * @param string $key |
||
| 528 | * @param mixed $default |
||
| 529 | * |
||
| 530 | * @return mixed |
||
| 531 | */ |
||
| 532 | View Code Duplication | protected function arrayItem(array $array, $key, $default = null) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Put state to session storage and return it. |
||
| 555 | * |
||
| 556 | * @return string|bool |
||
| 557 | */ |
||
| 558 | public function makeState() |
||
| 581 | } |
||
| 582 |