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 | /** |
||
| 106 | * The options for guzzle\client |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected static $guzzleOptions = [ 'http_errors' => false ]; |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Create a new provider instance. |
||
| 115 | * |
||
| 116 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 117 | * @param string $clientId |
||
| 118 | * @param string $clientSecret |
||
| 119 | * @param string|null $redirectUrl |
||
| 120 | */ |
||
| 121 | public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the authentication URL for the provider. |
||
| 131 | * |
||
| 132 | * @param string $state |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | abstract protected function getAuthUrl($state); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the token URL for the provider. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | abstract protected function getTokenUrl(); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the raw user for the given access token. |
||
| 147 | * |
||
| 148 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | abstract protected function getUserByToken(AccessTokenInterface $token); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Map the raw user array to a Socialite User instance. |
||
| 156 | * |
||
| 157 | * @param array $user |
||
| 158 | * |
||
| 159 | * @return \Overtrue\Socialite\User |
||
| 160 | */ |
||
| 161 | abstract protected function mapUserToObject(array $user); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Redirect the user of the application to the provider's authentication screen. |
||
| 165 | * |
||
| 166 | * @param string $redirectUrl |
||
| 167 | * |
||
| 168 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 169 | */ |
||
| 170 | public function redirect($redirectUrl = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function user(AccessTokenInterface $token = null) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set redirect url. |
||
| 205 | * |
||
| 206 | * @param string $redirectUrl |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setRedirectUrl($redirectUrl) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set redirect url. |
||
| 219 | * |
||
| 220 | * @param string $redirectUrl |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function withRedirectUrl($redirectUrl) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return the redirect url. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getRedirectUrl() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param \Overtrue\Socialite\AccessTokenInterface $accessToken |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function setAccessToken(AccessTokenInterface $accessToken) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the access token for the given code. |
||
| 255 | * |
||
| 256 | * @param string $code |
||
| 257 | * |
||
| 258 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 259 | */ |
||
| 260 | public function getAccessToken($code) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the scopes of the requested access. |
||
| 278 | * |
||
| 279 | * @param array $scopes |
||
| 280 | * |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function scopes(array $scopes) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Set the request instance. |
||
| 292 | * |
||
| 293 | * @param Request $request |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function setRequest(Request $request) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the request instance. |
||
| 306 | * |
||
| 307 | * @return \Symfony\Component\HttpFoundation\Request |
||
| 308 | */ |
||
| 309 | public function getRequest() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Indicates that the provider should operate as stateless. |
||
| 316 | * |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function stateless() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the custom parameters of the request. |
||
| 328 | * |
||
| 329 | * @param array $parameters |
||
| 330 | * |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function with(array $parameters) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getName() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the authentication URL for the provider. |
||
| 354 | * |
||
| 355 | * @param string $url |
||
| 356 | * @param string $state |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | protected function buildAuthUrlFromBase($url, $state) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get the GET parameters for the code request. |
||
| 367 | * |
||
| 368 | * @param string|null $state |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | protected function getCodeFields($state = null) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Format the given scopes. |
||
| 390 | * |
||
| 391 | * @param array $scopes |
||
| 392 | * @param string $scopeSeparator |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Determine if the current request / session has a mismatching "state". |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | protected function hasInvalidState() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get the POST fields for the token request. |
||
| 419 | * |
||
| 420 | * @param string $code |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function getTokenFields($code) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the access token from the token response body. |
||
| 436 | * |
||
| 437 | * @param \Psr\Http\Message\StreamInterface|array $body |
||
| 438 | * |
||
| 439 | * @return \Overtrue\Socialite\AccessTokenInterface |
||
| 440 | */ |
||
| 441 | protected function parseAccessToken($body) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the code from the request. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | protected function getCode() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get a fresh instance of the Guzzle HTTP client. |
||
| 466 | * |
||
| 467 | * @return \GuzzleHttp\Client |
||
| 468 | */ |
||
| 469 | protected function getHttpClient() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set options for Guzzle HTTP client |
||
| 476 | * |
||
| 477 | * @param array $config |
||
| 478 | */ |
||
| 479 | public static function setGuzzleOptions($config=[]) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Determine if the provider is operating with state. |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | protected function usesState() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Determine if the provider is operating as stateless. |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | protected function isStateless() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Return array item by key. |
||
| 506 | * |
||
| 507 | * @param array $array |
||
| 508 | * @param string $key |
||
| 509 | * @param mixed $default |
||
| 510 | * |
||
| 511 | * @return mixed |
||
| 512 | */ |
||
| 513 | View Code Duplication | protected function arrayItem(array $array, $key, $default = null) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Put state to session storage and return it. |
||
| 536 | * |
||
| 537 | * @return string|bool |
||
| 538 | */ |
||
| 539 | protected function makeState() |
||
| 554 | } |
||
| 555 |