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 |
||
| 11 | class Transform implements Processor |
||
| 12 | { |
||
| 13 | use Processor\Implementation; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Allowed transformation functions |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | public $functions; |
||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Class constructor |
||
| 24 | * |
||
| 25 | * @param string $property Property key with the processing instruction |
||
| 26 | */ |
||
| 27 | 20 | public function __construct($property) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Default transformation functions |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | 20 | public function getDefaultFunctions() |
|
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Apply processing to a single node |
||
| 65 | * |
||
| 66 | * @param Node $node |
||
| 67 | */ |
||
| 68 | 15 | public function applyToNode(Node $node) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Resolve instructions of nodes by getting their results |
||
| 114 | * |
||
| 115 | * @param Node $node |
||
| 116 | * |
||
| 117 | * @return mixed $result |
||
| 118 | */ |
||
| 119 | 14 | protected function resolveNodes($node) { |
|
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Generate a private key |
||
| 130 | * |
||
| 131 | * @param array $options |
||
| 132 | * @throws RuntimeException |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 1 | public function generate_private_key($options = []) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Generate a public based on existing private key |
||
| 152 | * |
||
| 153 | * @param string $privateKey |
||
| 154 | * @param string $passphrase |
||
| 155 | * @throws \RuntimeException |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 1 | public function generate_public_key($privateKey, $passphrase = null) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Encrypts data with private key |
||
| 175 | * |
||
| 176 | * @link http://php.net/manual/en/function.openssl-private-encrypt.php |
||
| 177 | * |
||
| 178 | * @param string $data |
||
| 179 | * @param string $key The private key |
||
| 180 | * @param string $passphrase |
||
| 181 | * @param int $padding |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | 1 | View Code Duplication | function private_encrypt($data, $key, $passphrase = null, $padding = OPENSSL_PKCS1_PADDING) |
| 204 | |||
| 205 | /** |
||
| 206 | * Decrypts data with public key, that was encrypted with private key |
||
| 207 | * |
||
| 208 | * @link http://php.net/manual/en/function.openssl-private-decrypt.php |
||
| 209 | * |
||
| 210 | * @param string $data |
||
| 211 | * @param string $key The public key |
||
| 212 | * @param string $passphrase |
||
| 213 | * @param int $padding |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 1 | function private_decrypt($data, $key, $padding = OPENSSL_PKCS1_PADDING) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Encrypts data with public key |
||
| 234 | * |
||
| 235 | * @link http://php.net/manual/en/function.openssl-public-encrypt.php |
||
| 236 | * |
||
| 237 | * @param string $data |
||
| 238 | * @param string $key The public key |
||
| 239 | * @param int $padding |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 1 | function public_encrypt($data, $key, $padding = OPENSSL_PKCS1_PADDING) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Generate signature based on private key |
||
| 258 | * |
||
| 259 | * @link http://php.net/manual/en/function.openssl-sign.php |
||
| 260 | * |
||
| 261 | * @param string $data |
||
| 262 | * @param string $key The private key |
||
| 263 | * @param string $passphrase |
||
| 264 | * @param int $signature_alg |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 1 | View Code Duplication | function generate_signature($data, $key, $passphrase = null, $signature_alg = OPENSSL_ALGO_SHA1) |
| 287 | |||
| 288 | /** |
||
| 289 | * Verify signature that was encrypted with private key through public key |
||
| 290 | * |
||
| 291 | * @link http://php.net/manual/en/function.openssl-verify.php |
||
| 292 | * |
||
| 293 | * @param string $data |
||
| 294 | * @param string $signature |
||
| 295 | * @param string $key The public key |
||
| 296 | * @param string $passphrase |
||
| 297 | * @param int $signature_alg |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 1 | function verify_signature($data, $signature, $key, $signature_alg = OPENSSL_ALGO_SHA1) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Check if input is valid base64 |
||
| 312 | * |
||
| 313 | * @param string $data |
||
| 314 | * |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | 2 | function is_base64($data) |
|
| 321 | } |
||
| 322 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: