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 = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Check the version of the given property in the User-Agent. |
||
| 95 | * |
||
| 96 | * @param string $propertyName |
||
| 97 | * @return string|float|false |
||
| 98 | * |
||
| 99 | * @see \Jenssegers\Agent\Agent::version() |
||
| 100 | */ |
||
| 101 | public function version($propertyName, $type = Agent::VERSION_TYPE_STRING) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check a certain value exists, case insensitived. |
||
| 110 | * |
||
| 111 | * @param string $value |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function is($value) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get or check the current app channel. |
||
| 123 | * |
||
| 124 | * @return string|bool |
||
| 125 | */ |
||
| 126 | public function appChannel() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set the User-Agent to be used. |
||
| 141 | * |
||
| 142 | * @param string $userAgent |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function setUserAgent($userAgent = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Parse Agent information. |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | protected function parseAgent() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Parse common client. |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function parseCommonClient() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Parse app client from the User-Agent. |
||
| 198 | * |
||
| 199 | * @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)` |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | protected function parseAppClient() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get app client information from the User-Agent. |
||
| 218 | * |
||
| 219 | * @param string $userAgent |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function getAppClientData($userAgent) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get app client attributes. |
||
| 237 | * |
||
| 238 | * @param array $info |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | protected function getAppClientAttributes($info) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Reset app client attributes. |
||
| 271 | */ |
||
| 272 | protected function resetAppClientAttributes() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Handle dynamic calls to the Agent instance. |
||
| 286 | * |
||
| 287 | * @param string $method |
||
| 288 | * @param array $parameters |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | public function __call($method, $parameters) |
||
| 295 | } |
||
| 296 |