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 declare(strict_types=1); |
||
26 | class OpensslStack |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $stack = []; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $passkey; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | private $cost; |
||
42 | |||
43 | /** |
||
44 | * OpensslStack constructor. |
||
45 | * |
||
46 | * @param string $passkey Password or key |
||
47 | * @param int $cost Cost when using password mode |
||
48 | */ |
||
49 | public function __construct(string $passkey, int $cost = 0) |
||
55 | |||
56 | /** |
||
57 | * @param string $cipher |
||
58 | * @param string $algo |
||
59 | * @return OpensslStack |
||
60 | */ |
||
61 | public function add(string $cipher, string $algo): self |
||
67 | |||
68 | /** |
||
69 | * Encrypt data using custom stack |
||
70 | * |
||
71 | * @param string $data Data to encrypt |
||
72 | * @return string |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | View Code Duplication | public function encrypt(string $data): string |
|
83 | |||
84 | /** |
||
85 | * Decrypt data using custom stack |
||
86 | * |
||
87 | * @param string $data Data to decrypt |
||
88 | * @return string |
||
89 | * @throws \Exception |
||
90 | */ |
||
91 | View Code Duplication | public function decrypt(string $data): string |
|
99 | } |
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.