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 |
||
| 22 | class Client extends Fluent |
||
| 23 | { |
||
| 24 | use FluentArrayAccess; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The Agent instance. |
||
| 28 | * |
||
| 29 | * @var \Jenssegers\Agent\Agent |
||
| 30 | */ |
||
| 31 | protected $agent; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return this instance. |
||
| 35 | * |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function instance() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get the Agent instance. |
||
| 45 | * |
||
| 46 | * @return \Jenssegers\Agent\Agent |
||
| 47 | */ |
||
| 48 | public function agent() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set the Agent instance. |
||
| 55 | * |
||
| 56 | * @param \Jenssegers\Agent\Agent $agent |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function setAgent(Agent $agent) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Merge new data into the current attributes. |
||
| 68 | * |
||
| 69 | * @param array ...$data |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function add(array ...$data) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set a given attribute value. |
||
| 81 | * |
||
| 82 | * @param array|string $key |
||
| 83 | * @param mixed $value |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function set($key, $value = true) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Remove attributes. |
||
| 95 | * |
||
| 96 | * @param array|string $keys |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function remove($keys) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Check the version of the given property in the User-Agent. |
||
| 112 | * |
||
| 113 | * @param string $propertyName |
||
| 114 | * @return string|float|false |
||
| 115 | * |
||
| 116 | * @see \Jenssegers\Agent\Agent::version() |
||
| 117 | */ |
||
| 118 | public function version($propertyName, $type = Agent::VERSION_TYPE_STRING) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Check a certain value exists, case insensitived. |
||
| 127 | * |
||
| 128 | * @param string $value |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function is($value) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get or check the current app channel. |
||
| 140 | * |
||
| 141 | * @return string|bool |
||
| 142 | */ |
||
| 143 | public function appChannel() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the User-Agent to be used. |
||
| 162 | * |
||
| 163 | * @param string $userAgent |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setUserAgent($userAgent = null) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Parse Agent information. |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | protected function parseAgent() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Parse common client. |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | protected function parseCommonClient() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Parse app client from the User-Agent. |
||
| 219 | * |
||
| 220 | * @example `Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H143 _ua(eyJuZXQiOiJXaUZpIiwib3MiOiJpT1MiLCJhcHBWIjoiMC4xLjIiLCJvc1YiOiI4LjQiLCJhcHAiOiJndXBpYW8iLCJhcHBDIjoiRGVidWciLCJ0ZGlkIjoiaDNiYjFmNTBhYzBhMzdkYmE4ODhlMTgyNjU3OWJkZmZmIiwiYWNpZCI6IjIxZDNmYmQzNDNmMjViYmI0MzU2ZGEyMmJmZjUxZDczZjg0YWQwNmQiLCJsb2MiOiJ6aF9DTiIsInBmIjoiaVBob25lNywxIn0)` |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | protected function parseAppClient() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get app client information from the User-Agent. |
||
| 239 | * |
||
| 240 | * @param string $userAgent |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | protected function getAppClientData($userAgent) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get app client attributes. |
||
| 258 | * |
||
| 259 | * @param array $info |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | protected function getAppClientAttributes($info) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Reset app client attributes. |
||
| 292 | */ |
||
| 293 | protected function resetAppClientAttributes() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Handle dynamic calls to the Agent instance. |
||
| 307 | * |
||
| 308 | * @param string $method |
||
| 309 | * @param array $parameters |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public function __call($method, $parameters) |
||
| 316 | } |
||
| 317 |