Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Client |
||
| 11 | { |
||
| 12 | public $login; |
||
| 13 | public $password; |
||
| 14 | public $proxy; |
||
| 15 | public $method = 'GET'; |
||
| 16 | public $postDataInBody = false; |
||
| 17 | /** |
||
| 18 | * @var ResponseInterface |
||
| 19 | */ |
||
| 20 | public $request; |
||
| 21 | |||
| 22 | const TYPE_JSON = 'json'; |
||
| 23 | const TYPE_XML = 'xml'; |
||
| 24 | const TYPE_FORM = 'form'; |
||
| 25 | |||
| 26 | protected $protocol = 'https'; |
||
| 27 | protected $url = ''; |
||
| 28 | protected $type = 'json'; |
||
| 29 | protected $output_type; |
||
| 30 | protected $_guzzleOptions = []; |
||
| 31 | protected $_custom_guzzle_options = []; |
||
| 32 | protected $_guzzle; |
||
| 33 | protected $_errors; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Client constructor. |
||
| 37 | * |
||
| 38 | * @param array $config |
||
| 39 | */ |
||
| 40 | public function __construct(array $config = []) |
||
| 48 | |||
| 49 | public function init() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param bool $asString |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getError($asString = true) |
||
| 72 | |||
| 73 | public function validate(array $data) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param array $data |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function filter(array $data) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $param |
||
| 98 | * @param $value |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | public function filterParam($param, $value) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param $param |
||
| 108 | * @param $value |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function validateParam($param, $value) |
||
| 115 | |||
| 116 | public function setGuzzleOptions($array) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param $url |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | protected function buildUrl($url) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return GuzzleClient |
||
| 136 | */ |
||
| 137 | public function getGuzzle() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $urlRequest |
||
| 144 | * @param array $data |
||
| 145 | * @return string|\stdClass|\SimpleXMLElement |
||
| 146 | */ |
||
| 147 | public function getContent($urlRequest, $data = []) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $data |
||
| 168 | * @return mixed|null|\SimpleXMLElement |
||
| 169 | */ |
||
| 170 | public function unSerialize($data) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $param |
||
| 187 | * @param $message |
||
| 188 | */ |
||
| 189 | public function addError($param, $message) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param array $data |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | protected function beforePrepareData(array $data) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $data |
||
| 205 | * @return string|array |
||
| 206 | * @throws \Exception |
||
| 207 | */ |
||
| 208 | protected function prepareData(array $data) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @throws \Exception |
||
| 237 | */ |
||
| 238 | public function guzzleOptions() |
||
| 265 | } |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: