Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 14 | abstract class Client extends Pool |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array Array of servers |
||
| 19 | */ |
||
| 20 | protected $servers = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array Active connections |
||
| 24 | */ |
||
| 25 | protected $servConn = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $servConnFree = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var integer |
||
| 34 | */ |
||
| 35 | protected $maxConnPerServ = 32; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | protected $acquireOnGet = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $pending = []; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Setting default config options |
||
| 50 | * Overriden from ConnectionPool::getConfigDefaults |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | protected function getConfigDefaults() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Applies config |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | protected function applyConfig() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Adds server |
||
| 91 | * @param string $url Server URL |
||
| 92 | * @param integer $weight Weight |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function addServer($url, $weight = null) |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns available connection from the pool |
||
| 103 | * @param string $url Address |
||
| 104 | * @param callback $cb onConnected |
||
| 105 | * @param integer $pri Optional. Priority |
||
| 106 | * @param \Closure $beforeConnect Called before establishing the connection |
||
| 107 | * @call ( callable $cb ) |
||
| 108 | * @call ( string $url = null, callable $cb = null, integer $pri = 0 ) |
||
| 109 | * @return boolean Success|Connection |
||
| 110 | */ |
||
| 111 | public function getConnection($url = null, $cb = null, $pri = 0, \Closure $beforeConnect = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Detach Connection |
||
| 173 | * @param object $conn Connection |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function detach($conn) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Mark connection as free |
||
| 184 | * @param ClientConnection $conn Connection |
||
| 185 | * @param string $url URL |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function markConnFree(ClientConnection $conn, $url) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Mark connection as busy |
||
| 198 | * @param ClientConnection $conn Connection |
||
| 199 | * @param string $url URL |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function markConnBusy(ClientConnection $conn, $url) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Detaches connection from URL |
||
| 212 | * @param ClientConnection $conn Connection |
||
| 213 | * @param string $url URL |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function detachConnFromUrl(ClientConnection $conn, $url) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Touch pending "requests for connection" |
||
| 228 | * @param string $url URL |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function touchPending($url) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns available connection from the pool by key |
||
| 242 | * @param string $key Key |
||
| 243 | * @param callable $cb Callback |
||
| 244 | * @callback $cb ( ) |
||
| 245 | * @return boolean Success |
||
| 246 | */ |
||
| 247 | public function getConnectionByKey($key, $cb = null) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns available connection from the pool |
||
| 260 | * @param callable $cb Callback |
||
| 261 | * @callback $cb ( ) |
||
| 262 | * @return boolean Success |
||
| 263 | */ |
||
| 264 | public function getConnectionRR($cb = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sends a request to arbitrary server |
||
| 271 | * @param string $server Server |
||
| 272 | * @param string $data Data |
||
| 273 | * @param callable $onResponse Called when the request complete |
||
| 274 | * @callback $onResponse ( ) |
||
| 275 | * @return boolean Success |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function requestByServer($server, $data, $onResponse = null) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Sends a request to server according to the key |
||
| 291 | * @param string $key Key |
||
| 292 | * @param string $data Data |
||
| 293 | * @param callable $onResponse Callback called when the request complete |
||
| 294 | * @callback $onResponse ( ) |
||
| 295 | * @return boolean Success |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function requestByKey($key, $data, $onResponse = null) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Called when application instance is going to shutdown |
||
| 311 | * @param boolean $graceful Graceful? |
||
| 312 | * @return boolean Ready to shutdown? |
||
| 313 | */ |
||
| 314 | public function onShutdown($graceful = false) |
||
| 318 | } |
||
| 319 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.