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 |
||
39 | abstract class AbstractMcryptMixer extends AbstractMixer |
||
40 | { |
||
41 | /** |
||
42 | * mcrypt module resource |
||
43 | * |
||
44 | * @var resource |
||
45 | */ |
||
46 | private $mcrypt; |
||
47 | |||
48 | /** |
||
49 | * Block size of cipher |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | private $blockSize; |
||
54 | |||
55 | /** |
||
56 | * Cipher initialization vector |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $initv; |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public static function test() |
||
69 | |||
70 | /** |
||
71 | * Construct mcrypt mixer |
||
72 | */ |
||
73 | public function __construct() |
||
79 | |||
80 | /** |
||
81 | * Performs cleanup |
||
82 | */ |
||
83 | public function __destruct() |
||
89 | |||
90 | /** |
||
91 | * Fetch the cipher for mcrypt. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | abstract protected function getCipher(); |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | protected function getPartSize() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | protected function mixParts1($part1, $part2) |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | protected function mixParts2($part1, $part2) |
||
120 | |||
121 | /** |
||
122 | * Encrypts a block using the suppied key |
||
123 | * |
||
124 | * @param string $input Plaintext to encrypt |
||
125 | * @param string $key Encryption key |
||
126 | * |
||
127 | * @return string Resulting ciphertext |
||
128 | */ |
||
129 | View Code Duplication | private function encryptBlock($input, $key) |
|
141 | |||
142 | /** |
||
143 | * Derypts a block using the suppied key |
||
144 | * |
||
145 | * @param string $input Ciphertext to decrypt |
||
146 | * @param string $key Encryption key |
||
147 | * |
||
148 | * @return string Resulting plaintext |
||
149 | */ |
||
150 | View Code Duplication | private function decryptBlock($input, $key) |
|
162 | |||
163 | /** |
||
164 | * Sets up the mcrypt module |
||
165 | * |
||
166 | * @param string $key |
||
167 | * |
||
168 | * @return void |
||
169 | */ |
||
170 | private function prepareCipher($key) |
||
176 | } |
||
177 |
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.