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 array $config Config variables |
||
| 75 | * @param boolean $init |
||
| 76 | */ |
||
| 77 | public function __construct($config = [], $init = true) |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Init |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | protected function init() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Called when the worker is ready to go |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function onReady() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Called when worker is going to update configuration |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function onConfigUpdated() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Applies config |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | protected function applyConfig() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Setting default config options |
||
| 153 | * @return boolean |
||
| 154 | */ |
||
| 155 | protected function getConfigDefaults() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns instance object |
||
| 162 | * @param string $arg name / array config / ConfigSection |
||
| 163 | * @param boolean $spawn Spawn? Default is true |
||
| 164 | * @return this |
||
| 165 | */ |
||
| 166 | public static function getInstance($arg = '', $spawn = true) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets default connection class |
||
| 195 | * @param string $class Connection class name |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function setConnectionClass($class) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Enable socket events |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function enable() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Disable all events of sockets |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function disable() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Called when ConnectionPool is now enabled |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | protected function onEnable() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Called when ConnectionPool is now disabled |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | protected function onDisable() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Called when application instance is going to shutdown |
||
| 247 | * @param boolean $graceful |
||
| 248 | * @return boolean Ready to shutdown? |
||
| 249 | */ |
||
| 250 | public function onShutdown($graceful = false) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Called when ConnectionPool is finished |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | protected function onFinish() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Finishes ConnectionPool |
||
| 265 | * @return boolean Success |
||
| 266 | */ |
||
| 267 | public function finish($graceful = false) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Attach Connection |
||
| 293 | * @param object $conn Connection |
||
| 294 | * @param mixed $inf Info |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function attach($conn, $inf = null) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Detach Connection |
||
| 311 | * @param object $conn Connection |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | View Code Duplication | public function detach($conn) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Establish a connection with remote peer |
||
| 327 | * @param string $url URL |
||
| 328 | * @param callback $cb Callback |
||
| 329 | * @param string $class Optional. Connection class name |
||
| 330 | * @return integer Connection's ID. Boolean false when failed |
||
| 331 | */ |
||
| 332 | public function connect($url, $cb, $class = null) |
||
| 341 | } |
||
| 342 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..