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:
1 | <?php |
||
17 | class ForkHandler implements ThreadInterface |
||
18 | { |
||
19 | protected $threadKey; |
||
20 | private $callable; |
||
21 | private $pid; |
||
22 | |||
23 | /** |
||
24 | * constructor method |
||
25 | * |
||
26 | * @throws RuntimeException |
||
27 | * @throws InvalidArgumentException |
||
28 | */ |
||
29 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * Private function for set the method will be forked; |
||
41 | * |
||
42 | * @param callable $callable string with the function name or a array with the instance and the method name |
||
43 | * @return mixed|void |
||
44 | */ |
||
45 | public function setCallable(callable $callable) |
||
49 | |||
50 | /** |
||
51 | * Start the thread |
||
52 | * |
||
53 | * @throws RuntimeException |
||
54 | */ |
||
55 | public function execute() |
||
93 | |||
94 | /** |
||
95 | * Save the thread result in a shared memory block |
||
96 | * |
||
97 | * @param mixed $object Need to be serializable |
||
98 | */ |
||
99 | protected function saveResult($object) |
||
104 | |||
105 | /** |
||
106 | * Get the thread result from the shared memory block and erase it |
||
107 | * |
||
108 | * @return mixed |
||
109 | * @throws \Error |
||
110 | * @throws object |
||
111 | */ |
||
112 | public function getResult() |
||
131 | |||
132 | /** |
||
133 | * Kill a thread |
||
134 | * |
||
135 | * @param int $signal |
||
136 | * @param bool $wait |
||
137 | */ |
||
138 | public function stop($signal = SIGKILL, $wait = false) |
||
149 | |||
150 | /** |
||
151 | * Check if the forked process is alive |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isAlive() |
||
159 | |||
160 | /** |
||
161 | * Handle the signal to the thread |
||
162 | * |
||
163 | * @param int $signal |
||
164 | */ |
||
165 | private function signalHandler($signal) |
||
172 | |||
173 | public function waitFinish() |
||
177 | } |
||
178 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.