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 Extender 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 Extender, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Extender; |
||
38 | class Extender { |
||
39 | |||
40 | // configurable things |
||
41 | |||
42 | /** |
||
43 | * Max result lenght (in bytes) retrieved from parent in miltithread mode |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | private $max_result_bytes_in_multithread = null; |
||
48 | |||
49 | /** |
||
50 | * Maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | private $max_childs_runtime = null; |
||
55 | |||
56 | /** |
||
57 | * Multithread mode |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | private $multithread_mode = false; |
||
62 | |||
63 | /** |
||
64 | * Verbose mode, if requested via command line arg -v |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | private $verbose_mode = false; |
||
69 | |||
70 | /** |
||
71 | * Debug mode, if requested via command line arg -V |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $debug_mode = false; |
||
76 | |||
77 | /** |
||
78 | * Summary mode, if requested via command line arg -s |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $summary_mode = false; |
||
83 | |||
84 | /** |
||
85 | * Daemon mode, if requested via command line arg -d |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $daemon_mode = false; |
||
90 | |||
91 | /** |
||
92 | * Timestamp, relative, of current extend() cycle |
||
93 | * |
||
94 | * @var float |
||
95 | */ |
||
96 | private $timestamp = null; |
||
97 | |||
98 | /** |
||
99 | * Timestamp, absolute, sinnce extender was initiated |
||
100 | * |
||
101 | * @var float |
||
102 | */ |
||
103 | private $timestamp_absolute = null; |
||
104 | |||
105 | /** |
||
106 | * PID of the parent extender process |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | private $parent_pid = null; |
||
111 | |||
112 | /** |
||
113 | * Set exender in paused mode (no job will be processed) |
||
114 | * |
||
115 | * @var bool |
||
116 | */ |
||
117 | private $paused = false; |
||
118 | |||
119 | // Helper classes |
||
120 | |||
121 | /** |
||
122 | * Events manager instance |
||
123 | * |
||
124 | * @var \Comodojo\Extender\Events |
||
125 | */ |
||
126 | private $events = null; |
||
127 | |||
128 | /** |
||
129 | * Console_Color2 instance |
||
130 | * |
||
131 | * @var \Console_Color2 |
||
132 | */ |
||
133 | private $color = null; |
||
134 | |||
135 | /** |
||
136 | * Logger instance |
||
137 | * |
||
138 | * @var \Monolog\Logger |
||
139 | */ |
||
140 | private $logger = null; |
||
141 | |||
142 | /** |
||
143 | * JobsRunner instance |
||
144 | * |
||
145 | * @var \Comodojo\Extender\Runner\JobsRunner |
||
146 | */ |
||
147 | private $runner = null; |
||
148 | |||
149 | /** |
||
150 | * TasksTable instance |
||
151 | * |
||
152 | * @var \Comodojo\Extender\TasksTable |
||
153 | */ |
||
154 | private $tasks = null; |
||
155 | |||
156 | // checks and locks are static! |
||
157 | |||
158 | // local archives |
||
159 | |||
160 | /** |
||
161 | * Failed processes, refreshed each cycle (in daemon mode) |
||
162 | * |
||
163 | * @var int |
||
164 | */ |
||
165 | private $failed_processes = 0; |
||
166 | |||
167 | /** |
||
168 | * Completed processes |
||
169 | * |
||
170 | * @var int |
||
171 | */ |
||
172 | private $completed_processes = 0; |
||
173 | |||
174 | /** |
||
175 | * Constructor method |
||
176 | * |
||
177 | * Prepare extender environment, do checks and fire extender.ready event |
||
178 | */ |
||
179 | 12 | final public function __construct() { |
|
282 | |||
283 | /** |
||
284 | * Set max result length (in bytes) that should be read from child tasks |
||
285 | * |
||
286 | * @param int $bytes Maximum length (bytes) |
||
287 | * |
||
288 | * @return Extender $this |
||
289 | */ |
||
290 | 3 | final public function setMaxResultLength($bytes) { |
|
297 | |||
298 | /** |
||
299 | * Get max result length (in bytes) |
||
300 | * |
||
301 | * @return int Bytes parent should read (max) |
||
302 | */ |
||
303 | 3 | final public function getMaxResultLength() { |
|
308 | |||
309 | /** |
||
310 | * Set maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
311 | * |
||
312 | * After $time seconds, parent will start killing tasks |
||
313 | * |
||
314 | * @param int $time Maximum time (seconds) |
||
315 | * |
||
316 | * @return Extender $this |
||
317 | */ |
||
318 | 3 | final public function setMaxChildsRuntime($time) { |
|
325 | |||
326 | /** |
||
327 | * Get maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
328 | * |
||
329 | * @return int Time parent will wait for childs to be completed |
||
330 | */ |
||
331 | 3 | final public function getMaxChildsRuntime() { |
|
336 | |||
337 | /** |
||
338 | * Set working mode (single or multithread) |
||
339 | * |
||
340 | * If multithread enabled, extender will use pcntl to fork child tasks |
||
341 | * |
||
342 | * @param bool $mode Enable/disable multithread |
||
343 | * |
||
344 | * @return Extender $this |
||
345 | */ |
||
346 | 3 | final public function setMultithreadMode($mode) { |
|
353 | |||
354 | /** |
||
355 | * Get multithread mode status |
||
356 | * |
||
357 | * @return bool True if enabled, false if disabled |
||
358 | */ |
||
359 | 12 | final public function getMultithreadMode() { |
|
364 | |||
365 | /** |
||
366 | * Get daemon mode status |
||
367 | * |
||
368 | * @return bool True if enabled, false if disabled |
||
369 | */ |
||
370 | 6 | final public function getDaemonMode() { |
|
375 | |||
376 | /** |
||
377 | * Get the number of completed processes |
||
378 | * |
||
379 | * @return int |
||
380 | */ |
||
381 | 3 | final public function getCompletedProcesses() { |
|
386 | |||
387 | /** |
||
388 | * Get the number of failed processes |
||
389 | * |
||
390 | * @return int |
||
391 | */ |
||
392 | 3 | final public function getFailedProcesses() { |
|
397 | |||
398 | /** |
||
399 | * Get current version |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 3 | final public function getVersion() { |
|
408 | |||
409 | /** |
||
410 | * Get events manager |
||
411 | * |
||
412 | * @return \Comodojo\Extender\Events |
||
413 | */ |
||
414 | 6 | final public function events() { |
|
419 | |||
420 | /** |
||
421 | * Get console color instance |
||
422 | * |
||
423 | * @return \Console_Color2 |
||
424 | */ |
||
425 | 3 | final public function color() { |
|
430 | |||
431 | /** |
||
432 | * Get internal logger |
||
433 | * |
||
434 | * @return \Monolog\Logger |
||
435 | */ |
||
436 | 3 | final public function logger() { |
|
441 | |||
442 | /** |
||
443 | * Get jobs' runner |
||
444 | * |
||
445 | * @return JobsRunner |
||
446 | */ |
||
447 | 3 | final public function runner() { |
|
452 | |||
453 | /** |
||
454 | * Get the tasks' table |
||
455 | * |
||
456 | * @return TasksTable |
||
457 | */ |
||
458 | 6 | final public function tasks() { |
|
463 | |||
464 | /** |
||
465 | * Do extend! |
||
466 | * |
||
467 | */ |
||
468 | 3 | public function extend() { |
|
491 | |||
492 | /** |
||
493 | * Change parent process priority according to EXTENDER_NICENESS |
||
494 | * |
||
495 | */ |
||
496 | final public function adjustNiceness() { |
||
507 | |||
508 | /** |
||
509 | * Register signals |
||
510 | * |
||
511 | */ |
||
512 | final public function registerSignals() { |
||
549 | |||
550 | /** |
||
551 | * Delete all status file after exit() called |
||
552 | * |
||
553 | */ |
||
554 | 3 | final public function shutdown($force = false) { |
|
583 | |||
584 | /** |
||
585 | * The sigTerm handler. |
||
586 | * |
||
587 | * It kills everything and then exit with status 1 |
||
588 | */ |
||
589 | final public function sigTermHandler() { |
||
602 | |||
603 | /** |
||
604 | * The sigStop handler. |
||
605 | * |
||
606 | * It just pauses extender execution |
||
607 | */ |
||
608 | final public function sigStopHandler() { |
||
619 | |||
620 | /** |
||
621 | * The sigCont handler. |
||
622 | * |
||
623 | * It just resume extender execution |
||
624 | */ |
||
625 | final public function sigContHandler() { |
||
636 | |||
637 | /** |
||
638 | * The generig signal handler. |
||
639 | * |
||
640 | * It can be used to handle custom signals |
||
641 | */ |
||
642 | final public function genericSignalHandler($signal) { |
||
653 | |||
654 | 3 | private function cycle() { |
|
827 | |||
828 | private static function showHelp($color) { |
||
851 | |||
852 | 12 | private static function getCommandlineOptions() { |
|
865 | |||
866 | /** |
||
867 | * @param double $timestamp |
||
868 | */ |
||
869 | private static function showSummary($timestamp, $completed_processes, $color) { |
||
916 | |||
917 | /** |
||
918 | * @param integer $returnCode |
||
919 | */ |
||
920 | 3 | View Code Duplication | private static function end($returnCode) { |
935 | |||
936 | } |
||
937 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.