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 |
||
25 | class Cache implements CacheInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var CacheAdapterInterface |
||
29 | */ |
||
30 | private $adapter; |
||
31 | |||
32 | /** |
||
33 | * @var FormatterInterface |
||
34 | */ |
||
35 | private $formatter; |
||
36 | |||
37 | /** |
||
38 | * @var int|null |
||
39 | */ |
||
40 | private $lifetime; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $cacheException; |
||
46 | |||
47 | /** |
||
48 | * @param CacheAdapterInterface $adapter |
||
49 | * @param FormatterInterface|null $formatter |
||
50 | * @param int|null $lifetime |
||
51 | * @param bool $cacheException |
||
52 | */ |
||
53 | 135 | public function __construct( |
|
54 | CacheAdapterInterface $adapter, |
||
55 | FormatterInterface $formatter = null, |
||
56 | $lifetime = null, |
||
57 | $cacheException = true |
||
58 | ) { |
||
59 | 135 | $this->setAdapter($adapter); |
|
60 | 135 | $this->setFormatter($formatter ?: new Formatter()); |
|
61 | 135 | $this->setlifetime($lifetime); |
|
62 | 135 | $this->cacheException($cacheException); |
|
63 | 135 | } |
|
64 | |||
65 | /** |
||
66 | * @return CacheAdapterInterface |
||
67 | */ |
||
68 | 18 | public function getAdapter() |
|
69 | { |
||
70 | 18 | return $this->adapter; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param CacheAdapterInterface $adapter |
||
75 | */ |
||
76 | 135 | public function setAdapter(CacheAdapterInterface $adapter) |
|
80 | |||
81 | /** |
||
82 | * @return FormatterInterface |
||
83 | */ |
||
84 | 18 | public function getFormatter() |
|
88 | |||
89 | /** |
||
90 | * @param FormatterInterface $formatter |
||
91 | */ |
||
92 | 135 | public function setFormatter(FormatterInterface $formatter) |
|
96 | |||
97 | /** |
||
98 | * @return int|null |
||
99 | */ |
||
100 | 18 | public function getLifetime() |
|
104 | |||
105 | /** |
||
106 | * @param int|null $lifetime |
||
107 | */ |
||
108 | 135 | public function setLifetime($lifetime = null) |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 135 | public function cacheException($cacheException = null) |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 18 | View Code Duplication | public function getResponse(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory) |
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 27 | View Code Duplication | public function getException(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory) |
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 18 | public function saveResponse(ResponseInterface $response, InternalRequestInterface $internalRequest) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 27 | public function saveException(HttpAdapterException $exception, InternalRequestInterface $internalRequest) |
|
173 | |||
174 | /** |
||
175 | * @param InternalRequestInterface $internalRequest |
||
176 | * @param string $context |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 72 | private function getIdentifier(InternalRequestInterface $internalRequest, $context) |
|
184 | |||
185 | /** |
||
186 | * @param InternalRequestInterface $internalRequest |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | 72 | private function serializeInternalRequest(InternalRequestInterface $internalRequest) |
|
197 | |||
198 | /** |
||
199 | * @param ResponseInterface $response |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 9 | private function serializeResponse(ResponseInterface $response) |
|
207 | |||
208 | /** |
||
209 | * @param HttpAdapterException $exception |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 9 | private function serializeException(HttpAdapterException $exception) |
|
217 | |||
218 | /** |
||
219 | * @param string $serialized |
||
220 | * @param MessageFactoryInterface $messageFactory |
||
221 | * |
||
222 | * @return ResponseInterface |
||
223 | */ |
||
224 | 9 | private function unserializeResponse($serialized, MessageFactoryInterface $messageFactory) |
|
228 | |||
229 | /** |
||
230 | * @param string $serialized |
||
231 | * |
||
232 | * @return HttpAdapterException |
||
233 | */ |
||
234 | 9 | private function unserializeException($serialized) |
|
238 | |||
239 | /** |
||
240 | * @param array $unserialized |
||
241 | * @param MessageFactoryInterface $messageFactory |
||
242 | * |
||
243 | * @return ResponseInterface |
||
244 | */ |
||
245 | 9 | private function createResponse(array $unserialized, MessageFactoryInterface $messageFactory) |
|
255 | |||
256 | /** |
||
257 | * @param array $unserialized |
||
258 | * |
||
259 | * @return HttpAdapterException |
||
260 | */ |
||
261 | 9 | private function createException(array $unserialized) |
|
265 | |||
266 | /** |
||
267 | * @param array $data |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | 72 | private function serialize(array $data) |
|
275 | |||
276 | /** |
||
277 | * @param string $data |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | 18 | private function unserialize($data) |
|
285 | } |
||
286 |
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.