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 |
||
17 | class User extends \Asymptix\db\DBTimedObject { |
||
18 | const STATUS_ACTIVATED = 1; |
||
19 | const STATUS_DEACTIVATED = 0; |
||
20 | |||
21 | const LOGIN_NOT_ACTIVATED = 0; |
||
22 | const LOGIN_INVALID_USERNAME = -1; |
||
23 | const LOGIN_INVALID_PASSWORD = -2; |
||
24 | |||
25 | const GENDER_NONE = 'none'; |
||
26 | const GENDER_MALE = 'male'; |
||
27 | const GENDER_FEMALE = 'female'; |
||
28 | |||
29 | const TABLE_NAME = "users"; |
||
30 | const ID_FIELD_NAME = "user_id"; |
||
31 | protected $fieldsList = array( |
||
32 | 'user_id' => 0, // int(10) unsigned NOT NULL AUTO_INCREMENT |
||
33 | 'username' => "", // varchar(255) NOT NULL |
||
|
|||
34 | 'email' => "", // varchar(255) NOT NULL |
||
35 | 'password' => "", // varchar(255) NOT NULL |
||
36 | 'auth_key' => "", // varchar(32) NOT NULL |
||
37 | 'role' => 0, // int(1) unsigned not null |
||
38 | |||
39 | 'full_name' => "", // VARCHAR(255) NOT NULL |
||
40 | 'gender' => self::GENDER_NONE, // ENUM( 'male', 'female', 'none', '' ) NOT NULL DEFAULT 'none' |
||
41 | 'language' => "en", // VARCHAR(2) NOT NULL DEFAULT 'en' |
||
42 | |||
43 | 'last_login_time' => "0000-00-00 00:00:00", // datetime DEFAULT NULL |
||
44 | 'create_time' => "0000-00-00 00:00:00", // datetime DEFAULT NULL |
||
45 | 'create_user_id' => 0, // int(11) DEFAULT NULL |
||
46 | 'update_time' => "0000-00-00 00:00:00", // datetime DEFAULT NULL |
||
47 | 'update_user_id' => 0, // int(11) DEFAULT NULL |
||
48 | |||
49 | 'activation' => 0, // tinyint(1) NOT NULL DEFAULT '0' |
||
50 | |||
51 | 'signature' => "", // TEXT NOT NULL |
||
52 | 'avatar' => "", // varchar(100) not null |
||
53 | |||
54 | // additional fields according to your database structure |
||
55 | ); |
||
56 | |||
57 | public function __construct() { |
||
60 | |||
61 | /** |
||
62 | * For user accounts we must verify if login field ID is unique. |
||
63 | */ |
||
64 | public function save($debug = false) { |
||
72 | |||
73 | /** |
||
74 | * Password encoding method. |
||
75 | * |
||
76 | * @param string $password Password |
||
77 | * @return string Encoded password string. |
||
78 | */ |
||
79 | public static function passwordEncode($password) { |
||
82 | |||
83 | /** |
||
84 | * Login functionality. |
||
85 | * |
||
86 | * @param string $login Username from login form. |
||
87 | * @param string $password Password from login form. |
||
88 | * |
||
89 | * @return mixed User object on success or integer result code if some problems occurred. |
||
90 | */ |
||
91 | public static function login($login, $password) { |
||
109 | |||
110 | /** |
||
111 | * Updates login time. |
||
112 | */ |
||
113 | public function updateLoginTime() { |
||
119 | |||
120 | /** |
||
121 | * Checks if user is logged in. |
||
122 | * |
||
123 | * @global User $_USER Current user object. |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public static function checkLoggedIn() { |
||
135 | |||
136 | /** |
||
137 | * Checks if account of the current user is equal to the needed account page. |
||
138 | * |
||
139 | * @global User $_USER Current user object. |
||
140 | * @param array $roles Needed roles. |
||
141 | * |
||
142 | * @return boolean |
||
143 | */ |
||
144 | public static function checkAccountAccess($roles = array()) { |
||
164 | |||
165 | /** |
||
166 | * Logout functionality. |
||
167 | */ |
||
168 | public static function logout() { |
||
174 | |||
175 | /** |
||
176 | * Returns current users avatar image file path. |
||
177 | * |
||
178 | * @param boolean $icon Return only default icon flag. |
||
179 | * |
||
180 | * @return string Image path. |
||
181 | */ |
||
182 | public function getAvatarPath($icon = false) { |
||
192 | |||
193 | /** |
||
194 | * Updates users avatar image file path in the DB. |
||
195 | * |
||
196 | * @param string $newAvatarFileName New filename. |
||
197 | * |
||
198 | * @return boolean Success flag. |
||
199 | */ |
||
200 | public function updateAvatar($newAvatarFileName) { |
||
223 | |||
224 | } |
||
225 |
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.