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 |
||
14 | class Facade |
||
15 | { |
||
16 | /** |
||
17 | * @var CredentialsInterface |
||
18 | */ |
||
19 | private static $credentials; |
||
20 | /** |
||
21 | * @var TransportInterface |
||
22 | */ |
||
23 | private static $transport; |
||
24 | /** |
||
25 | * @var PublicAPIClient |
||
26 | */ |
||
27 | private static $client; |
||
28 | /** |
||
29 | * @var LoggerInterface|null |
||
30 | */ |
||
31 | private static $logger; |
||
32 | |||
33 | /** |
||
34 | * Set logger |
||
35 | * |
||
36 | * @param LoggerInterface $logger |
||
37 | */ |
||
38 | public static function setLogger(LoggerInterface $logger) |
||
42 | |||
43 | /** |
||
44 | * Sets (or replaces) credentials |
||
45 | * |
||
46 | * @param CredentialsInterface $credentials |
||
47 | */ |
||
48 | View Code Duplication | public static function setCredentials(CredentialsInterface $credentials) |
|
55 | |||
56 | /** |
||
57 | * Sets (or replaces) transport |
||
58 | * |
||
59 | * @param TransportInterface $transport |
||
60 | */ |
||
61 | View Code Duplication | public static function setTransport(TransportInterface $transport) |
|
68 | |||
69 | /** |
||
70 | * Sends request to Covery and returns access level, associated with |
||
71 | * used credentials |
||
72 | * |
||
73 | * This method can be used for Covery health check and availability |
||
74 | * On any problem (network, credentials, server side) this method |
||
75 | * will throw an exception |
||
76 | * |
||
77 | * @return mixed |
||
78 | * @throws Exception |
||
79 | */ |
||
80 | public static function ping() |
||
88 | |||
89 | /** |
||
90 | * Sends envelope to Covery and returns it's ID on Covery side |
||
91 | * Before sending, validation is performed |
||
92 | * |
||
93 | * @param EnvelopeInterface $envelope |
||
94 | * @return int |
||
95 | * @throws Exception |
||
96 | */ |
||
97 | View Code Duplication | public static function sendEvent(EnvelopeInterface $envelope) |
|
105 | |||
106 | /** |
||
107 | * Sends envelope to Covery for analysis |
||
108 | * |
||
109 | * @param EnvelopeInterface $envelope |
||
110 | * @return Result |
||
111 | * @throws Exception |
||
112 | */ |
||
113 | View Code Duplication | public static function makeDecision(EnvelopeInterface $envelope) |
|
121 | } |
||
122 |
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.