Complex classes like Freemius_Api_WordPress 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 Freemius_Api_WordPress, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
85 | class Freemius_Api_WordPress extends Freemius_Api_Base { |
||
86 | private static $_logger = array(); |
||
87 | |||
88 | /** |
||
89 | * @param string $pScope 'app', 'developer', 'user' or 'install'. |
||
90 | * @param number $pID Element's id. |
||
91 | * @param string $pPublic Public key. |
||
92 | * @param string|bool $pSecret Element's secret key. |
||
93 | * @param bool $pSandbox Whether or not to run API in sandbox mode. |
||
94 | */ |
||
95 | public function __construct( $pScope, $pID, $pPublic, $pSecret = false, $pSandbox = false ) { |
||
103 | |||
104 | public static function GetUrl( $pCanonizedPath = '', $pIsSandbox = false ) { |
||
113 | |||
114 | #---------------------------------------------------------------------------------- |
||
115 | #region Servers Clock Diff |
||
116 | #---------------------------------------------------------------------------------- |
||
117 | |||
118 | /** |
||
119 | * @var int Clock diff in seconds between current server to API server. |
||
120 | */ |
||
121 | private static $_clock_diff = 0; |
||
122 | |||
123 | /** |
||
124 | * Set clock diff for all API calls. |
||
125 | * |
||
126 | * @since 1.0.3 |
||
127 | * |
||
128 | * @param $pSeconds |
||
129 | */ |
||
130 | public static function SetClockDiff( $pSeconds ) { |
||
133 | |||
134 | /** |
||
135 | * Find clock diff between current server to API server. |
||
136 | * |
||
137 | * @since 1.0.2 |
||
138 | * @return int Clock diff in seconds. |
||
139 | */ |
||
140 | public static function FindClockDiff() { |
||
146 | |||
147 | #endregion |
||
148 | |||
149 | /** |
||
150 | * @var string http or https |
||
151 | */ |
||
152 | private static $_protocol = FS_API__PROTOCOL; |
||
153 | |||
154 | /** |
||
155 | * Set API connection protocol. |
||
156 | * |
||
157 | * @since 1.0.4 |
||
158 | */ |
||
159 | public static function SetHttp() { |
||
162 | |||
163 | /** |
||
164 | * @since 1.0.4 |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public static function IsHttps() { |
||
171 | |||
172 | /** |
||
173 | * Sign request with the following HTTP headers: |
||
174 | * Content-MD5: MD5(HTTP Request body) |
||
175 | * Date: Current date (i.e Sat, 14 Feb 2016 20:24:46 +0000) |
||
176 | * Authorization: FS {scope_entity_id}:{scope_entity_public_key}:base64encode(sha256(string_to_sign, |
||
177 | * {scope_entity_secret_key})) |
||
178 | * |
||
179 | * @param string $pResourceUrl |
||
180 | * @param array $pWPRemoteArgs |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | function SignRequest( $pResourceUrl, $pWPRemoteArgs ) { |
||
200 | |||
201 | /** |
||
202 | * Generate Authorization request headers: |
||
203 | * |
||
204 | * Content-MD5: MD5(HTTP Request body) |
||
205 | * Date: Current date (i.e Sat, 14 Feb 2016 20:24:46 +0000) |
||
206 | * Authorization: FS {scope_entity_id}:{scope_entity_public_key}:base64encode(sha256(string_to_sign, |
||
207 | * {scope_entity_secret_key})) |
||
208 | * |
||
209 | * @author Vova Feldman |
||
210 | * |
||
211 | * @param string $pResourceUrl |
||
212 | * @param string $pMethod |
||
213 | * @param string $pPostParams |
||
214 | * |
||
215 | * @return array |
||
216 | * @throws Freemius_Exception |
||
217 | */ |
||
218 | function GenerateAuthorizationParams( |
||
263 | |||
264 | /** |
||
265 | * Get API request URL signed via query string. |
||
266 | * |
||
267 | * @param string $pPath |
||
268 | * |
||
269 | * @throws Freemius_Exception |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | function GetSignedUrl( $pPath ) { |
||
287 | |||
288 | /** |
||
289 | * @author Vova Feldman |
||
290 | * |
||
291 | * @param string $pUrl |
||
292 | * @param array $pWPRemoteArgs |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | private static function ExecuteRequest( $pUrl, &$pWPRemoteArgs ) { |
||
325 | |||
326 | /** |
||
327 | * @return array |
||
328 | */ |
||
329 | static function GetLogger() { |
||
332 | |||
333 | /** |
||
334 | * @param string $pCanonizedPath |
||
335 | * @param string $pMethod |
||
336 | * @param array $pParams |
||
337 | * @param null|array $pWPRemoteArgs |
||
338 | * @param bool $pIsSandbox |
||
339 | * @param null|callable $pBeforeExecutionFunction |
||
340 | * |
||
341 | * @return object[]|object|null |
||
342 | * |
||
343 | * @throws \Freemius_Exception |
||
344 | */ |
||
345 | private static function MakeStaticRequest( |
||
470 | |||
471 | |||
472 | /** |
||
473 | * Makes an HTTP request. This method can be overridden by subclasses if |
||
474 | * developers want to do fancier things or use something other than wp_remote_request() |
||
475 | * to make the request. |
||
476 | * |
||
477 | * @param string $pCanonizedPath The URL to make the request to |
||
478 | * @param string $pMethod HTTP method |
||
479 | * @param array $pParams The parameters to use for the POST body |
||
480 | * @param null|array $pWPRemoteArgs wp_remote_request options. |
||
481 | * |
||
482 | * @return object[]|object|null |
||
483 | * |
||
484 | * @throws Freemius_Exception |
||
485 | */ |
||
486 | public function MakeRequest( |
||
506 | |||
507 | /** |
||
508 | * Sets CURLOPT_IPRESOLVE to CURL_IPRESOLVE_V4 for cURL-Handle provided as parameter |
||
509 | * |
||
510 | * @param resource $handle A cURL handle returned by curl_init() |
||
511 | * |
||
512 | * @return resource $handle A cURL handle returned by curl_init() with CURLOPT_IPRESOLVE set to |
||
513 | * CURL_IPRESOLVE_V4 |
||
514 | * |
||
515 | * @link https://gist.github.com/golderweb/3a2aaec2d56125cc004e |
||
516 | */ |
||
517 | static function CurlResolveToIPv4( $handle ) { |
||
522 | |||
523 | #---------------------------------------------------------------------------------- |
||
524 | #region Connectivity Test |
||
525 | #---------------------------------------------------------------------------------- |
||
526 | |||
527 | /** |
||
528 | * If successful connectivity to the API endpoint using ping.json endpoint. |
||
529 | * |
||
530 | * - OR - |
||
531 | * |
||
532 | * Validate if ping result object is valid. |
||
533 | * |
||
534 | * @param mixed $pPong |
||
535 | * |
||
536 | * @return bool |
||
537 | */ |
||
538 | public static function Test( $pPong = null ) { |
||
549 | |||
550 | /** |
||
551 | * Ping API to test connectivity. |
||
552 | * |
||
553 | * @return object |
||
554 | */ |
||
555 | public static function Ping() { |
||
575 | |||
576 | #endregion |
||
577 | |||
578 | #---------------------------------------------------------------------------------- |
||
579 | #region Connectivity Exceptions |
||
580 | #---------------------------------------------------------------------------------- |
||
581 | |||
582 | /** |
||
583 | * @param \WP_Error $pError |
||
584 | * |
||
585 | * @return bool |
||
586 | */ |
||
587 | private static function IsCurlError( WP_Error $pError ) { |
||
592 | |||
593 | /** |
||
594 | * @param WP_Error $pError |
||
595 | * |
||
596 | * @throws Freemius_Exception |
||
597 | */ |
||
598 | private static function ThrowWPRemoteException( WP_Error $pError ) { |
||
661 | |||
662 | /** |
||
663 | * @param string $pResult |
||
664 | * |
||
665 | * @throws Freemius_Exception |
||
666 | */ |
||
667 | private static function ThrowCloudFlareDDoSException( $pResult = '' ) { |
||
677 | |||
678 | /** |
||
679 | * @param string $pResult |
||
680 | * |
||
681 | * @throws Freemius_Exception |
||
682 | */ |
||
683 | private static function ThrowSquidAclException( $pResult = '' ) { |
||
693 | |||
694 | #endregion |
||
695 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.