Complex classes like FacebookRequest 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 FacebookRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class FacebookRequest |
||
40 | { |
||
41 | /** |
||
42 | * @var FacebookApp The Facebook app entity. |
||
43 | */ |
||
44 | protected $app; |
||
45 | |||
46 | /** |
||
47 | * @var string|null The access token to use for this request. |
||
48 | */ |
||
49 | protected $accessToken; |
||
50 | |||
51 | /** |
||
52 | * @var string The HTTP method for this request. |
||
53 | */ |
||
54 | protected $method; |
||
55 | |||
56 | /** |
||
57 | * @var string The Graph endpoint for this request. |
||
58 | */ |
||
59 | protected $endpoint; |
||
60 | |||
61 | /** |
||
62 | * @var array The headers to send with this request. |
||
63 | */ |
||
64 | protected $headers = []; |
||
65 | |||
66 | /** |
||
67 | * @var array The parameters to send with this request. |
||
68 | */ |
||
69 | protected $params = []; |
||
70 | |||
71 | /** |
||
72 | * @var array The files to send with this request. |
||
73 | */ |
||
74 | protected $files = []; |
||
75 | |||
76 | /** |
||
77 | * @var string ETag to send with this request. |
||
78 | */ |
||
79 | protected $eTag; |
||
80 | |||
81 | /** |
||
82 | * @var string Graph version to use for this request. |
||
83 | */ |
||
84 | protected $graphVersion; |
||
85 | |||
86 | /** |
||
87 | * Creates a new Request entity. |
||
88 | * |
||
89 | * @param FacebookApp|null $app |
||
90 | * @param AccessToken|string|null $accessToken |
||
91 | * @param string|null $method |
||
92 | * @param string|null $endpoint |
||
93 | * @param array|null $params |
||
94 | * @param string|null $eTag |
||
95 | * @param string|null $graphVersion |
||
96 | */ |
||
97 | public function __construct(FacebookApp $app = null, $accessToken = null, $method = null, $endpoint = null, array $params = [], $eTag = null, $graphVersion = null) |
||
98 | { |
||
99 | $this->setApp($app); |
||
100 | $this->setAccessToken($accessToken); |
||
101 | $this->setMethod($method); |
||
102 | $this->setEndpoint($endpoint); |
||
103 | $this->setParams($params); |
||
104 | $this->setETag($eTag); |
||
105 | $this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set the access token for this request. |
||
110 | * |
||
111 | * @param AccessToken|string |
||
112 | * |
||
113 | * @return FacebookRequest |
||
114 | */ |
||
115 | public function setAccessToken($accessToken) |
||
116 | { |
||
117 | $this->accessToken = $accessToken; |
||
118 | if ($accessToken instanceof AccessToken) { |
||
119 | $this->accessToken = $accessToken->getValue(); |
||
120 | } |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Sets the access token with one harvested from a URL or POST params. |
||
127 | * |
||
128 | * @param string $accessToken The access token. |
||
129 | * |
||
130 | * @return FacebookRequest |
||
131 | * |
||
132 | * @throws FacebookSDKException |
||
133 | */ |
||
134 | public function setAccessTokenFromParams($accessToken) |
||
135 | { |
||
136 | $existingAccessToken = $this->getAccessToken(); |
||
137 | if (!$existingAccessToken) { |
||
|
|||
138 | $this->setAccessToken($accessToken); |
||
139 | } elseif ($accessToken !== $existingAccessToken) { |
||
140 | throw new FacebookSDKException('Access token mismatch. The access token provided in the FacebookRequest and the one provided in the URL or POST params do not match.'); |
||
141 | } |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Return the access token for this request. |
||
148 | * |
||
149 | * @return string|null |
||
150 | */ |
||
151 | public function getAccessToken() |
||
152 | { |
||
153 | return $this->accessToken; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Return the access token for this request as an AccessToken entity. |
||
158 | * |
||
159 | * @return AccessToken|null |
||
160 | */ |
||
161 | public function getAccessTokenEntity() |
||
162 | { |
||
163 | return $this->accessToken ? new AccessToken($this->accessToken) : null; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Set the FacebookApp entity used for this request. |
||
168 | * |
||
169 | * @param FacebookApp|null $app |
||
170 | */ |
||
171 | public function setApp(FacebookApp $app = null) |
||
172 | { |
||
173 | $this->app = $app; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Return the FacebookApp entity used for this request. |
||
178 | * |
||
179 | * @return FacebookApp |
||
180 | */ |
||
181 | public function getApp() |
||
182 | { |
||
183 | return $this->app; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Generate an app secret proof to sign this request. |
||
188 | * |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function getAppSecretProof() |
||
192 | { |
||
193 | if (!$accessTokenEntity = $this->getAccessTokenEntity()) { |
||
194 | return null; |
||
195 | } |
||
196 | |||
197 | return $accessTokenEntity->getAppSecretProof($this->app->getSecret()); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Validate that an access token exists for this request. |
||
202 | * |
||
203 | * @throws FacebookSDKException |
||
204 | */ |
||
205 | public function validateAccessToken() |
||
206 | { |
||
207 | $accessToken = $this->getAccessToken(); |
||
208 | if (!$accessToken) { |
||
209 | throw new FacebookSDKException('You must provide an access token.'); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Set the HTTP method for this request. |
||
215 | * |
||
216 | * @param string |
||
217 | */ |
||
218 | public function setMethod($method) |
||
222 | |||
223 | /** |
||
224 | * Return the HTTP method for this request. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getMethod() |
||
229 | { |
||
230 | return $this->method; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Validate that the HTTP method is set. |
||
235 | * |
||
236 | * @throws FacebookSDKException |
||
237 | */ |
||
238 | public function validateMethod() |
||
248 | |||
249 | /** |
||
250 | * Set the endpoint for this request. |
||
251 | * |
||
252 | * @param string |
||
253 | * |
||
254 | * @return FacebookRequest |
||
255 | * |
||
256 | * @throws FacebookSDKException |
||
257 | */ |
||
258 | public function setEndpoint($endpoint) |
||
272 | |||
273 | /** |
||
274 | * Return the endpoint for this request. |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getEndpoint() |
||
283 | |||
284 | /** |
||
285 | * Generate and return the headers for this request. |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | public function getHeaders() |
||
299 | |||
300 | /** |
||
301 | * Set the headers for this request. |
||
302 | * |
||
303 | * @param array $headers |
||
304 | */ |
||
305 | public function setHeaders(array $headers) |
||
309 | |||
310 | /** |
||
311 | * Sets the eTag value. |
||
312 | * |
||
313 | * @param string $eTag |
||
314 | */ |
||
315 | public function setETag($eTag) |
||
319 | |||
320 | /** |
||
321 | * Set the params for this request. |
||
322 | * |
||
323 | * @param array $params |
||
324 | * |
||
325 | * @return FacebookRequest |
||
326 | * |
||
327 | * @throws FacebookSDKException |
||
328 | */ |
||
329 | public function setParams(array $params = []) |
||
345 | |||
346 | /** |
||
347 | * Set the params for this request without filtering them first. |
||
348 | * |
||
349 | * @param array $params |
||
350 | * |
||
351 | * @return FacebookRequest |
||
352 | */ |
||
353 | public function dangerouslySetParams(array $params = []) |
||
359 | |||
360 | /** |
||
361 | * Iterate over the params and pull out the file uploads. |
||
362 | * |
||
363 | * @param array $params |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | public function sanitizeFileParams(array $params) |
||
378 | |||
379 | /** |
||
380 | * Add a file to be uploaded. |
||
381 | * |
||
382 | * @param string $key |
||
383 | * @param FacebookFile $file |
||
384 | */ |
||
385 | public function addFile($key, FacebookFile $file) |
||
389 | |||
390 | /** |
||
391 | * Removes all the files from the upload queue. |
||
392 | */ |
||
393 | public function resetFiles() |
||
397 | |||
398 | /** |
||
399 | * Get the list of files to be uploaded. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function getFiles() |
||
407 | |||
408 | /** |
||
409 | * Let's us know if there is a file upload with this request. |
||
410 | * |
||
411 | * @return boolean |
||
412 | */ |
||
413 | public function containsFileUploads() |
||
417 | |||
418 | /** |
||
419 | * Let's us know if there is a video upload with this request. |
||
420 | * |
||
421 | * @return boolean |
||
422 | */ |
||
423 | public function containsVideoUploads() |
||
433 | |||
434 | /** |
||
435 | * Returns the body of the request as multipart/form-data. |
||
436 | * |
||
437 | * @return RequestBodyMultipart |
||
438 | */ |
||
439 | public function getMultipartBody() |
||
445 | |||
446 | /** |
||
447 | * Returns the body of the request as URL-encoded. |
||
448 | * |
||
449 | * @return RequestBodyUrlEncoded |
||
450 | */ |
||
451 | public function getUrlEncodedBody() |
||
457 | |||
458 | /** |
||
459 | * Generate and return the params for this request. |
||
460 | * |
||
461 | * @return array |
||
462 | */ |
||
463 | public function getParams() |
||
475 | |||
476 | /** |
||
477 | * Only return params on POST requests. |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | public function getPostParams() |
||
489 | |||
490 | /** |
||
491 | * The graph version used for this request. |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | public function getGraphVersion() |
||
499 | |||
500 | /** |
||
501 | * Generate and return the URL for this request. |
||
502 | * |
||
503 | * @return string |
||
504 | */ |
||
505 | public function getUrl() |
||
521 | |||
522 | /** |
||
523 | * Return the default headers that every request should use. |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | public static function getDefaultHeaders() |
||
534 | } |
||
535 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: