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) |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Init |
||
97 | * @return void |
||
98 | */ |
||
99 | protected function init() |
||
102 | |||
103 | /** |
||
104 | * Called when the worker is ready to go |
||
105 | * @return void |
||
106 | */ |
||
107 | public function onReady() |
||
111 | |||
112 | /** |
||
113 | * Called when worker is going to update configuration |
||
114 | * @return void |
||
115 | */ |
||
116 | public function onConfigUpdated() |
||
130 | |||
131 | /** |
||
132 | * Applies config |
||
133 | * @return void |
||
134 | */ |
||
135 | protected function applyConfig() |
||
153 | |||
154 | /** |
||
155 | * Setting default config options |
||
156 | * @return boolean |
||
157 | */ |
||
158 | protected function getConfigDefaults() |
||
162 | |||
163 | /** |
||
164 | * Returns instance object |
||
165 | * @param string $arg name / array config / ConfigSection |
||
166 | * @param boolean $spawn Spawn? Default is true |
||
167 | * @return this |
||
168 | */ |
||
169 | public static function getInstance($arg = '', $spawn = true) |
||
194 | |||
195 | /** |
||
196 | * Sets default connection class |
||
197 | * @param string $class Connection class name |
||
198 | * @return void |
||
199 | */ |
||
200 | public function setConnectionClass($class) |
||
204 | |||
205 | /** |
||
206 | * Enable socket events |
||
207 | * @return void |
||
208 | */ |
||
209 | public function enable() |
||
217 | |||
218 | /** |
||
219 | * Disable all events of sockets |
||
220 | * @return void |
||
221 | */ |
||
222 | public function disable() |
||
230 | |||
231 | /** |
||
232 | * Called when ConnectionPool is now enabled |
||
233 | * @return void |
||
234 | */ |
||
235 | protected function onEnable() |
||
238 | |||
239 | /** |
||
240 | * Called when ConnectionPool is now disabled |
||
241 | * @return void |
||
242 | */ |
||
243 | protected function onDisable() |
||
246 | |||
247 | /** |
||
248 | * Called when application instance is going to shutdown |
||
249 | * @param boolean $graceful |
||
250 | * @return boolean Ready to shutdown? |
||
251 | */ |
||
252 | public function onShutdown($graceful = false) |
||
256 | |||
257 | /** |
||
258 | * Called when ConnectionPool is finished |
||
259 | * @return void |
||
260 | */ |
||
261 | protected function onFinish() |
||
264 | |||
265 | /** |
||
266 | * Finishes ConnectionPool |
||
267 | * @return boolean Success |
||
268 | */ |
||
269 | public function finish($graceful = false) |
||
292 | |||
293 | /** |
||
294 | * Attach Connection |
||
295 | * @param object $conn Connection |
||
296 | * @param mixed $inf Info |
||
297 | * @return void |
||
298 | */ |
||
299 | View Code Duplication | public function attach($conn, $inf = null) |
|
310 | |||
311 | /** |
||
312 | * Detach Connection |
||
313 | * @param object $conn Connection |
||
314 | * @return void |
||
315 | */ |
||
316 | View Code Duplication | public function detach($conn) |
|
326 | |||
327 | /** |
||
328 | * Establish a connection with remote peer |
||
329 | * @param string $url URL |
||
330 | * @param callback $cb Callback |
||
331 | * @param string $class Optional. Connection class name |
||
332 | * @return integer Connection's ID. Boolean false when failed |
||
333 | */ |
||
334 | public function connect($url, $cb, $class = null) |
||
343 | } |
||
344 |
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..