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 |
||
| 10 | class Encryption{ |
||
|
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * Cipher algorithm |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | const CIPHER = 'aes-256-cbc'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Hash function |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | const HASH_FUNCTION = 'sha256'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * constructor for Encryption object. |
||
| 28 | * |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | private function __construct(){} |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Encrypt an id. |
||
| 35 | * |
||
| 36 | * @access public |
||
| 37 | * @static static method |
||
| 38 | * @param integer|string $id |
||
| 39 | * @return string |
||
| 40 | * @see http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/ |
||
| 41 | */ |
||
| 42 | public static function encryptId($id){ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Decryption for Id. |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * @static static method |
||
| 64 | * @param string $id |
||
| 65 | * @return integer |
||
| 66 | * @throws Exception if $id is empty |
||
| 67 | */ |
||
| 68 | public static function decryptId($id){ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Decryption for Ids with dash '-', Example: "feed-km1chg3" |
||
| 89 | * |
||
| 90 | * @access public |
||
| 91 | * @static static method |
||
| 92 | * @param string $id |
||
| 93 | * @return integer |
||
| 94 | * @throws Exception if $id is empty |
||
| 95 | */ |
||
| 96 | public static function decryptIdWithDash($id){ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * get characters that will be used in encryption/decryption provided by a key |
||
| 119 | * |
||
| 120 | * @access private |
||
| 121 | * @static static method |
||
| 122 | * @return string |
||
| 123 | * @throws Exception if $id is empty |
||
| 124 | */ |
||
| 125 | private static function getCharacters(){ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Encrypt a string. |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * @static static method |
||
| 152 | * @param string $plain |
||
| 153 | * @return string |
||
| 154 | * @throws Exception If functions don't exists |
||
| 155 | */ |
||
| 156 | public static function encrypt($plain){ |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Decrypted a string. |
||
| 185 | * |
||
| 186 | * @access public |
||
| 187 | * @static static method |
||
| 188 | * @param string $ciphertext |
||
| 189 | * @return string |
||
| 190 | * @throws Exception If $ciphertext is empty, or If functions don't exists |
||
| 191 | */ |
||
| 192 | public static function decrypt($ciphertext){ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * A timing attack resistant comparison. |
||
| 227 | * |
||
| 228 | * @access private |
||
| 229 | * @static static method |
||
| 230 | * @param string $hmac The hmac from the ciphertext being decrypted. |
||
| 231 | * @param string $compare The comparison hmac. |
||
| 232 | * @return bool |
||
| 233 | * @see https://github.com/sarciszewski/php-future/blob/bd6c91fb924b2b35a3e4f4074a642868bd051baf/src/Security.php#L36 |
||
| 234 | */ |
||
| 235 | private static function hashEquals($hmac, $compare){ |
||
| 258 | } |
||
| 259 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.