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 |
||
19 | class Response implements ResponseInterface |
||
20 | { |
||
21 | const FORMAT_RAW = 'raw'; |
||
22 | const FORMAT_ARRAY = 'array'; |
||
23 | |||
24 | /** |
||
25 | * @var ResponseInterface |
||
26 | */ |
||
27 | private $response; |
||
28 | /** |
||
29 | * @var |
||
30 | */ |
||
31 | private $returnType; |
||
32 | /** |
||
33 | * @var SerializerInterface |
||
34 | */ |
||
35 | private $serializer; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $context; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param ResponseInterface $response |
||
45 | * @param string $returnType |
||
46 | * @param SerializerInterface $serializer |
||
47 | * @param array $context |
||
48 | */ |
||
49 | public function __construct( |
||
60 | |||
61 | /** |
||
62 | * Get the body specified by the Returns annotation |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | public function body() |
||
84 | |||
85 | /** |
||
86 | * Build the deserialization context |
||
87 | */ |
||
88 | private function createContext() |
||
123 | |||
124 | /** |
||
125 | * Retrieves the HTTP protocol version as a string. |
||
126 | * |
||
127 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
128 | * |
||
129 | * @return string HTTP protocol version. |
||
130 | */ |
||
131 | public function getProtocolVersion() |
||
135 | |||
136 | /** |
||
137 | * Return an instance with the specified HTTP protocol version. |
||
138 | * |
||
139 | * The version string MUST contain only the HTTP version number (e.g., |
||
140 | * "1.1", "1.0"). |
||
141 | * |
||
142 | * This method MUST be implemented in such a way as to retain the |
||
143 | * immutability of the message, and MUST return an instance that has the |
||
144 | * new protocol version. |
||
145 | * |
||
146 | * @param string $version HTTP protocol version |
||
147 | * @return self |
||
148 | */ |
||
149 | public function withProtocolVersion($version) |
||
153 | |||
154 | /** |
||
155 | * Retrieves all message header values. |
||
156 | * |
||
157 | * The keys represent the header name as it will be sent over the wire, and |
||
158 | * each value is an array of strings associated with the header. |
||
159 | * |
||
160 | * // Represent the headers as a string |
||
161 | * foreach ($message->getHeaders() as $name => $values) { |
||
162 | * echo $name . ": " . implode(", ", $values); |
||
163 | * } |
||
164 | * |
||
165 | * // Emit headers iteratively: |
||
166 | * foreach ($message->getHeaders() as $name => $values) { |
||
167 | * foreach ($values as $value) { |
||
168 | * header(sprintf('%s: %s', $name, $value), false); |
||
169 | * } |
||
170 | * } |
||
171 | * |
||
172 | * While header names are not case-sensitive, getHeaders() will preserve the |
||
173 | * exact case in which headers were originally specified. |
||
174 | * |
||
175 | * @return array Returns an associative array of the message's headers. Each |
||
176 | * key MUST be a header name, and each value MUST be an array of strings |
||
177 | * for that header. |
||
178 | */ |
||
179 | public function getHeaders() |
||
183 | |||
184 | /** |
||
185 | * Checks if a header exists by the given case-insensitive name. |
||
186 | * |
||
187 | * @param string $name Case-insensitive header field name. |
||
188 | * @return bool Returns true if any header names match the given header |
||
189 | * name using a case-insensitive string comparison. Returns false if |
||
190 | * no matching header name is found in the message. |
||
191 | */ |
||
192 | public function hasHeader($name) |
||
196 | |||
197 | /** |
||
198 | * Retrieves a message header value by the given case-insensitive name. |
||
199 | * |
||
200 | * This method returns an array of all the header values of the given |
||
201 | * case-insensitive header name. |
||
202 | * |
||
203 | * If the header does not appear in the message, this method MUST return an |
||
204 | * empty array. |
||
205 | * |
||
206 | * @param string $name Case-insensitive header field name. |
||
207 | * @return string[] An array of string values as provided for the given |
||
208 | * header. If the header does not appear in the message, this method MUST |
||
209 | * return an empty array. |
||
210 | */ |
||
211 | public function getHeader($name) |
||
215 | |||
216 | /** |
||
217 | * Retrieves a comma-separated string of the values for a single header. |
||
218 | * |
||
219 | * This method returns all of the header values of the given |
||
220 | * case-insensitive header name as a string concatenated together using |
||
221 | * a comma. |
||
222 | * |
||
223 | * NOTE: Not all header values may be appropriately represented using |
||
224 | * comma concatenation. For such headers, use getHeader() instead |
||
225 | * and supply your own delimiter when concatenating. |
||
226 | * |
||
227 | * If the header does not appear in the message, this method MUST return |
||
228 | * an empty string. |
||
229 | * |
||
230 | * @param string $name Case-insensitive header field name. |
||
231 | * @return string A string of values as provided for the given header |
||
232 | * concatenated together using a comma. If the header does not appear in |
||
233 | * the message, this method MUST return an empty string. |
||
234 | */ |
||
235 | public function getHeaderLine($name) |
||
239 | |||
240 | /** |
||
241 | * Return an instance with the provided value replacing the specified header. |
||
242 | * |
||
243 | * While header names are case-insensitive, the casing of the header will |
||
244 | * be preserved by this function, and returned from getHeaders(). |
||
245 | * |
||
246 | * This method MUST be implemented in such a way as to retain the |
||
247 | * immutability of the message, and MUST return an instance that has the |
||
248 | * new and/or updated header and value. |
||
249 | * |
||
250 | * @param string $name Case-insensitive header field name. |
||
251 | * @param string|string[] $value Header value(s). |
||
252 | * @return self |
||
253 | * @throws \InvalidArgumentException for invalid header names or values. |
||
254 | */ |
||
255 | public function withHeader($name, $value) |
||
259 | |||
260 | /** |
||
261 | * Return an instance with the specified header appended with the given value. |
||
262 | * |
||
263 | * Existing values for the specified header will be maintained. The new |
||
264 | * value(s) will be appended to the existing list. If the header did not |
||
265 | * exist previously, it will be added. |
||
266 | * |
||
267 | * This method MUST be implemented in such a way as to retain the |
||
268 | * immutability of the message, and MUST return an instance that has the |
||
269 | * new header and/or value. |
||
270 | * |
||
271 | * @param string $name Case-insensitive header field name to add. |
||
272 | * @param string|string[] $value Header value(s). |
||
273 | * @return self |
||
274 | * @throws \InvalidArgumentException for invalid header names or values. |
||
275 | */ |
||
276 | public function withAddedHeader($name, $value) |
||
280 | |||
281 | /** |
||
282 | * Return an instance without the specified header. |
||
283 | * |
||
284 | * Header resolution MUST be done without case-sensitivity. |
||
285 | * |
||
286 | * This method MUST be implemented in such a way as to retain the |
||
287 | * immutability of the message, and MUST return an instance that removes |
||
288 | * the named header. |
||
289 | * |
||
290 | * @param string $name Case-insensitive header field name to remove. |
||
291 | * @return self |
||
292 | */ |
||
293 | public function withoutHeader($name) |
||
297 | |||
298 | /** |
||
299 | * Gets the body of the message. |
||
300 | * |
||
301 | * @return StreamInterface Returns the body as a stream. |
||
302 | */ |
||
303 | public function getBody() |
||
307 | |||
308 | /** |
||
309 | * Return an instance with the specified message body. |
||
310 | * |
||
311 | * The body MUST be a StreamInterface object. |
||
312 | * |
||
313 | * This method MUST be implemented in such a way as to retain the |
||
314 | * immutability of the message, and MUST return a new instance that has the |
||
315 | * new body stream. |
||
316 | * |
||
317 | * @param StreamInterface $body Body. |
||
318 | * @return self |
||
319 | * @throws \InvalidArgumentException When the body is not valid. |
||
320 | */ |
||
321 | public function withBody(StreamInterface $body) |
||
325 | |||
326 | /** |
||
327 | * Gets the response status code. |
||
328 | * |
||
329 | * The status code is a 3-digit integer result code of the server's attempt |
||
330 | * to understand and satisfy the request. |
||
331 | * |
||
332 | * @return int Status code. |
||
333 | */ |
||
334 | public function getStatusCode() |
||
338 | |||
339 | /** |
||
340 | * Return an instance with the specified status code and, optionally, reason phrase. |
||
341 | * |
||
342 | * If no reason phrase is specified, implementations MAY choose to default |
||
343 | * to the RFC 7231 or IANA recommended reason phrase for the response's |
||
344 | * status code. |
||
345 | * |
||
346 | * This method MUST be implemented in such a way as to retain the |
||
347 | * immutability of the message, and MUST return an instance that has the |
||
348 | * updated status and reason phrase. |
||
349 | * |
||
350 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||
351 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||
352 | * @param int $code The 3-digit integer result code to set. |
||
353 | * @param string $reasonPhrase The reason phrase to use with the |
||
354 | * provided status code; if none is provided, implementations MAY |
||
355 | * use the defaults as suggested in the HTTP specification. |
||
356 | * @return self |
||
357 | * @throws \InvalidArgumentException For invalid status code arguments. |
||
358 | */ |
||
359 | public function withStatus($code, $reasonPhrase = '') |
||
363 | |||
364 | /** |
||
365 | * Gets the response reason phrase associated with the status code. |
||
366 | * |
||
367 | * Because a reason phrase is not a required element in a response |
||
368 | * status line, the reason phrase value MAY be null. Implementations MAY |
||
369 | * choose to return the default RFC 7231 recommended reason phrase (or those |
||
370 | * listed in the IANA HTTP Status Code Registry) for the response's |
||
371 | * status code. |
||
372 | * |
||
373 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||
374 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||
375 | * @return string Reason phrase; must return an empty string if none present. |
||
376 | */ |
||
377 | public function getReasonPhrase() |
||
381 | } |
||
382 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.