Complex classes like EAuthServiceBase 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 EAuthServiceBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class EAuthServiceBase extends CComponent implements IAuthService { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string the service name. |
||
| 21 | */ |
||
| 22 | protected $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * @var string the service title to display in views. |
||
| 27 | */ |
||
| 28 | protected $title; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string the service type (e.g. OpenID, OAuth). |
||
| 32 | */ |
||
| 33 | protected $type; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array arguments for the jQuery.eauth() javascript function. |
||
| 37 | */ |
||
| 38 | protected $jsArguments = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array authorization attributes. |
||
| 42 | * @see getAttribute |
||
| 43 | * @see getItem |
||
| 44 | */ |
||
| 45 | protected $attributes = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var boolean whether user was successfuly authenticated. |
||
| 49 | * @see getIsAuthenticated |
||
| 50 | */ |
||
| 51 | protected $authenticated = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var boolean whether is attributes was fetched. |
||
| 55 | */ |
||
| 56 | private $fetched = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var EAuth the {@link EAuth} application component. |
||
| 60 | */ |
||
| 61 | private $component; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string the redirect url after successful authorization. |
||
| 65 | */ |
||
| 66 | private $redirectUrl = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string the redirect url after unsuccessful authorization (e.g. user canceled). |
||
| 70 | */ |
||
| 71 | private $cancelUrl = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * PHP getter magic method. |
||
| 75 | * This method is overridden so that service attributes can be accessed like properties. |
||
| 76 | * |
||
| 77 | * @param string $name property name. |
||
| 78 | * @return mixed property value. |
||
| 79 | * @see getAttribute |
||
| 80 | */ |
||
| 81 | public function __get($name) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Checks if a attribute value is null. |
||
| 92 | * This method overrides the parent implementation by checking |
||
| 93 | * if the attribute is null or not. |
||
| 94 | * |
||
| 95 | * @param string $name the attribute name. |
||
| 96 | * @return boolean whether the attribute value is null. |
||
| 97 | */ |
||
| 98 | public function __isset($name) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initialize the component. |
||
| 109 | * Sets the default {@link redirectUrl} and {@link cancelUrl}. |
||
| 110 | * |
||
| 111 | * @param EAuth $component the component instance. |
||
| 112 | * @param array $options properties initialization. |
||
| 113 | */ |
||
| 114 | public function init($component, $options = array()) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns service name(id). |
||
| 131 | * |
||
| 132 | * @return string the service name(id). |
||
| 133 | */ |
||
| 134 | public function getServiceName() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns service title. |
||
| 140 | * |
||
| 141 | * @return string the service title. |
||
| 142 | */ |
||
| 143 | public function getServiceTitle() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns service type (e.g. OpenID, OAuth). |
||
| 149 | * |
||
| 150 | * @return string the service type (e.g. OpenID, OAuth). |
||
| 151 | */ |
||
| 152 | public function getServiceType() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns arguments for the jQuery.eauth() javascript function. |
||
| 158 | * |
||
| 159 | * @return array the arguments for the jQuery.eauth() javascript function. |
||
| 160 | */ |
||
| 161 | public function getJsArguments() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Sets {@link EAuth} application component |
||
| 167 | * |
||
| 168 | * @param EAuth $component the application auth component. |
||
| 169 | */ |
||
| 170 | public function setComponent($component) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the {@link EAuth} application component. |
||
| 176 | * |
||
| 177 | * @return EAuth the {@link EAuth} application component. |
||
| 178 | */ |
||
| 179 | public function getComponent() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets redirect url after successful authorization. |
||
| 185 | * |
||
| 186 | * @param string url to redirect. |
||
| 187 | */ |
||
| 188 | public function setRedirectUrl($url) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the redirect url after successful authorization. |
||
| 194 | * |
||
| 195 | * @return string the redirect url after successful authorization. |
||
| 196 | */ |
||
| 197 | public function getRedirectUrl() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets redirect url after unsuccessful authorization (e.g. user canceled). |
||
| 203 | * |
||
| 204 | * @param string url to redirect. |
||
| 205 | */ |
||
| 206 | public function setCancelUrl($url) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the redirect url after unsuccessful authorization (e.g. user canceled). |
||
| 212 | * |
||
| 213 | * @return string the redirect url after unsuccessful authorization (e.g. user canceled). |
||
| 214 | */ |
||
| 215 | public function getCancelUrl() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Authenticate the user. |
||
| 221 | * |
||
| 222 | * @return boolean whether user was successfuly authenticated. |
||
| 223 | */ |
||
| 224 | public function authenticate() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Whether user was successfuly authenticated. |
||
| 230 | * |
||
| 231 | * @return boolean whether user was successfuly authenticated. |
||
| 232 | */ |
||
| 233 | public function getIsAuthenticated() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Redirect to the url. If url is null, {@link redirectUrl} will be used. |
||
| 239 | * |
||
| 240 | * @param string $url url to redirect. |
||
| 241 | * @param array $params |
||
| 242 | */ |
||
| 243 | public function redirect($url = null, $params = array()) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Redirect to the {@link cancelUrl} or simply close the popup window. |
||
| 249 | */ |
||
| 250 | public function cancel($url = null) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Makes the curl request to the url. |
||
| 256 | * |
||
| 257 | * @param string $url url to request. |
||
| 258 | * @param array $options HTTP request options. Keys: query, data, referer. |
||
| 259 | * @param boolean $parseJson Whether to parse response in json format. |
||
| 260 | * @return stdClass the response. |
||
| 261 | */ |
||
| 262 | protected function makeRequest($url, $options = array(), $parseJson = true) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Initializes a new session and return a cURL handle. |
||
| 294 | * |
||
| 295 | * @param string $url url to request. |
||
| 296 | * @param array $options HTTP request options. Keys: query, data, referer. |
||
| 297 | * @param boolean $parseJson Whether to parse response in json format. |
||
|
|
|||
| 298 | * @return cURL handle. |
||
| 299 | */ |
||
| 300 | protected function initRequest($url, $options = array()) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Parse response from {@link makeRequest} in json format and check OAuth errors. |
||
| 345 | * |
||
| 346 | * @param string $response Json string. |
||
| 347 | * @return object result. |
||
| 348 | */ |
||
| 349 | protected function parseJson($response) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the error info from json. |
||
| 371 | * |
||
| 372 | * @param stdClass $json the json response. |
||
| 373 | * @return array the error array with 2 keys: code and message. Should be null if no errors. |
||
| 374 | */ |
||
| 375 | protected function fetchJsonError($json) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return string a prefix for the name of the session variables storing eauth session data. |
||
| 389 | */ |
||
| 390 | protected function getStateKeyPrefix() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Stores a variable in eauth session. |
||
| 396 | * |
||
| 397 | * @param string $key variable name. |
||
| 398 | * @param mixed $value variable value. |
||
| 399 | * @param mixed $defaultValue default value. If $value===$defaultValue, the variable will be |
||
| 400 | * removed from the session. |
||
| 401 | * @see getState |
||
| 402 | */ |
||
| 403 | protected function setState($key, $value, $defaultValue = null) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Returns a value indicating whether there is a state of the specified name. |
||
| 416 | * |
||
| 417 | * @param string $key state name. |
||
| 418 | * @return boolean whether there is a state of the specified name. |
||
| 419 | */ |
||
| 420 | protected function hasState($key) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the value of a variable that is stored in eauth session. |
||
| 428 | * |
||
| 429 | * @param string $key variable name. |
||
| 430 | * @param mixed $defaultValue default value. |
||
| 431 | * @return mixed the value of the variable. If it doesn't exist in the session, |
||
| 432 | * the provided default value will be returned. |
||
| 433 | * @see setState |
||
| 434 | */ |
||
| 435 | protected function getState($key, $defaultValue = null) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Fetch attributes array. |
||
| 443 | * |
||
| 444 | * @return boolean whether the attributes was successfully fetched. |
||
| 445 | */ |
||
| 446 | protected function fetchAttributes() { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Fetch attributes array. |
||
| 452 | * This function is internally used to handle fetched state. |
||
| 453 | */ |
||
| 454 | protected function _fetchAttributes() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the user unique id. |
||
| 466 | * |
||
| 467 | * @return mixed the user id. |
||
| 468 | */ |
||
| 469 | public function getId() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Returns the array that contains all available authorization attributes. |
||
| 477 | * |
||
| 478 | * @return array the attributes. |
||
| 479 | */ |
||
| 480 | public function getAttributes() { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the authorization attribute value. |
||
| 491 | * |
||
| 492 | * @param string $key the attribute name. |
||
| 493 | * @param mixed $default the default value. |
||
| 494 | * @return mixed the attribute value. |
||
| 495 | */ |
||
| 496 | public function getAttribute($key, $default = null) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Whether the authorization attribute exists. |
||
| 509 | * |
||
| 510 | * @param string $key the attribute name. |
||
| 511 | * @return boolean true if attribute exists, false otherwise. |
||
| 512 | */ |
||
| 513 | public function hasAttribute($key) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns the object with a human-readable representation of the current authorization. |
||
| 520 | * |
||
| 521 | * @return stdClass the object. |
||
| 522 | */ |
||
| 523 | public function getItem() { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns the array that contains all available authorization attributes. |
||
| 537 | * |
||
| 538 | * @return array the attributes. |
||
| 539 | * @deprecated because getAttributes is more semantic. |
||
| 540 | */ |
||
| 541 | public function getItemAttributes() { |
||
| 544 | } |
||
| 545 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.