Complex classes like FS_Api 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 FS_Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class FS_Api { |
||
| 23 | /** |
||
| 24 | * @var FS_Api[] |
||
| 25 | */ |
||
| 26 | private static $_instances = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var FS_Option_Manager Freemius options, options-manager. |
||
| 30 | */ |
||
| 31 | private static $_options; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var FS_Option_Manager API Caching layer |
||
| 35 | */ |
||
| 36 | private static $_cache; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int Clock diff in seconds between current server to API server. |
||
| 40 | */ |
||
| 41 | private static $_clock_diff; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Freemius_Api |
||
| 45 | */ |
||
| 46 | private $_api; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $_slug; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var FS_Logger |
||
| 55 | * @since 1.0.4 |
||
| 56 | */ |
||
| 57 | private $_logger; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $slug |
||
| 61 | * @param string $scope 'app', 'developer', 'user' or 'install'. |
||
| 62 | * @param number $id Element's id. |
||
| 63 | * @param string $public_key Public key. |
||
| 64 | * @param bool $is_sandbox |
||
| 65 | * @param bool|string $secret_key Element's secret key. |
||
| 66 | * |
||
| 67 | * @return FS_Api |
||
| 68 | */ |
||
| 69 | static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) { |
||
| 82 | |||
| 83 | private static function _init() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $slug |
||
| 101 | * @param string $scope 'app', 'developer', 'user' or 'install'. |
||
| 102 | * @param number $id Element's id. |
||
| 103 | * @param string $public_key Public key. |
||
| 104 | * @param bool|string $secret_key Element's secret key. |
||
| 105 | * @param bool $is_sandbox |
||
| 106 | */ |
||
| 107 | private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Find clock diff between server and API server, and store the diff locally. |
||
| 116 | * |
||
| 117 | * @param bool|int $diff |
||
| 118 | * |
||
| 119 | * @return bool|int False if clock diff didn't change, otherwise returns the clock diff in seconds. |
||
| 120 | */ |
||
| 121 | private function _sync_clock_diff( $diff = false ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Override API call to enable retry with servers' clock auto sync method. |
||
| 146 | * |
||
| 147 | * @param string $path |
||
| 148 | * @param string $method |
||
| 149 | * @param array $params |
||
| 150 | * @param bool $retry Is in retry or first call attempt. |
||
| 151 | * |
||
| 152 | * @return array|mixed|string|void |
||
| 153 | */ |
||
| 154 | private function _call( $path, $method = 'GET', $params = array(), $retry = false ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Override API call to wrap it in servers' clock sync method. |
||
| 186 | * |
||
| 187 | * @param string $path |
||
| 188 | * @param string $method |
||
| 189 | * @param array $params |
||
| 190 | * |
||
| 191 | * @return array|mixed|string|void |
||
| 192 | * @throws Freemius_Exception |
||
| 193 | */ |
||
| 194 | function call( $path, $method = 'GET', $params = array() ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get API request URL signed via query string. |
||
| 200 | * |
||
| 201 | * @param string $path |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | function get_signed_url( $path ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $path |
||
| 211 | * @param bool $flush |
||
| 212 | * @param int $expiration (optional) Time until expiration in seconds from now, defaults to 24 hours |
||
| 213 | * |
||
| 214 | * @return stdClass|mixed |
||
| 215 | */ |
||
| 216 | function get( $path = '/', $flush = false, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) { |
||
| 263 | |||
| 264 | private function get_cache_key( $path, $method = 'GET', $params = array() ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Test API connectivity. |
||
| 273 | * |
||
| 274 | * @since 1.0.9 If fails, try to fallback to HTTP. |
||
| 275 | * |
||
| 276 | * @param null|string $unique_anonymous_id |
||
| 277 | * |
||
| 278 | * @return bool True if successful connectivity to the API. |
||
| 279 | */ |
||
| 280 | function test( $unique_anonymous_id = null ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Ping API for connectivity test, and return result object. |
||
| 308 | * |
||
| 309 | * @author Vova Feldman (@svovaf) |
||
| 310 | * @since 1.0.9 |
||
| 311 | * |
||
| 312 | * @param null|string $unique_anonymous_id |
||
| 313 | * |
||
| 314 | * @return object |
||
| 315 | */ |
||
| 316 | function ping( $unique_anonymous_id = null ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check if valid ping request result. |
||
| 324 | * |
||
| 325 | * @author Vova Feldman (@svovaf) |
||
| 326 | * @since 1.1.1 |
||
| 327 | * |
||
| 328 | * @param mixed $pong |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | function is_valid_ping( $pong ) { |
||
| 335 | |||
| 336 | function get_url( $path = '' ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Clear API cache. |
||
| 342 | * |
||
| 343 | * @author Vova Feldman (@svovaf) |
||
| 344 | * @since 1.0.9 |
||
| 345 | */ |
||
| 346 | static function clear_cache() { |
||
| 350 | } |
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.