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 |
||
| 14 | class Hash { |
||
| 15 | use Module; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Create ah hash for payload |
||
| 19 | * @param mixed $payload The payload string/object/array |
||
| 20 | * @param integer $method The hashing method, default is "md5" |
||
| 21 | * @return string The hash string |
||
| 22 | */ |
||
| 23 | public static function make($payload,$method='md5'){ |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Verify if given payload matches hash |
||
| 29 | * @param mixed $payload The payload string/object/array |
||
| 30 | * @param string $hash The hash string |
||
| 31 | * @param integer $method The hashing method |
||
| 32 | * @return bool Returns `true` if payload matches hash |
||
| 33 | */ |
||
| 34 | public static function verify($payload,$hash,$method='md5'){ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * List registered hashing algorithms |
||
| 40 | * |
||
| 41 | * @method methods |
||
| 42 | * |
||
| 43 | * @return array Array containing the list of supported hashing algorithms. |
||
| 44 | */ |
||
| 45 | public static function methods(){ |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Check if an alghoritm is registered in current PHP |
||
| 52 | * |
||
| 53 | * @method can |
||
| 54 | * |
||
| 55 | * @param string $algo The hashing algorithm name |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | public static function can($algo){ |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Static magic for creating hashes with a specified algorithm. |
||
| 65 | * |
||
| 66 | * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
||
| 67 | */ |
||
| 68 | public static function __callStatic($method,$params){ |
||
| 71 | |||
| 72 | public static function uuid($type=4, $namespace='', $name=''){ |
||
| 102 | |||
| 103 | |||
| 104 | } |
||
| 105 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.