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 Pin implements Api |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * Pin objects to local storage. |
||
28 | * |
||
29 | * @Endpoint(name="pin:add") |
||
30 | * |
||
31 | * @param string $arg path to object(s) to be pinned |
||
32 | * @param bool $recursive recursively pin the object linked to by the specified object(s) |
||
33 | * @param bool $progress show progress |
||
34 | * |
||
35 | * @return Command |
||
36 | */ |
||
37 | public function add(string $arg, bool $recursive = true, bool $progress = false): Command |
||
41 | |||
42 | /** |
||
43 | * List objects pinned to local storage. |
||
44 | * |
||
45 | * @Endpoint(name="pin:ls") |
||
46 | * |
||
47 | * @param string $arg path to object(s) to be listed |
||
48 | * @param string $type the type of pinned keys to list |
||
49 | * @param bool $quiet write just hashes of objects |
||
50 | * |
||
51 | * @return Command |
||
52 | */ |
||
53 | public function ls(string $arg = null, string $type = 'all', bool $quiet = false): Command |
||
57 | |||
58 | /** |
||
59 | * Remove pinned objects from local storage. |
||
60 | * |
||
61 | * @Endpoint(name="pin:rm") |
||
62 | * |
||
63 | * @param string $arg path to object(s) to be unpinned |
||
64 | * @param bool $recursive recursively unpin the object linked to by the specified object(s) |
||
65 | * |
||
66 | * @return Command |
||
67 | */ |
||
68 | public function rm(string $arg, bool $recursive = true): Command |
||
72 | |||
73 | /** |
||
74 | * Update a recursive pin. |
||
75 | * |
||
76 | * @Endpoint(name="pin:update") |
||
77 | * |
||
78 | * @param string $arg path to old object |
||
79 | * @param string $arg1 path to new object to be pinned |
||
80 | * @param bool $unpin remove the old pin |
||
81 | * |
||
82 | * @return Command |
||
83 | */ |
||
84 | public function update(string $arg, string $arg1, bool $unpin = true): Command |
||
88 | |||
89 | /** |
||
90 | * Verify that recursive pins are complete. |
||
91 | * |
||
92 | * @Endpoint(name="pin:verify") |
||
93 | * |
||
94 | * @param bool $verbose also write the hashes of non-broken pins |
||
95 | * @param bool $quiet write just hashes of broken pins |
||
96 | * |
||
97 | * @return Command |
||
98 | */ |
||
99 | public function verify(bool $verbose = false, bool $quiet = false): Command |
||
103 | } |
||
104 |
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.