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 |
||
23 | View Code Duplication | class Upstream extends AbstractNode |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * add upstream |
||
27 | * |
||
28 | * @param \Unikorp\KongAdminApi\Document\Upstream $document |
||
29 | * |
||
30 | * @return \Psr\Http\Message\ResponseInterface |
||
31 | */ |
||
32 | 1 | public function addUpstream(Document $document): ResponseInterface |
|
36 | |||
37 | /** |
||
38 | * retrieve upstream |
||
39 | * |
||
40 | * @param string $nameOrId |
||
41 | * |
||
42 | * @return \Psr\Http\Message\ResponseInterface |
||
43 | */ |
||
44 | 1 | public function retrieveUpstream(string $nameOrId): ResponseInterface |
|
48 | |||
49 | /** |
||
50 | * list upstreams |
||
51 | * |
||
52 | * @return \Psr\Http\Message\ResponseInterface |
||
53 | */ |
||
54 | 1 | public function listUpstreams(): ResponseInterface |
|
58 | |||
59 | /** |
||
60 | * update upstream |
||
61 | * |
||
62 | * @param string $nameOrId |
||
63 | * @param \Unikorp\KongAdminApi\Document\Upstream $document |
||
64 | * |
||
65 | * @return \Psr\Http\Message\ResponseInterface |
||
66 | */ |
||
67 | 1 | public function updateUpstream(string $nameOrId, Document $document): ResponseInterface |
|
71 | |||
72 | /** |
||
73 | * update or create upstream |
||
74 | * |
||
75 | * @param \Unikorp\KongAdminApi\Document\Upstream $document |
||
76 | * |
||
77 | * @return \Psr\Http\Message\ResponseInterface |
||
78 | */ |
||
79 | 1 | public function updateOrCreateUpstream(Document $document): ResponseInterface |
|
83 | |||
84 | /** |
||
85 | * delete upstream |
||
86 | * |
||
87 | * @param string $nameOrId |
||
88 | * |
||
89 | * @return \Psr\Http\Message\ResponseInterface |
||
90 | */ |
||
91 | 1 | public function deleteUpstream(string $nameOrId): ResponseInterface |
|
95 | } |
||
96 |
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.