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 |
||
| 8 | class Client |
||
| 9 | { |
||
| 10 | public $login; |
||
| 11 | public $password; |
||
| 12 | public $proxy; |
||
| 13 | public $method = 'GET'; |
||
| 14 | public $postDataInBody = false; |
||
| 15 | |||
| 16 | const TYPE_JSON = 'json'; |
||
| 17 | const TYPE_XML = 'xml'; |
||
| 18 | const TYPE_FORM = 'form'; |
||
| 19 | |||
| 20 | protected $protocol = 'https'; |
||
| 21 | protected $url = ''; |
||
| 22 | protected $type = 'json'; |
||
| 23 | protected $output_type; |
||
| 24 | protected $_guzzleOptions = []; |
||
| 25 | protected $_guzzle; |
||
| 26 | protected $_errors; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Client constructor. |
||
| 30 | * @param array $config |
||
| 31 | */ |
||
| 32 | public function __construct(array $config = []) |
||
| 40 | |||
| 41 | public function init() |
||
| 48 | |||
| 49 | public function getError($asString = true) |
||
| 60 | |||
| 61 | public function validate(array $data) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param array $data |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function filter(array $data) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $param |
||
| 86 | * @param $value |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | public function filterParam($param, $value) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $param |
||
| 96 | * @param $value |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function validateParam($param, $value) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $url |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | protected function buildUrl($url) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param $urlRequest |
||
| 119 | * @param array $data |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function getContent($urlRequest, $data = []) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $data |
||
| 142 | * @return mixed|null|\SimpleXMLElement |
||
| 143 | */ |
||
| 144 | public function unSerialize($data) |
||
| 158 | |||
| 159 | public function addError($param, $message) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $data |
||
| 166 | * @return string|array |
||
| 167 | * @throws \Exception |
||
| 168 | */ |
||
| 169 | protected function prepareData(array $data) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @throws \Exception |
||
| 196 | */ |
||
| 197 | public function guzzleOptions() |
||
| 224 | } |
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: