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 |
||
16 | class Messenger extends EventEmitter |
||
17 | { |
||
18 | const INTERVAL = 0.1; |
||
19 | const TERMINATE_RPC = 'wyrihaximus.react.child-process.messenger.terminate'; |
||
20 | |||
21 | /** |
||
22 | * @var Stream |
||
23 | */ |
||
24 | protected $stdin; |
||
25 | |||
26 | /** |
||
27 | * @var Stream |
||
28 | */ |
||
29 | protected $stdout; |
||
30 | |||
31 | /** |
||
32 | * @var Stream |
||
33 | */ |
||
34 | protected $stderr; |
||
35 | |||
36 | /** |
||
37 | * @var OutstandingCalls |
||
38 | */ |
||
39 | protected $outstandingRpcCalls; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $rpcs = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $options = []; |
||
50 | |||
51 | /** |
||
52 | * @var string[] |
||
53 | */ |
||
54 | protected $buffers = [ |
||
55 | 'stdin' => '', |
||
56 | 'stdout' => '', |
||
57 | 'stderr' => '', |
||
58 | ]; |
||
59 | |||
60 | protected $defaultOptions = [ |
||
61 | 'lineClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Line', |
||
62 | 'messageFactoryClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Factory', |
||
63 | 'lineOptions' => [], |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * @param Stream $stdin |
||
68 | * @param Stream $stdout |
||
69 | * @param Stream $stderr |
||
70 | 8 | * @param array $options |
|
71 | */ |
||
72 | 8 | public function __construct(Stream $stdin, Stream $stdout, Stream $stderr, array $options) |
|
83 | |||
84 | /** |
||
85 | * @param string $target |
||
86 | 2 | * @param callable $listener |
|
87 | */ |
||
88 | 2 | public function registerRpc($target, callable $listener) |
|
92 | |||
93 | /** |
||
94 | * @param string $target |
||
95 | */ |
||
96 | public function deregisterRpc($target) |
||
100 | |||
101 | /** |
||
102 | * @param string $target |
||
103 | 1 | * @return bool |
|
104 | */ |
||
105 | 1 | public function hasRpc($target) |
|
109 | |||
110 | /** |
||
111 | * @param $target |
||
112 | * @param $payload |
||
113 | 2 | * @return React\Promise\PromiseInterface |
|
114 | */ |
||
115 | public function callRpc($target, $payload) |
||
128 | |||
129 | protected function attachMessenger() |
||
150 | |||
151 | /** |
||
152 | 2 | * @param string $line |
|
153 | */ |
||
154 | 2 | View Code Duplication | protected function write($line) |
162 | |||
163 | /** |
||
164 | * @param string $line |
||
165 | */ |
||
166 | View Code Duplication | protected function writeErr($line) |
|
174 | |||
175 | /** |
||
176 | 1 | * @param Message $message |
|
177 | */ |
||
178 | 1 | public function message(Message $message) |
|
182 | |||
183 | /** |
||
184 | * @param Error $error |
||
185 | */ |
||
186 | public function error(Error $error) |
||
190 | |||
191 | /** |
||
192 | * @param string $uniqid |
||
193 | * @return OutstandingCall |
||
194 | */ |
||
195 | public function getOutstandingCall($uniqid) |
||
199 | |||
200 | 1 | /** |
|
201 | * @param Rpc $rpc |
||
202 | 1 | * @return \React\Promise\Promise |
|
203 | */ |
||
204 | public function rpc(Rpc $rpc) |
||
214 | |||
215 | 2 | /** |
|
216 | 1 | * @param string $data |
|
217 | 1 | * @param string $source |
|
218 | 1 | */ |
|
219 | 1 | protected function onData($data, $source) |
|
231 | 1 | ||
232 | 1 | /** |
|
233 | * @param array $messages |
||
234 | * @param string $source |
||
235 | 1 | */ |
|
236 | 1 | protected function iterateMessages(array $messages, $source) |
|
247 | |||
248 | /** |
||
249 | * @param ActionableMessageInterface $line |
||
250 | * @return LineInterface |
||
251 | */ |
||
252 | public function createLine(ActionableMessageInterface $line) |
||
257 | |||
258 | /** |
||
259 | 3 | * @return \React\Promise\Promise |
|
260 | */ |
||
261 | 3 | public function softTerminate() |
|
265 | |||
266 | /** |
||
267 | 3 | * @return Stream |
|
268 | */ |
||
269 | 3 | public function getStdin() |
|
273 | |||
274 | /** |
||
275 | 3 | * @return Stream |
|
276 | */ |
||
277 | 3 | public function getStdout() |
|
281 | |||
282 | /** |
||
283 | * @return Stream |
||
284 | */ |
||
285 | public function getStderr() |
||
289 | |||
290 | 1 | /** |
|
291 | 1 | * Forward any unknown calls when there is a call forward possible. |
|
292 | 1 | * |
|
293 | * @param string $name |
||
294 | * @param array $arguments |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function __call($name, array $arguments) |
||
305 | } |
||
306 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.