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 |
||
18 | abstract class AbstractEncryption implements EncryptionInterface |
||
19 | { |
||
20 | // @codingStandardsIgnoreStart |
||
21 | const ENCRYPTION_PADDING = "\x28\xbf\x4e\x5e\x4e\x75\x8a\x41\x64\x00\x4e\x56\xff\xfa\x01\x08\x2e\x2e\x00\xb6\xd0\x68\x3e\x80\x2f\x0c\xa9\xfe\x64\x53\x69\x7a"; |
||
22 | // @codingStandardsIgnoreEnd |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $encryptionKey; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $userEntry; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $ownerEntry; |
||
38 | |||
39 | /** |
||
40 | * @var Permissions |
||
41 | */ |
||
42 | private $userPermissions; |
||
43 | |||
44 | /** |
||
45 | * @param string $permanentFileIdentifier |
||
46 | * @param string $userPassword |
||
47 | * @param string|null $ownerPassword |
||
48 | * @param Permissions|null $userPermissions |
||
49 | * @throws UnexpectedValueException |
||
50 | */ |
||
51 | public function __construct( |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function writeEncryptDictionary(PdfWriter $pdfWriter) |
||
137 | |||
138 | /** |
||
139 | * Adds additional entries to the encrypt dictionary if required. |
||
140 | * |
||
141 | * @param PdfWriter $pdfWriter |
||
142 | */ |
||
143 | protected function writeAdditionalEncryptDictionaryEntries(PdfWriter $pdfWriter) |
||
146 | |||
147 | /** |
||
148 | * Returns the revision number of the encryption. |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | abstract protected function getRevision(); |
||
153 | |||
154 | /** |
||
155 | * Returns the algorithm number of the encryption. |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | abstract protected function getAlgorithm(); |
||
160 | |||
161 | /** |
||
162 | * Returns the key length to be used. |
||
163 | * |
||
164 | * The returned value must be either 40 or 128. |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | abstract protected function getKeyLength(); |
||
169 | |||
170 | /** |
||
171 | * Computes an individual ecryption key for an object. |
||
172 | * |
||
173 | * @param string $objectNumber |
||
174 | * @param string $generationNumber |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function computeIndividualEncryptionKey($objectNumber, $generationNumber) |
||
185 | |||
186 | /** |
||
187 | * Encodes a given password into latin-1 and performs length check. |
||
188 | * |
||
189 | * @param string $password |
||
190 | * @return string |
||
191 | * @throws UnsupportedPasswordException |
||
192 | */ |
||
193 | private function encodePassword($password) |
||
215 | |||
216 | /** |
||
217 | * Computes the encryption key as defined by algorithm 3.2 in 3.5.2. |
||
218 | * |
||
219 | * @param string $password |
||
220 | * @param int $revision |
||
221 | * @param int $keyLength |
||
222 | * @param string $ownerEntry |
||
223 | * @param int $permissions |
||
224 | * @param string $idEntry |
||
225 | * @param bool $encryptMetadata |
||
226 | * @return string |
||
227 | */ |
||
228 | View Code Duplication | private function computeEncryptionKey( |
|
258 | |||
259 | /** |
||
260 | * Computes the owner entry as defined by algorithm 3.3 in 3.5.2. |
||
261 | * |
||
262 | * @param string $ownerPassword |
||
263 | * @param string $userPassword |
||
264 | * @param int $revision |
||
265 | * @param int $keyLength |
||
266 | * @return string |
||
267 | */ |
||
268 | View Code Duplication | private function computeOwnerEntry($ownerPassword, $userPassword, $revision, $keyLength) |
|
290 | |||
291 | /** |
||
292 | * Computes the user entry (rev 2) as defined by algorithm 3.4 in 3.5.2. |
||
293 | * |
||
294 | * @param string $userPassword |
||
295 | * @param string $ownerEntry |
||
296 | * @param int $userPermissionFlags |
||
297 | * @param string $idEntry |
||
298 | * @return string[] |
||
299 | */ |
||
300 | private function computeUserEntryRev2($userPassword, $ownerEntry, $userPermissionFlags, $idEntry) |
||
309 | |||
310 | /** |
||
311 | * Computes the user entry (rev 3 or greater) as defined by algorithm 3.5 in 3.5.2. |
||
312 | * |
||
313 | * @param string $userPassword |
||
314 | * @param int $revision |
||
315 | * @param int $keyLength |
||
316 | * @param string $ownerEntry |
||
317 | * @param int $permissions |
||
318 | * @param string $idEntry |
||
319 | * @return string[] |
||
320 | */ |
||
321 | private function computeUserEntryRev3OrGreater( |
||
349 | |||
350 | /** |
||
351 | * Applies loop RC4 encryption. |
||
352 | * |
||
353 | * @param string $value |
||
354 | * @param string $key |
||
355 | * @param int $keyLength |
||
356 | * @return string |
||
357 | */ |
||
358 | View Code Duplication | private function applyRc4Loop($value, $key, $keyLength) |
|
372 | } |
||
373 |
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.