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() { |
|
288 | |||
289 | /** |
||
290 | * Set max result length (in bytes) that should be read from child tasks |
||
291 | * |
||
292 | * @param int $bytes Maximum length (bytes) |
||
293 | * |
||
294 | * @return Extender $this |
||
295 | */ |
||
296 | 3 | final public function setMaxResultLength($bytes) { |
|
303 | |||
304 | /** |
||
305 | * Get max result length (in bytes) |
||
306 | * |
||
307 | * @return int Bytes parent should read (max) |
||
308 | */ |
||
309 | 3 | final public function getMaxResultLength() { |
|
314 | |||
315 | /** |
||
316 | * Set maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
317 | * |
||
318 | * After $time seconds, parent will start killing tasks |
||
319 | * |
||
320 | * @param int $time Maximum time (seconds) |
||
321 | * |
||
322 | * @return Extender $this |
||
323 | */ |
||
324 | 3 | final public function setMaxChildsRuntime($time) { |
|
331 | |||
332 | /** |
||
333 | * Get maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
334 | * |
||
335 | * @return int Time parent will wait for childs to be completed |
||
336 | */ |
||
337 | 3 | final public function getMaxChildsRuntime() { |
|
342 | |||
343 | /** |
||
344 | * Set working mode (single or multithread) |
||
345 | * |
||
346 | * If multithread enabled, extender will use pcntl to fork child tasks |
||
347 | * |
||
348 | * @param bool $mode Enable/disable multithread |
||
349 | * |
||
350 | * @return Extender $this |
||
351 | */ |
||
352 | 3 | final public function setMultithreadMode($mode) { |
|
359 | |||
360 | /** |
||
361 | * Get multithread mode status |
||
362 | * |
||
363 | * @return bool True if enabled, false if disabled |
||
364 | */ |
||
365 | 12 | final public function getMultithreadMode() { |
|
370 | |||
371 | /** |
||
372 | * Get daemon mode status |
||
373 | * |
||
374 | * @return bool True if enabled, false if disabled |
||
375 | */ |
||
376 | 6 | final public function getDaemonMode() { |
|
381 | |||
382 | /** |
||
383 | * Get the number of completed processes |
||
384 | * |
||
385 | * @return int |
||
386 | */ |
||
387 | 3 | final public function getCompletedProcesses() { |
|
392 | |||
393 | /** |
||
394 | * Get the number of failed processes |
||
395 | * |
||
396 | * @return int |
||
397 | */ |
||
398 | 3 | final public function getFailedProcesses() { |
|
403 | |||
404 | /** |
||
405 | * Get current version |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | 3 | final public function getVersion() { |
|
414 | |||
415 | /** |
||
416 | * Get events manager |
||
417 | * |
||
418 | * @return \Comodojo\Extender\Events |
||
419 | */ |
||
420 | 6 | final public function events() { |
|
425 | |||
426 | /** |
||
427 | * Get console color instance |
||
428 | * |
||
429 | * @return \Console_Color2 |
||
430 | */ |
||
431 | 3 | final public function color() { |
|
436 | |||
437 | /** |
||
438 | * Get internal logger |
||
439 | * |
||
440 | * @return \Monolog\Logger |
||
441 | */ |
||
442 | 3 | final public function logger() { |
|
447 | |||
448 | /** |
||
449 | * Get jobs' runner |
||
450 | * |
||
451 | * @return JobsRunner |
||
452 | */ |
||
453 | 3 | final public function runner() { |
|
458 | |||
459 | /** |
||
460 | * Get the tasks' table |
||
461 | * |
||
462 | * @return TasksTable |
||
463 | */ |
||
464 | 6 | final public function tasks() { |
|
469 | |||
470 | /** |
||
471 | * Do extend! |
||
472 | * |
||
473 | */ |
||
474 | 3 | public function extend() { |
|
504 | |||
505 | /** |
||
506 | * Change parent process priority according to EXTENDER_NICENESS |
||
507 | * |
||
508 | */ |
||
509 | final public function adjustNiceness() { |
||
520 | |||
521 | /** |
||
522 | * Register signals |
||
523 | * |
||
524 | */ |
||
525 | final public function registerSignals() { |
||
562 | |||
563 | /** |
||
564 | * Delete all status file after exit() called |
||
565 | * |
||
566 | */ |
||
567 | 3 | final public function shutdown($force = false) { |
|
596 | |||
597 | /** |
||
598 | * The sigTerm handler. |
||
599 | * |
||
600 | * It kills everything and then exit with status 1 |
||
601 | */ |
||
602 | final public function sigTermHandler() { |
||
615 | |||
616 | /** |
||
617 | * The sigStop handler. |
||
618 | * |
||
619 | * It just pauses extender execution |
||
620 | */ |
||
621 | final public function sigStopHandler() { |
||
632 | |||
633 | /** |
||
634 | * The sigCont handler. |
||
635 | * |
||
636 | * It just resume extender execution |
||
637 | */ |
||
638 | final public function sigContHandler() { |
||
649 | |||
650 | /** |
||
651 | * The generig signal handler. |
||
652 | * |
||
653 | * It can be used to handle custom signals |
||
654 | */ |
||
655 | final public function genericSignalHandler($signal) { |
||
666 | |||
667 | 3 | private function cycle() { |
|
840 | |||
841 | private static function showHelp($color) { |
||
864 | |||
865 | 12 | private static function getCommandlineOptions() { |
|
878 | |||
879 | /** |
||
880 | * @param double $timestamp |
||
881 | */ |
||
882 | private static function showSummary($timestamp, $completed_processes, $color) { |
||
929 | |||
930 | /** |
||
931 | * @param integer $returnCode |
||
932 | */ |
||
933 | 3 | View Code Duplication | private static function end($returnCode) { |
948 | |||
949 | } |
||
950 |
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.