Complex classes like FacebookResponseException 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 FacebookResponseException, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class FacebookResponseException extends FacebookSDKException |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var FacebookResponse The response that threw the exception. |
||
| 37 | */ |
||
| 38 | protected $response; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array Decoded response. |
||
| 42 | */ |
||
| 43 | protected $responseData; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Creates a FacebookResponseException. |
||
| 47 | * |
||
| 48 | * @param FacebookResponse $response The response that threw the exception. |
||
| 49 | * @param FacebookSDKException $previousException The more detailed exception. |
||
| 50 | */ |
||
| 51 | public function __construct(FacebookResponse $response, FacebookSDKException $previousException = null) |
||
| 52 | { |
||
| 53 | $this->response = $response; |
||
| 54 | $this->responseData = $response->getDecodedBody(); |
||
| 55 | |||
| 56 | $errorMessage = $this->get('message', 'Unknown error from Graph.'); |
||
| 57 | $errorCode = $this->get('code', -1); |
||
| 58 | |||
| 59 | parent::__construct($errorMessage, $errorCode, $previousException); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * A factory for creating the appropriate exception based on the response from Graph. |
||
| 64 | * |
||
| 65 | * @param FacebookResponse $response The response that threw the exception. |
||
| 66 | * |
||
| 67 | * @return FacebookResponseException |
||
| 68 | */ |
||
| 69 | public static function create(FacebookResponse $response) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Checks isset and returns that or a default value. |
||
| 140 | * |
||
| 141 | * @param string $key |
||
| 142 | * @param mixed $default |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | private function get($key, $default = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the HTTP status code |
||
| 157 | * |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function getHttpStatusCode() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the sub-error code |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function getSubErrorCode() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the error type |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getErrorType() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the raw response used to create the exception. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getRawResponse() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the decoded response used to create the exception. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getResponseData() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns the response entity used to create the exception. |
||
| 207 | * |
||
| 208 | * @return FacebookResponse |
||
| 209 | */ |
||
| 210 | public function getResponse() |
||
| 214 | } |
||
| 215 |