Complex classes like HttpGetRequest 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 HttpGetRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class HttpGetRequest |
||
| 18 | { |
||
| 19 | public $origin; |
||
| 20 | public $scheme = 'http'; |
||
| 21 | public $host = 'example.com'; |
||
| 22 | public $port = 80; |
||
| 23 | public $path = '/'; |
||
| 24 | |||
| 25 | public $query = array(); |
||
| 26 | public $headers = array(); |
||
| 27 | |||
| 28 | public $curlOpts = array(); |
||
| 29 | |||
| 30 | public $username = null; |
||
| 31 | public $password = null; |
||
| 32 | |||
| 33 | public $maybePublic = false; |
||
| 34 | public $verbose = false; |
||
| 35 | |||
| 36 | /** @var CConfig */ |
||
| 37 | protected $config; |
||
| 38 | |||
| 39 | /** @internal */ |
||
| 40 | const TOKEN_LABEL = 'access_token'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * normalize url and authentication info |
||
| 44 | * @param string $origin domain text |
||
| 45 | * @param string $url |
||
| 46 | * @param IO\IOInterface $io |
||
| 47 | */ |
||
| 48 | 35 | public function __construct($origin, $url, IO\IOInterface $io) |
|
| 49 | { |
||
| 50 | 35 | $this->origin = $origin; |
|
| 51 | 35 | $this->importURL($url); |
|
| 52 | |||
| 53 | 35 | if ($this->username && $this->password) { |
|
| 54 | 3 | $io->setAuthentication($origin, $this->username, $this->password); |
|
| 55 | 33 | } elseif ($io->hasAuthentication($origin)) { |
|
| 56 | 2 | $auth = $io->getAuthentication($origin); |
|
| 57 | 2 | $this->username = $auth['username']; |
|
| 58 | 2 | $this->password = $auth['password']; |
|
| 59 | } |
||
| 60 | 35 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $url |
||
| 64 | */ |
||
| 65 | 35 | public function importURL($url) |
|
| 66 | { |
||
| 67 | 35 | $struct = parse_url($url); |
|
| 68 | // @codeCoverageIgnoreStart |
||
| 69 | if (!$struct) { |
||
| 70 | throw new \InvalidArgumentException("$url is not valid URL"); |
||
| 71 | } |
||
| 72 | // @codeCoverageIgnoreEnd |
||
| 73 | |||
| 74 | 35 | $this->scheme = self::setOr($struct, 'scheme'); |
|
| 75 | 35 | $this->host = self::setOr($struct, 'host'); |
|
| 76 | 35 | $this->port = self::setOr($struct, 'port'); |
|
| 77 | 35 | $this->path = self::setOr($struct, 'path'); |
|
| 78 | 35 | $this->username = self::setOr($struct, 'user'); |
|
| 79 | 35 | $this->password = self::setOr($struct, 'pass'); |
|
| 80 | |||
| 81 | 35 | if (!empty($struct['query'])) { |
|
| 82 | 9 | parse_str($struct['query'], $this->query); |
|
| 83 | } |
||
| 84 | 35 | } |
|
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $struct |
||
| 89 | * @param string $key |
||
| 90 | * @param string $default |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | 35 | private static function setOr(array $struct, $key, $default = null) |
|
| 94 | { |
||
| 95 | 35 | if (!empty($struct[$key])) { |
|
| 96 | 35 | return $struct[$key]; |
|
| 97 | } |
||
| 98 | |||
| 99 | 35 | return $default; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * process option for RemortFileSystem |
||
| 104 | * @param array $options |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | 8 | public function processRFSOption(array $options) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | 10 | public function getCurlOpts() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * enable ECC cipher suites in cURL/NSS |
||
| 150 | */ |
||
| 151 | 10 | public function nssCiphers() |
|
| 170 | |||
| 171 | 18 | public function getURL() |
|
| 191 | |||
| 192 | 5 | public function setConfig(CConfig $config) |
|
| 196 | |||
| 197 | 6 | public function promptAuth(HttpGetResponse $res, IO\IOInterface $io) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @internal |
||
| 225 | * @param int $privateCode 404|403 |
||
| 226 | * @param Composer\Util\GitHub|Composer\Util\GitLab $util |
||
| 227 | * @param HttpGetResponse $res |
||
| 228 | * @param IO\IOInterface $io |
||
| 229 | * @throws Composer\Downloader\TransportException |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 2 | public function promptAuthWithUtil($privateCode, $util, HttpGetResponse $res, IO\IOInterface $io) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 10 | public static function genUA() |
|
| 271 | } |
||
| 272 |
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.