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 |
||
| 34 | class CAT { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * which version is this? |
||
| 38 | * even if we are unreleased, keep track of internal version-to-be |
||
| 39 | * developers need to set this in code. The user-displayed string |
||
| 40 | * is generated into $VERSION below |
||
| 41 | */ |
||
| 42 | public static $VERSION_MAJOR = 1; |
||
| 43 | public static $VERSION_MINOR = 2; |
||
| 44 | public static $VERSION_PATCH = 0; |
||
| 45 | public static $VERSION_EXTRA = ""; |
||
| 46 | public static $RELEASE_VERSION = FALSE; |
||
| 47 | public static $USER_API_VERSION = 2; |
||
| 48 | |||
| 49 | /* |
||
| 50 | * This is the user-displayed string; controlled by the four options above |
||
| 51 | * It is generated in the constructor. |
||
| 52 | */ |
||
| 53 | |||
| 54 | public static $VERSION; |
||
| 55 | /** |
||
| 56 | /** |
||
| 57 | * database which this class queries by default |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private static $LANG = ''; |
||
| 62 | private static $DB_TYPE = "INST"; |
||
| 63 | /** |
||
| 64 | * Constructor sets the language by calling set_lang |
||
| 65 | * and stores language settings in object properties |
||
| 66 | * additionally it also sets static variables $laing_index and $root |
||
| 67 | */ |
||
| 68 | public function __construct() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | public function totalIdPs($level) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sets the gettext domain |
||
| 110 | * |
||
| 111 | * @param string $domain |
||
| 112 | * @return string previous seting so that you can restore it later |
||
| 113 | */ |
||
| 114 | public static function set_locale($domain) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * set_lang does all language setting magic |
||
| 125 | * checks if lang has been declared in the http call |
||
| 126 | * if not, checks for saved lang in the SESSION |
||
| 127 | * or finally checks browser properties. |
||
| 128 | * Only one of the supported langiages can be set |
||
| 129 | * if a match is not found, the default langiage is used |
||
| 130 | * @param $hardsetlang - this is currently not used but |
||
| 131 | * will allow to forst lang setting if this was ever required |
||
| 132 | */ |
||
| 133 | private static function set_lang($hardsetlang = 0) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * gets the language setting in CAT |
||
| 185 | */ |
||
| 186 | static public function get_lang() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Prepares a list of countries known to the CAT. |
||
| 194 | * |
||
| 195 | * @param int $active_only is set and nonzero will cause that only countries with some institutions underneath will be listed |
||
| 196 | * @return array Array indexed by (uppercase) lang codes and sorted according to the current locale |
||
| 197 | */ |
||
| 198 | public function printCountryList($active_only = 0) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Writes an audit log entry to the audit log file. These audits are semantic logs; they don't record every single modification |
||
| 220 | * in the database, but provide a logical "who did what" overview. The exact modification SQL statements are logged |
||
| 221 | * automatically with writeSQLAudit() instead. The log file path is configurable in _config.php. |
||
| 222 | * |
||
| 223 | * @param string $user persistent identifier of the user who triggered the action |
||
| 224 | * @param string $category type of modification, from the fixed vocabulary: "NEW", "OWN", "MOD", "DEL" |
||
| 225 | * @param string $message message to log into the audit log |
||
| 226 | * @return boolean TRUE if successful. Will terminate script execution on failure. |
||
| 227 | */ |
||
| 228 | public static function writeAudit($user, $category, $message) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Write an audit log entry to the SQL log file. Every SQL statement which is not a simple SELECT one will be written |
||
| 255 | * to the log file. The log file path is configurable in _config.php. |
||
| 256 | * |
||
| 257 | * @param string $query the SQL query to be logged |
||
| 258 | */ |
||
| 259 | public static function writeSQLAudit($query) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * stores the location of the root directory |
||
| 278 | * @static string $root |
||
| 279 | */ |
||
| 280 | public static $root; |
||
| 281 | |||
| 282 | /** |
||
| 283 | * language display name for the language set by the constructor |
||
| 284 | */ |
||
| 285 | public static $locale; |
||
| 286 | |||
| 287 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.