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 Pool 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 Pool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class Pool extends ObjectStorage |
||
| 18 | { |
||
| 19 | |||
| 20 | use EventLoopContainer; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string Default connection class |
||
| 24 | */ |
||
| 25 | public $connectionClass; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string Name |
||
| 29 | */ |
||
| 30 | public $name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \PHPDaemon\Config\Section Configuration |
||
| 34 | */ |
||
| 35 | public $config; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ConnectionPool[] Instances storage |
||
| 39 | */ |
||
| 40 | protected static $instances = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var integer Max concurrency |
||
| 44 | */ |
||
| 45 | public $maxConcurrency = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var integer Max allowed packet |
||
| 49 | */ |
||
| 50 | public $maxAllowedPacket = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var boolean Is finished? |
||
| 54 | */ |
||
| 55 | protected $finished = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var boolean Is enabled? |
||
| 59 | */ |
||
| 60 | protected $enabled = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var boolean Is overloaded? |
||
| 64 | */ |
||
| 65 | protected $overload = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var object|null Application instance object |
||
| 69 | */ |
||
| 70 | public $appInstance; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Constructor |
||
| 74 | * @param $config Config variables |
||
| 75 | * @param boolean $init |
||
| 76 | */ |
||
| 77 | public function __construct($config = [], $init = true) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Init |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | protected function init() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Called when the worker is ready to go |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function onReady() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Called when worker is going to update configuration |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public function onConfigUpdated() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Applies config |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | protected function applyConfig() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Setting default config options |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | protected function getConfigDefaults() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns instance object |
||
| 168 | * @param string $arg name / array config / ConfigSection |
||
| 169 | * @param boolean $spawn Spawn? Default is true |
||
| 170 | * @return this |
||
| 171 | */ |
||
| 172 | public static function getInstance($arg = '', $spawn = true) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets default connection class |
||
| 200 | * @param string $class Connection class name |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function setConnectionClass($class) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Enable socket events |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function enable() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Disable all events of sockets |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | public function disable() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Called when ConnectionPool is now enabled |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | protected function onEnable() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Called when ConnectionPool is now disabled |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | protected function onDisable() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Called when application instance is going to shutdown |
||
| 252 | * @param boolean $graceful |
||
| 253 | * @return boolean Ready to shutdown? |
||
| 254 | */ |
||
| 255 | public function onShutdown($graceful = false) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Called when ConnectionPool is finished |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | protected function onFinish() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Finishes ConnectionPool |
||
| 270 | * @return boolean Success |
||
| 271 | */ |
||
| 272 | public function finish($graceful = false) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Attach Connection |
||
| 298 | * @param object $conn Connection |
||
| 299 | * @param mixed $inf Info |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function attach($conn, $inf = null) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Detach Connection |
||
| 316 | * @param object $conn Connection |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | View Code Duplication | public function detach($conn) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Establish a connection with remote peer |
||
| 332 | * @param string $url URL |
||
| 333 | * @param callback $cb Callback |
||
| 334 | * @param string $class Optional. Connection class name |
||
| 335 | * @param \Closure $beforeConnect |
||
| 336 | * @return integer Connection's ID. Boolean false when failed |
||
| 337 | */ |
||
| 338 | public function connect($url, $cb, $class = null, \Closure $beforeConnect = null) |
||
| 347 | } |
||
| 348 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: