Conditions | 4 |
Paths | 64 |
Total Lines | 72 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace Wubbajack\Tests; |
||
127 | public function testDecrypt() |
||
128 | { |
||
129 | $encryptedFile = $this->fileCrypt->encrypt($this->test_file, $this->test_encrypted_file); |
||
130 | |||
131 | $this->fileCrypt->decrypt($encryptedFile, $this->test_decrypted_file); |
||
132 | |||
133 | // Test if the decrypted file exists |
||
134 | $this->assertTrue(file_exists($this->test_decrypted_file)); |
||
135 | |||
136 | // Test if the checksum equals the original file |
||
137 | $this->assertEquals($encryptedFile->getChecksum(), sha1_file($this->test_decrypted_file)); |
||
138 | |||
139 | // Test if the decrypted file contains the same content as the original |
||
140 | $this->assertEquals(file_get_contents($this->test_file), file_get_contents($this->test_decrypted_file)); |
||
141 | |||
142 | // Test decryption of encrypted file with incorrect wrong IV |
||
143 | try { |
||
144 | $invalidEncryptedFile = EncryptedFile::create( |
||
145 | '2394qsf3-f9', |
||
146 | $encryptedFile->getChecksum(), |
||
147 | $encryptedFile->getPadding(), |
||
148 | $encryptedFile->getFile()->getRealPath() |
||
149 | ); |
||
150 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
151 | $this->fail('No exception was thrown on decrypting with an incorrect IV'); |
||
152 | } catch (\Exception $e) { |
||
153 | $this->assertInstanceOf( |
||
154 | DecryptException::class, |
||
155 | $e, |
||
156 | 'Expected an instance of DecryptException containing a message about IV, got '. get_class($e) |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | // Test decryption of encrypted file with incorrect checksum |
||
161 | try { |
||
162 | $invalidEncryptedFile = EncryptedFile::create( |
||
163 | $encryptedFile->getIV(), |
||
164 | bin2hex(openssl_random_pseudo_bytes(16)), |
||
165 | $encryptedFile->getPadding(), |
||
166 | $encryptedFile->getFile()->getRealPath() |
||
167 | ); |
||
168 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
169 | $this->fail('No exception was thrown on decrypting with unmatching checksums'); |
||
170 | } catch (\Exception $e) { |
||
171 | $this->assertInstanceOf( |
||
172 | DecryptException::class, |
||
173 | $e, |
||
174 | 'Expected an instance of DecryptException with a message about the checksum, got '. get_class($e) |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | // Test decryption of encrypted file with twice the amount of padding |
||
179 | try { |
||
180 | $invalidEncryptedFile = EncryptedFile::create( |
||
181 | $encryptedFile->getIV(), |
||
182 | $encryptedFile->getChecksum(), |
||
183 | $encryptedFile->getPadding() * 2, |
||
184 | $encryptedFile->getFile()->getRealPath() |
||
185 | ); |
||
186 | $this->fileCrypt->decrypt($invalidEncryptedFile, $this->test_decrypted_file); |
||
187 | $this->fail('No exception was thrown on decrypting with too much padding'); |
||
188 | } catch (\Exception $e) { |
||
189 | $this->assertInstanceOf( |
||
190 | DecryptException::class, |
||
191 | $e, |
||
192 | 'Expected an instance of DecryptException with a message about the checksum, got '. get_class($e) |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | // Test the decrypt method with an invalid variable type |
||
197 | $this->fileCrypt->decrypt($this->test_encrypted_file, $this->test_decrypted_file); |
||
198 | } |
||
199 | |||
254 |
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.