Complex classes like Embedly 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 Embedly, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Embedly { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * |
||
| 14 | * @const |
||
| 15 | */ |
||
| 16 | const VERSION = '0.1.0'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $hostname = 'api.embed.ly'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $key = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $api_version = array( |
||
| 35 | 'oembed' => 1, |
||
| 36 | 'objectify' => 2, |
||
| 37 | 'preview' => 1 |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $user_agent = ""; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * |
||
| 48 | * @var array|object |
||
| 49 | */ |
||
| 50 | protected $services = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @param array $args |
||
| 55 | */ |
||
| 56 | public function __construct(array $args = array()) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * Flexibly parse host strings. |
||
| 82 | * |
||
| 83 | * Returns an array of |
||
| 84 | * { protocol: |
||
| 85 | * , host: |
||
| 86 | * , port: |
||
| 87 | * , url: |
||
| 88 | * } |
||
| 89 | * |
||
| 90 | * @param string $host |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function parse_host($host) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @return string|array |
||
| 134 | */ |
||
| 135 | public function oembed($params) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * |
||
| 142 | * @param string|array $params |
||
| 143 | * @return object |
||
| 144 | */ |
||
| 145 | public function preview($params) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * |
||
| 152 | * @param array $params |
||
| 153 | * @return object |
||
| 154 | */ |
||
| 155 | public function objectify($params) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function api_version() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * |
||
| 171 | * @param string $version |
||
| 172 | * @param array $action |
||
| 173 | * @param array $params |
||
| 174 | * @return object |
||
| 175 | */ |
||
| 176 | public function apicall($version, $action, $params) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function services() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function services_regex() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | protected function q($params) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * |
||
| 293 | * @param resource $ch |
||
| 294 | * @param array $headers |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | protected function setCurlOptions(&$ch, $headers = array()) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * |
||
| 309 | * @param resource $ch |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | protected function curlExec(&$ch) |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * |
||
| 324 | * @param string $r |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public static function reg_delim_stripper($r) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * |
||
| 336 | * @param stdClass $o |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public static function reg_imploder(\stdClass $o) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * |
||
| 346 | * @param string $key |
||
| 347 | * @param string|array $value |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public static function url_encode($key, $value) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * |
||
| 363 | * @param string $input |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public static function paramify($input) |
||
| 374 | } |
||
| 375 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.