| Conditions | 10 |
| Paths | 52 |
| Total Lines | 45 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 13 | function defuseCryption($message, $ascii_key, $type) |
||
| 14 | { |
||
| 15 | // load PhpEncryption library |
||
| 16 | $path = '../includes/libraries/Encryption/Encryption/'; |
||
| 17 | |||
| 18 | include_once $path.'Crypto.php'; |
||
| 19 | include_once $path.'Encoding.php'; |
||
| 20 | include_once $path.'DerivedKeys.php'; |
||
| 21 | include_once $path.'Key.php'; |
||
| 22 | include_once $path.'KeyOrPassword.php'; |
||
| 23 | include_once $path.'File.php'; |
||
| 24 | include_once $path.'RuntimeTests.php'; |
||
| 25 | include_once $path.'KeyProtectedByPassword.php'; |
||
| 26 | include_once $path.'Core.php'; |
||
| 27 | |||
| 28 | // init |
||
| 29 | $err = ''; |
||
| 30 | if (empty($ascii_key) === true) { |
||
| 31 | $ascii_key = file_get_contents(SECUREPATH.'/teampass-seckey.txt'); |
||
| 32 | } |
||
| 33 | |||
| 34 | // convert KEY |
||
| 35 | $key = \Defuse\Crypto\Key::loadFromAsciiSafeString($ascii_key); |
||
| 36 | |||
| 37 | try { |
||
| 38 | if ($type === 'encrypt') { |
||
| 39 | $text = \Defuse\Crypto\Crypto::encrypt($message, $key); |
||
| 40 | } elseif ($type === 'decrypt') { |
||
| 41 | $text = \Defuse\Crypto\Crypto::decrypt($message, $key); |
||
| 42 | } |
||
| 43 | } catch (Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException $ex) { |
||
| 44 | $err = 'an attack! either the wrong key was loaded, or the ciphertext has changed since it was created either corrupted in the database or intentionally modified by someone trying to carry out an attack.'; |
||
| 45 | } catch (Defuse\Crypto\Exception\BadFormatException $ex) { |
||
| 46 | $err = $ex; |
||
| 47 | } catch (Defuse\Crypto\Exception\EnvironmentIsBrokenException $ex) { |
||
| 48 | $err = $ex; |
||
| 49 | } catch (Defuse\Crypto\Exception\CryptoException $ex) { |
||
| 50 | $err = $ex; |
||
| 51 | } catch (Defuse\Crypto\Exception\IOException $ex) { |
||
| 52 | $err = $ex; |
||
| 53 | } |
||
| 54 | |||
| 55 | return array( |
||
| 56 | 'string' => isset($text) ? $text : '', |
||
| 57 | 'error' => $err, |
||
| 58 | ); |
||
| 222 |