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 |
||
| 18 | View Code Duplication | class Jetpack_Constants { |
|
|
|
|||
| 19 | static $set_constants = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Checks if a "constant" has been set in Jetpack_Constants, and if not, |
||
| 23 | * checks if the constant was defined with define( 'name', 'value ). |
||
| 24 | * |
||
| 25 | * @param $name string The name of the constant |
||
| 26 | * |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | public static function is_defined( $name ) { |
||
| 34 | /** |
||
| 35 | * Checks if a "constant" has been set in Jetpack_Constants |
||
| 36 | * and has the value of true |
||
| 37 | * |
||
| 38 | * @param $name string The name of the constant |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | public static function is_true( $name ) { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Attempts to retrieve the "constant" from Jetpack_Constants, and if it hasn't been set, |
||
| 48 | * then attempts to get the constant with the constant() function. |
||
| 49 | * |
||
| 50 | * @param $name |
||
| 51 | * |
||
| 52 | * @return mixed null if the constant does not exist or the value of the constant. |
||
| 53 | */ |
||
| 54 | public static function get_constant( $name ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the value of the "constant" within Jetpack_Constants. |
||
| 64 | * |
||
| 65 | * @param $name string The name of the "constant" |
||
| 66 | * @param $value string The value of the "constant" |
||
| 67 | */ |
||
| 68 | public static function set_constant( $name, $value ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Will unset a "constant" from Jetpack_Constants if the constant exists. |
||
| 74 | * |
||
| 75 | * @param $name string The name of the "constant" |
||
| 76 | * |
||
| 77 | * @return bool Whether the constant was removed. |
||
| 78 | */ |
||
| 79 | public static function clear_single_constant( $name ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Resets all of the constants within Jetpack_Constants. |
||
| 90 | */ |
||
| 91 | public static function clear_constants() { |
||
| 94 | } |
||
| 95 |
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.