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 |
||
8 | View Code Duplication | class TickFiniteQueue |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * @var LoopModelInterface |
||
12 | */ |
||
13 | protected $loop; |
||
14 | |||
15 | /** |
||
16 | * @var SplQueue |
||
17 | */ |
||
18 | protected $queue; |
||
19 | |||
20 | /** |
||
21 | * @var callable |
||
22 | */ |
||
23 | private $callback; |
||
24 | |||
25 | /** |
||
26 | * @param LoopModelInterface $loop |
||
27 | */ |
||
28 | 61 | public function __construct(LoopModelInterface $loop) |
|
33 | |||
34 | /** |
||
35 | * |
||
36 | */ |
||
37 | 10 | public function __destruct() |
|
42 | |||
43 | /** |
||
44 | * Add a callback to be invoked on a future tick of the event loop. |
||
45 | * |
||
46 | * Callbacks are guaranteed to be executed in the order they are enqueued, before any timer or stream events. |
||
47 | * |
||
48 | * @param callable $listener |
||
49 | */ |
||
50 | 17 | public function add(callable $listener) |
|
54 | |||
55 | /** |
||
56 | * Flush the callback queue. |
||
57 | * |
||
58 | * Invokes as many callbacks as were on the queue when tick() was called. |
||
59 | */ |
||
60 | 39 | public function tick() |
|
71 | |||
72 | /** |
||
73 | * Check if the next tick queue is empty. |
||
74 | * |
||
75 | * @return boolean |
||
76 | */ |
||
77 | 6 | public function isEmpty() |
|
81 | } |
||
82 |
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.