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 namespace Limoncello\Crypt; |
||
25 | class SymmetricCrypt extends BaseCrypt implements EncryptInterface, DecryptInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $method; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $password; |
||
36 | |||
37 | /** |
||
38 | * Such as OPENSSL_RAW_DATA, OPENSSL_ZERO_PADDING. |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | private $options; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $initializationVector = null; |
||
48 | |||
49 | /** |
||
50 | * @param string $method |
||
51 | * @param string $password |
||
52 | */ |
||
53 | 3 | public function __construct($method, $password) |
|
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | 1 | View Code Duplication | public function decrypt($data) |
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | 3 | View Code Duplication | public function encrypt($data) |
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | 3 | public function getPassword() |
|
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | 3 | public function getMethod() |
|
117 | |||
118 | /** |
||
119 | * @param string $method |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | 3 | public function setMethod($method) |
|
131 | |||
132 | /** |
||
133 | * @param string $password |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | 3 | public function setPassword($password) |
|
145 | |||
146 | /** |
||
147 | * @param null|string $value |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | 1 | public function resetInitializationVector($value = null) |
|
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 3 | public function getInitializationVector() |
|
171 | |||
172 | /** |
||
173 | * @return $this |
||
174 | */ |
||
175 | 3 | public function asRaw() |
|
179 | |||
180 | /** |
||
181 | * @return $this |
||
182 | */ |
||
183 | 1 | public function asBase64() |
|
187 | |||
188 | /** |
||
189 | * @return $this |
||
190 | */ |
||
191 | 1 | public function withZeroPadding() |
|
195 | |||
196 | /** |
||
197 | * @return $this |
||
198 | */ |
||
199 | 3 | public function withoutZeroPadding() |
|
203 | |||
204 | /** |
||
205 | * @return int |
||
206 | */ |
||
207 | 3 | protected function getOptions() |
|
211 | |||
212 | /** |
||
213 | * @param int $options |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | 3 | protected function setOptions($options) |
|
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 2 | protected function generateInitializationVector() |
|
235 | |||
236 | /** |
||
237 | * @param int $option |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | 3 | View Code Duplication | protected function setOption($option) |
249 | |||
250 | /** |
||
251 | * @param int $option |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | 3 | View Code Duplication | protected function clearOption($option) |
263 | |||
264 | /** |
||
265 | * We need this wrapper for testing purposes so we can mock system call to Open SSL. |
||
266 | * |
||
267 | * @param string $data |
||
268 | * @param string $method |
||
269 | * @param string $password |
||
270 | * @param int $options |
||
271 | * @param string $initializationVector |
||
272 | * |
||
273 | * @return string|false |
||
274 | */ |
||
275 | 1 | protected function openSslDecrypt($data, $method, $password, $options, $initializationVector) |
|
279 | |||
280 | /** |
||
281 | * We need this wrapper for testing purposes so we can mock system call to Open SSL. |
||
282 | * |
||
283 | * @param string $data |
||
284 | * @param string $method |
||
285 | * @param string $password |
||
286 | * @param int $options |
||
287 | * @param string $initializationVector |
||
288 | * |
||
289 | * @return string|false |
||
290 | */ |
||
291 | 2 | protected function openSslEncrypt($data, $method, $password, $options, $initializationVector) |
|
295 | } |
||
296 |
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.