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 |
||
30 | class OpensslWrapper |
||
31 | { |
||
32 | /** |
||
33 | * OpenSSL encrypt wrapper function. |
||
34 | * |
||
35 | * @param string $data Data to decrypt |
||
36 | * @param OpensslKey $key Key string |
||
37 | * @param string $tag AAD tag |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | 28 | View Code Duplication | protected static function opensslEncrypt(string $data, OpensslKey $key, string &$tag): string |
51 | |||
52 | /** |
||
53 | * OpenSSL decrypt wrapper function. |
||
54 | * |
||
55 | * @param string $input Data to decrypt |
||
56 | * @param OpensslKey $key Key string |
||
57 | * @param string $tag AAD authentication tag |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 35 | View Code Duplication | protected static function opensslDecrypt(string $input, OpensslKey $key, string $tag): string |
71 | |||
72 | /** |
||
73 | * Get IV size for specified CIPHER. |
||
74 | * |
||
75 | * @param string $cipher Openssl cipher |
||
76 | * |
||
77 | * @return int |
||
78 | */ |
||
79 | 46 | protected static function ivSize(string $cipher): int |
|
83 | |||
84 | /** |
||
85 | * Get a correctly sized IV for the specified cipher. |
||
86 | * |
||
87 | * @param string $cipher Openssl cipher |
||
88 | * |
||
89 | * @throws \Exception |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 37 | protected static function ivGenerate(string $cipher): string |
|
103 | |||
104 | /** |
||
105 | * Determines if the provided cipher requires a tag. |
||
106 | * |
||
107 | * @param string $cipher Openssl cipher |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | 36 | protected static function tagLength(string $cipher): int |
|
115 | } |
||
116 |
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.