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 | * @param array $config |
||
| 38 | */ |
||
| 39 | public function __construct(array $config = []) |
||
| 47 | |||
| 48 | public function init() |
||
| 55 | |||
| 56 | public function getError($asString = true) |
||
| 67 | |||
| 68 | public function validate(array $data) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $data |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function filter(array $data) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $param |
||
| 93 | * @param $value |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | public function filterParam($param, $value) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $param |
||
| 103 | * @param $value |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function validateParam($param, $value) |
||
| 110 | |||
| 111 | public function setGuzzleOptions($array) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param $url |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | protected function buildUrl($url) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return GuzzleClient |
||
| 131 | */ |
||
| 132 | public function getGuzzle() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $urlRequest |
||
| 139 | * @param array $data |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getContent($urlRequest, $data = []) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $data |
||
| 163 | * @return mixed|null|\SimpleXMLElement |
||
| 164 | */ |
||
| 165 | public function unSerialize($data) |
||
| 179 | |||
| 180 | public function addError($param, $message) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $data |
||
| 187 | * @return string|array |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | protected function prepareData(array $data) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @throws \Exception |
||
| 217 | */ |
||
| 218 | public function guzzleOptions() |
||
| 245 | } |
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: