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 | final class BatchResult |
||
15 | { |
||
16 | /** |
||
17 | * @var \SplObjectStorage |
||
18 | */ |
||
19 | private $responses; |
||
20 | |||
21 | /** |
||
22 | * @var \SplObjectStorage |
||
23 | */ |
||
24 | private $exceptions; |
||
25 | |||
26 | 10 | public function __construct() |
|
31 | |||
32 | /** |
||
33 | * Checks if there are any successful responses at all. |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | 1 | public function hasResponses() |
|
41 | |||
42 | /** |
||
43 | * Returns all successful responses. |
||
44 | * |
||
45 | * @return ResponseInterface[] |
||
46 | */ |
||
47 | 2 | public function getResponses() |
|
57 | |||
58 | /** |
||
59 | * Checks if there is a successful response for a request. |
||
60 | * |
||
61 | * @param RequestInterface $request |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | 2 | public function isSuccessful(RequestInterface $request) |
|
69 | |||
70 | /** |
||
71 | * Returns the response for a successful request. |
||
72 | * |
||
73 | * @param RequestInterface $request |
||
74 | * |
||
75 | * @return ResponseInterface |
||
76 | * |
||
77 | * @throws \UnexpectedValueException If request was not part of the batch or failed |
||
78 | */ |
||
79 | 2 | View Code Duplication | public function getResponseFor(RequestInterface $request) |
87 | |||
88 | /** |
||
89 | * Adds a response in an immutable way. |
||
90 | * |
||
91 | * @param RequestInterface $request |
||
92 | * @param ResponseInterface $response |
||
93 | * |
||
94 | * @return BatchResult the new BatchResult with this request-response pair added to it |
||
95 | */ |
||
96 | 6 | public function addResponse(RequestInterface $request, ResponseInterface $response) |
|
103 | |||
104 | /** |
||
105 | * Checks if there are any unsuccessful requests at all. |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function hasExceptions() |
|
113 | |||
114 | /** |
||
115 | * Returns all exceptions for the unsuccessful requests. |
||
116 | * |
||
117 | * @return Exception[] |
||
118 | */ |
||
119 | public function getExceptions() |
||
129 | |||
130 | /** |
||
131 | * Checks if there is an exception for a request, meaning the request failed. |
||
132 | * |
||
133 | * @param RequestInterface $request |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 1 | public function isFailed(RequestInterface $request) |
|
141 | |||
142 | /** |
||
143 | * Returns the exception for a failed request. |
||
144 | * |
||
145 | * @param RequestInterface $request |
||
146 | * |
||
147 | * @return Exception |
||
148 | * |
||
149 | * @throws \UnexpectedValueException If request was not part of the batch or was successful |
||
150 | */ |
||
151 | 1 | View Code Duplication | public function getExceptionFor(RequestInterface $request) |
159 | |||
160 | /** |
||
161 | * Adds an exception in an immutable way. |
||
162 | * |
||
163 | * @param RequestInterface $request |
||
164 | * @param Exception $exception |
||
165 | * |
||
166 | * @return BatchResult the new BatchResult with this request-exception pair added to it |
||
167 | */ |
||
168 | 2 | public function addException(RequestInterface $request, Exception $exception) |
|
175 | |||
176 | 6 | public function __clone() |
|
181 | } |
||
182 |
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.