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 Threads 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 Threads, and based on these observations, apply Extract Interface, too.
1 | <?php /** MicroThreads */ |
||
19 | abstract class Threads |
||
20 | { |
||
21 | /** @var string $name thread name */ |
||
22 | private $name; |
||
23 | /** @var integer $pid process ID */ |
||
24 | private $pid; |
||
25 | /** @var integer $puid process UID */ |
||
26 | private $puid; |
||
27 | /** @var integer $guid process GUID */ |
||
28 | private $guid; |
||
29 | /** @var bool $isChild is child */ |
||
30 | private $isChild = false; |
||
31 | /** @var array $internalIPCArray Internal IPC array */ |
||
32 | private $internalIPCArray = []; |
||
33 | /** @var integer $internalIPCKey Internal IPC key */ |
||
34 | private $internalIPCKey; |
||
35 | /** @var integer $internalSemaphoreKey Internal semaphore key */ |
||
36 | private $internalSemaphoreKey; |
||
37 | /** @var bool $isIPC is IPC */ |
||
38 | private $isIPC = false; |
||
39 | /** @var bool $running is running */ |
||
40 | private $running = false; |
||
41 | /** @var string $fileIPC1 file IPC1 */ |
||
42 | private $fileIPC1; |
||
43 | /** @var string $fileIPC2 file IPC2 */ |
||
44 | private $fileIPC2; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Constructor thread |
||
49 | * |
||
50 | * @access public |
||
51 | * |
||
52 | * @param string $name |
||
53 | * @param int $puid |
||
54 | * @param int $guid |
||
55 | * @param int $umask |
||
56 | * |
||
57 | * @result void |
||
58 | * @throws Exception |
||
59 | */ |
||
60 | public function __construct($name, $puid = 0, $guid = 0, $umask = -1) |
||
81 | |||
82 | /** |
||
83 | * Create IPC segment |
||
84 | * |
||
85 | * @access protected |
||
86 | * |
||
87 | * @return bool |
||
88 | * @throws Exception |
||
89 | */ |
||
90 | View Code Duplication | protected function createIPCSegment() |
|
108 | |||
109 | /** |
||
110 | * get thread name |
||
111 | * |
||
112 | * @access public |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getName() |
||
119 | |||
120 | /** |
||
121 | * Set thread name |
||
122 | * |
||
123 | * @access public |
||
124 | * |
||
125 | * @param string $name |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function setName($name) |
||
133 | |||
134 | /** |
||
135 | * Create IPC semaphore |
||
136 | * |
||
137 | * @access protected |
||
138 | * |
||
139 | * @return bool |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | View Code Duplication | protected function createIPCSemaphore() |
|
160 | |||
161 | /** |
||
162 | * Is running thread |
||
163 | * |
||
164 | * @access public |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function isRunning() |
||
171 | |||
172 | /** |
||
173 | * Set alive |
||
174 | * |
||
175 | * @access public |
||
176 | * |
||
177 | * @return void |
||
178 | * @throws \Micro\Base\Exception |
||
179 | */ |
||
180 | public function setAlive() |
||
184 | |||
185 | /** |
||
186 | * Set variable in shared memory |
||
187 | * |
||
188 | * @access public |
||
189 | * |
||
190 | * @param string $name |
||
191 | * @param mixed $value |
||
192 | * |
||
193 | * @return void |
||
194 | * @throws \Micro\Base\Exception |
||
195 | */ |
||
196 | public function setVariable($name, $value) |
||
201 | |||
202 | /** |
||
203 | * Write to IPC segment |
||
204 | * |
||
205 | * @access protected |
||
206 | * @return void |
||
207 | * @throws Exception |
||
208 | */ |
||
209 | protected function writeToIPCSegment() |
||
225 | |||
226 | /** |
||
227 | * Get last alive |
||
228 | * |
||
229 | * @access public |
||
230 | * @return int |
||
231 | * @throws \Micro\Base\Exception |
||
232 | */ |
||
233 | public function getLastAlive() |
||
242 | |||
243 | /** |
||
244 | * Get variable from shared memory |
||
245 | * |
||
246 | * @access public |
||
247 | * |
||
248 | * @param string $name |
||
249 | * |
||
250 | * @return mixed |
||
251 | * @throws \Micro\Base\Exception |
||
252 | */ |
||
253 | public function getVariable($name) |
||
259 | |||
260 | /** |
||
261 | * Read from IPC segment |
||
262 | * |
||
263 | * @access public |
||
264 | * @return void |
||
265 | * @throws Exception |
||
266 | */ |
||
267 | protected function readFromIPCSegment() |
||
279 | |||
280 | /** |
||
281 | * Get thread process ID |
||
282 | * |
||
283 | * @access public |
||
284 | * @return int |
||
285 | */ |
||
286 | public function getPid() |
||
290 | |||
291 | /** |
||
292 | * Register callback func into shared memory |
||
293 | * |
||
294 | * @access public |
||
295 | * |
||
296 | * @param mixed $argList |
||
297 | * @param string $methodName |
||
298 | * |
||
299 | * @return mixed|void |
||
300 | * @throws \Micro\Base\Exception |
||
301 | */ |
||
302 | public function register_callback_func($argList, $methodName) |
||
339 | |||
340 | /** |
||
341 | * Send signal USR1 |
||
342 | * |
||
343 | * @access protected |
||
344 | * @return void |
||
345 | */ |
||
346 | protected function sendSigUsr1() |
||
352 | |||
353 | /** |
||
354 | * Wait IPC semaphore |
||
355 | * |
||
356 | * @access protected |
||
357 | * @return void |
||
358 | */ |
||
359 | protected function waitIPCSemaphore() |
||
371 | |||
372 | /** |
||
373 | * Start |
||
374 | * |
||
375 | * @access public |
||
376 | * |
||
377 | * @return void |
||
378 | * @throws Exception |
||
379 | */ |
||
380 | public function start() |
||
410 | |||
411 | /** |
||
412 | * Running thread |
||
413 | * |
||
414 | * @access public |
||
415 | * @return void |
||
416 | */ |
||
417 | abstract public function run(); |
||
418 | |||
419 | /** |
||
420 | * Stop thread |
||
421 | * |
||
422 | * @access public |
||
423 | * @return bool |
||
424 | */ |
||
425 | public function stop() |
||
440 | |||
441 | /** |
||
442 | * Clean thread context |
||
443 | * |
||
444 | * @access protected |
||
445 | * @return void |
||
446 | */ |
||
447 | protected function cleanThreadContext() |
||
461 | |||
462 | /** |
||
463 | * Signal handler |
||
464 | * |
||
465 | * @access protected |
||
466 | * |
||
467 | * @param int $sigNo |
||
468 | * |
||
469 | * @return void |
||
470 | * @throws \Micro\Base\Exception |
||
471 | */ |
||
472 | protected function sigHandler($sigNo) |
||
509 | } |
||
510 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: