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 MultiRequestSentEvent extends AbstractEvent |
|
|
|||
24 | { |
||
25 | /** @var \Ivory\HttpAdapter\Message\ResponseInterface[] */ |
||
26 | private $responses; |
||
27 | |||
28 | /** @var \Ivory\HttpAdapter\HttpAdapterException[] */ |
||
29 | private $exceptions = array(); |
||
30 | |||
31 | /** |
||
32 | * Creates a multi request sent event. |
||
33 | * |
||
34 | * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. |
||
35 | * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses. |
||
36 | */ |
||
37 | public function __construct(HttpAdapterInterface $httpAdapter, array $responses) |
||
43 | |||
44 | /** |
||
45 | * Clears the responses. |
||
46 | */ |
||
47 | public function clearResponses() |
||
51 | |||
52 | /** |
||
53 | * Checks if there are responses. |
||
54 | * |
||
55 | * @return boolean TRUE if there are responses else FALSE. |
||
56 | */ |
||
57 | public function hasResponses() |
||
61 | |||
62 | /** |
||
63 | * Gets the responses. |
||
64 | * |
||
65 | * @return \Ivory\HttpAdapter\Message\ResponseInterface[] The responses. |
||
66 | */ |
||
67 | public function getResponses() |
||
71 | |||
72 | /** |
||
73 | * Sets the responses. |
||
74 | * |
||
75 | * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses. |
||
76 | */ |
||
77 | public function setResponses(array $responses) |
||
82 | |||
83 | /** |
||
84 | * Adds the responses. |
||
85 | * |
||
86 | * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses. |
||
87 | */ |
||
88 | public function addResponses(array $responses) |
||
94 | |||
95 | /** |
||
96 | * Removes the responses. |
||
97 | * |
||
98 | * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses. |
||
99 | */ |
||
100 | public function removeResponses(array $responses) |
||
106 | |||
107 | /** |
||
108 | * Checks if there is a response. |
||
109 | * |
||
110 | * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. |
||
111 | * |
||
112 | * @return boolean TRUE if there is the response else FALSE. |
||
113 | */ |
||
114 | public function hasResponse(ResponseInterface $response) |
||
118 | |||
119 | /** |
||
120 | * Adds a response. |
||
121 | * |
||
122 | * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. |
||
123 | */ |
||
124 | public function addResponse(ResponseInterface $response) |
||
128 | |||
129 | /** |
||
130 | * Removes a response. |
||
131 | * |
||
132 | * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. |
||
133 | */ |
||
134 | public function removeResponse(ResponseInterface $response) |
||
139 | |||
140 | /** |
||
141 | * Clears the exceptions. |
||
142 | */ |
||
143 | public function clearExceptions() |
||
147 | |||
148 | /** |
||
149 | * Checks if there are exceptions. |
||
150 | * |
||
151 | * @return boolean TRUE if there are exceptions else FALSE. |
||
152 | */ |
||
153 | public function hasExceptions() |
||
157 | |||
158 | /** |
||
159 | * Gets the exceptions. |
||
160 | * |
||
161 | * @return \Ivory\HttpAdapter\HttpAdapterException[] The exceptions. |
||
162 | */ |
||
163 | public function getExceptions() |
||
167 | |||
168 | /** |
||
169 | * Sets the exceptions. |
||
170 | * |
||
171 | * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions. |
||
172 | */ |
||
173 | public function setExceptions(array $exceptions) |
||
178 | |||
179 | /** |
||
180 | * Adds the exceptions. |
||
181 | * |
||
182 | * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions. |
||
183 | */ |
||
184 | public function addExceptions(array $exceptions) |
||
190 | |||
191 | /** |
||
192 | * Removes the exceptions. |
||
193 | * |
||
194 | * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions. |
||
195 | */ |
||
196 | public function removeExceptions(array $exceptions) |
||
202 | |||
203 | /** |
||
204 | * Checks if there is an exception. |
||
205 | * |
||
206 | * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. |
||
207 | * |
||
208 | * @return boolean TRUE if there is the exception else FALSE. |
||
209 | */ |
||
210 | public function hasException(HttpAdapterException $exception) |
||
214 | |||
215 | /** |
||
216 | * Adds an exception. |
||
217 | * |
||
218 | * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception |
||
219 | */ |
||
220 | public function addException(HttpAdapterException $exception) |
||
224 | |||
225 | /** |
||
226 | * Removes an exception. |
||
227 | * |
||
228 | * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. |
||
229 | */ |
||
230 | public function removeException(HttpAdapterException $exception) |
||
235 | } |
||
236 |
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.