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 |
||
16 | abstract class Pool extends ObjectStorage |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var string Default connection class |
||
21 | */ |
||
22 | public $connectionClass; |
||
23 | |||
24 | /** |
||
25 | * @var string Name |
||
26 | */ |
||
27 | public $name; |
||
28 | |||
29 | /** |
||
30 | * @var \PHPDaemon\Config\Section Configuration |
||
31 | */ |
||
32 | public $config; |
||
33 | |||
34 | /** |
||
35 | * @var ConnectionPool[] Instances storage |
||
36 | */ |
||
37 | protected static $instances = []; |
||
38 | |||
39 | /** |
||
40 | * @var integer Max concurrency |
||
41 | */ |
||
42 | public $maxConcurrency = 0; |
||
43 | |||
44 | /** |
||
45 | * @var integer Max allowed packet |
||
46 | */ |
||
47 | public $maxAllowedPacket = 0; |
||
48 | |||
49 | /** |
||
50 | * @var boolean Is finished? |
||
51 | */ |
||
52 | protected $finished = false; |
||
53 | |||
54 | /** |
||
55 | * @var boolean Is enabled? |
||
56 | */ |
||
57 | protected $enabled = false; |
||
58 | |||
59 | /** |
||
60 | * @var boolean Is overloaded? |
||
61 | */ |
||
62 | protected $overload = false; |
||
63 | |||
64 | /** |
||
65 | * @var object|null Application instance object |
||
66 | */ |
||
67 | public $appInstance; |
||
68 | |||
69 | /** |
||
70 | * Constructor |
||
71 | * @param array $config Config variables |
||
72 | * @param boolean $init |
||
73 | */ |
||
74 | public function __construct($config = [], $init = true) |
||
87 | |||
88 | /** |
||
89 | * Init |
||
90 | * @return void |
||
91 | */ |
||
92 | protected function init() |
||
95 | |||
96 | /** |
||
97 | * Called when the worker is ready to go |
||
98 | * @return void |
||
99 | */ |
||
100 | public function onReady() |
||
104 | |||
105 | /** |
||
106 | * Called when worker is going to update configuration |
||
107 | * @return void |
||
108 | */ |
||
109 | public function onConfigUpdated() |
||
123 | |||
124 | /** |
||
125 | * Applies config |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function applyConfig() |
||
146 | |||
147 | /** |
||
148 | * Setting default config options |
||
149 | * @return boolean |
||
150 | */ |
||
151 | protected function getConfigDefaults() |
||
155 | |||
156 | /** |
||
157 | * Returns instance object |
||
158 | * @param string $arg name / array config / ConfigSection |
||
159 | * @param boolean $spawn Spawn? Default is true |
||
160 | * @return this |
||
161 | */ |
||
162 | public static function getInstance($arg = '', $spawn = true) |
||
186 | |||
187 | /** |
||
188 | * Sets default connection class |
||
189 | * @param string $class Connection class name |
||
190 | * @return void |
||
191 | */ |
||
192 | public function setConnectionClass($class) |
||
196 | |||
197 | /** |
||
198 | * Enable socket events |
||
199 | * @return void |
||
200 | */ |
||
201 | public function enable() |
||
209 | |||
210 | /** |
||
211 | * Disable all events of sockets |
||
212 | * @return void |
||
213 | */ |
||
214 | public function disable() |
||
222 | |||
223 | /** |
||
224 | * Called when ConnectionPool is now enabled |
||
225 | * @return void |
||
226 | */ |
||
227 | protected function onEnable() |
||
230 | |||
231 | /** |
||
232 | * Called when ConnectionPool is now disabled |
||
233 | * @return void |
||
234 | */ |
||
235 | protected function onDisable() |
||
238 | |||
239 | /** |
||
240 | * Called when application instance is going to shutdown |
||
241 | * @param boolean $graceful |
||
242 | * @return boolean Ready to shutdown? |
||
243 | */ |
||
244 | public function onShutdown($graceful = false) |
||
248 | |||
249 | /** |
||
250 | * Called when ConnectionPool is finished |
||
251 | * @return void |
||
252 | */ |
||
253 | protected function onFinish() |
||
256 | |||
257 | /** |
||
258 | * Finishes ConnectionPool |
||
259 | * @return boolean Success |
||
260 | */ |
||
261 | public function finish($graceful = false) |
||
284 | |||
285 | /** |
||
286 | * Attach Connection |
||
287 | * @param object $conn Connection |
||
288 | * @param mixed $inf Info |
||
289 | * @return void |
||
290 | */ |
||
291 | View Code Duplication | public function attach($conn, $inf = null) |
|
302 | |||
303 | /** |
||
304 | * Detach Connection |
||
305 | * @param object $conn Connection |
||
306 | * @return void |
||
307 | */ |
||
308 | View Code Duplication | public function detach($conn) |
|
318 | |||
319 | /** |
||
320 | * Establish a connection with remote peer |
||
321 | * @param string $url URL |
||
322 | * @param callback $cb Callback |
||
323 | * @param string $class Optional. Connection class name |
||
324 | * @return integer Connection's ID. Boolean false when failed |
||
325 | */ |
||
326 | public function connect($url, $cb, $class = null) |
||
335 | } |
||
336 |
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..