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 |
||
24 | View Code Duplication | final class P2p implements Api |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * Close active p2p listener. |
||
28 | * |
||
29 | * @Endpoint(name="p2p:listener:close") |
||
30 | * |
||
31 | * @param string $arg P2P listener protocol Required: no |
||
32 | * @param bool $all close all listeners |
||
33 | * |
||
34 | * @return Command |
||
35 | */ |
||
36 | public function listenerClose(string $arg = null, bool $all = false): Command |
||
40 | |||
41 | /** |
||
42 | * List active p2p listeners. |
||
43 | * |
||
44 | * @Endpoint(name="p2p:listener:ls") |
||
45 | * |
||
46 | * @param bool $headers print table headers (HandlerID, Protocol, Local, Remote) |
||
47 | * |
||
48 | * @return Command |
||
49 | */ |
||
50 | public function listenerLs(bool $headers = false): Command |
||
54 | |||
55 | /** |
||
56 | * Forward p2p connections to a network multiaddr. |
||
57 | * |
||
58 | * @Endpoint(name="p2p:listener:open") |
||
59 | * |
||
60 | * @param string $arg protocol identifier |
||
61 | * @param string $arg1 request handling application address |
||
62 | * |
||
63 | * @return Command |
||
64 | */ |
||
65 | public function listenerOpen(string $arg, string $arg1): Command |
||
69 | |||
70 | /** |
||
71 | * Close active p2p stream. |
||
72 | * |
||
73 | * @Endpoint(name="p2p:stream:close") |
||
74 | * |
||
75 | * @param string $arg stream HandlerID Required: no |
||
76 | * @param bool $all close all streams |
||
77 | * |
||
78 | * @return Command |
||
79 | */ |
||
80 | public function streamClose(string $arg = null, bool $all = false): Command |
||
84 | |||
85 | /** |
||
86 | * Dial to a p2p listener. |
||
87 | * |
||
88 | * @Endpoint(name="p2p:stream:dial") |
||
89 | * |
||
90 | * @param string $arg remote peer to connect to Required: |
||
91 | * @param string $arg1 protocol identifier |
||
92 | * @param string $arg2 address to listen for connection/s (default: /ip4/127 |
||
93 | * |
||
94 | * @return Command |
||
95 | */ |
||
96 | public function streamDial(string $arg, string $arg1, string $arg2 = null): Command |
||
100 | |||
101 | /** |
||
102 | * List active p2p streams. |
||
103 | * |
||
104 | * @Endpoint(name="p2p:stream:ls") |
||
105 | * |
||
106 | * @param bool $headers print table headers (HagndlerID, Protocol, Local, Remote) |
||
107 | * |
||
108 | * @return Command |
||
109 | */ |
||
110 | public function streamLs(bool $headers = false): Command |
||
114 | } |
||
115 |
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.