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:
Complex classes like XoopsUserUtility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsUserUtility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class XoopsUserUtility |
||
24 | { |
||
25 | /** |
||
26 | * XoopsUserUtility::sendWelcome |
||
27 | * |
||
28 | * @param int|XoopsUser $user id or user object |
||
29 | * |
||
30 | * @return bool |
||
31 | */ |
||
32 | public static function sendWelcome($user) |
||
65 | |||
66 | /** |
||
67 | * XoopsUserUtility::validate |
||
68 | * |
||
69 | * @return false|string |
||
70 | */ |
||
71 | public static function validate() |
||
72 | { |
||
73 | $xoops = Xoops::getInstance(); |
||
74 | $args = func_get_args(); |
||
75 | $args_num = func_num_args(); |
||
76 | |||
77 | /* @var $user XoopsUser|null */ |
||
78 | $user = null; |
||
79 | $uname = null; |
||
80 | $email = null; |
||
81 | $pass = null; |
||
82 | $vpass = null; |
||
83 | |||
84 | switch ($args_num) { |
||
85 | case 1: |
||
86 | $user = $args[0]; |
||
87 | break; |
||
88 | case 2: |
||
89 | list ($uname, $email) = $args; |
||
90 | break; |
||
91 | case 3: |
||
92 | list ($user, $pass, $vpass) = $args; |
||
93 | break; |
||
94 | case 4: |
||
95 | list ($uname, $email, $pass, $vpass) = $args; |
||
96 | break; |
||
97 | default: |
||
98 | return false; |
||
99 | } |
||
100 | if (is_object($user)) { |
||
101 | $uname = $user->getVar('uname', 'n'); |
||
102 | $email = $user->getVar('email', 'n'); |
||
103 | } |
||
104 | |||
105 | //$user = empty($user) ? null : trim($user); |
||
106 | $uname = empty($uname) ? null : trim($uname); |
||
107 | $email = empty($email) ? null : trim($email); |
||
108 | $pass = empty($pass) ? null : trim($pass); |
||
109 | $vpass = empty($vpass) ? null : trim($vpass); |
||
110 | |||
111 | $xoops->getConfigs(); |
||
112 | |||
113 | $stop = ''; |
||
114 | // Invalid email address |
||
115 | if (!$xoops->checkEmail($email)) { |
||
|
|||
116 | $stop .= XoopsLocale::E_INVALID_EMAIL . '<br />'; |
||
117 | } |
||
118 | if (strrpos($email, ' ') > 0) { |
||
119 | $stop .= XoopsLocale::E_EMAIL_SHOULD_NOT_CONTAIN_SPACES . '<br />'; |
||
120 | } |
||
121 | // Check forbidden email address if current operator is not an administrator |
||
122 | if (!$xoops->userIsAdmin) { |
||
123 | $bad_emails = $xoops->getConfig('bad_emails'); |
||
124 | if (!empty($bad_emails)) { |
||
125 | foreach ($bad_emails as $be) { |
||
126 | if (!empty($be) && preg_match('/' . $be . '/i', $email)) { |
||
127 | $stop .= XoopsLocale::E_INVALID_EMAIL . '<br />'; |
||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | // $uname = XoopsLocale::trim($uname); |
||
134 | // no controls, puctuation, symbols, spaces or invisible separators |
||
135 | if (!preg_match('/^[^\p{C}\p{P}\p{S}\p{Z}]+$/u', $uname)) { |
||
136 | $stop .= XoopsLocale::E_INVALID_USERNAME . '<br />'; |
||
137 | } |
||
138 | // Check uname settings if current operator is not an administrator |
||
139 | if (!$xoops->userIsAdmin) { |
||
140 | $maxuname = $xoops->getConfig('maxuname'); |
||
141 | View Code Duplication | if (!empty($maxuname) && mb_strlen($uname) > $maxuname) { |
|
142 | $stop .= sprintf(XoopsLocale::EF_USERNAME_MUST_BE_LESS_THAN, $maxuname) . '<br />'; |
||
143 | } |
||
144 | $minuname = $xoops->getConfig('minuname'); |
||
145 | View Code Duplication | if (!empty($minuname) && mb_strlen($uname) < $minuname) { |
|
146 | $stop .= sprintf(XoopsLocale::EF_USERNAME_MUST_BE_MORE_THAN, $minuname) . '<br />'; |
||
147 | } |
||
148 | $bad_unames = $xoops->getConfig('bad_unames'); |
||
149 | if (!empty($bad_unames)) { |
||
150 | foreach ($bad_unames as $bu) { |
||
151 | if (!empty($bu) && preg_match('/' . $bu . '/i', $uname)) { |
||
152 | $stop .= XoopsLocale::E_NAME_IS_RESERVED . '<br />'; |
||
153 | break; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | // Check if uname/email already exists if the user is a new one |
||
159 | $uid = is_object($user) ? $user->getVar('uid') : 0; |
||
160 | |||
161 | $user_handler = $xoops->getHandlerUser(); |
||
162 | |||
163 | $criteria = new CriteriaCompo(new Criteria('uname', $uname)); |
||
164 | if ($uid > 0) { |
||
165 | $criteria->add(new Criteria('uid', $uid, '<>')); |
||
166 | } |
||
167 | $count = $user_handler->getCount($criteria); |
||
168 | if ($count > 0) { |
||
169 | $stop .= XoopsLocale::E_USERNAME_TAKEN . '<br />'; |
||
170 | } |
||
171 | |||
172 | $criteria = new CriteriaCompo(new Criteria('email', $email)); |
||
173 | if ($uid > 0) { |
||
174 | $criteria->add(new Criteria('uid', $uid, '<>')); |
||
175 | } |
||
176 | $count = $user_handler->getCount($criteria); |
||
177 | if ($count > 0) { |
||
178 | $stop .= XoopsLocale::E_EMAIL_TAKEN . '<br />'; |
||
179 | } |
||
180 | |||
181 | // If password is not set, skip password validation |
||
182 | if ($pass === null && $vpass === null) { |
||
183 | return $stop; |
||
184 | } |
||
185 | |||
186 | if (empty($pass) || empty($vpass)) { |
||
187 | $stop .= XoopsLocale::E_MUST_PROVIDE_PASSWORD . '<br />'; |
||
188 | } |
||
189 | if (isset($pass) && isset($vpass) && ($pass != $vpass)) { |
||
190 | $stop .= XoopsLocale::E_PASSWORDS_MUST_MATCH . '<br />'; |
||
191 | } else { |
||
192 | $minpass = $xoops->getConfig('minpass'); |
||
193 | if (($pass != '') && (!empty($minpass)) && (mb_strlen($pass) < $minpass)) { |
||
194 | $stop .= sprintf(XoopsLocale::EF_PASSWORD_MUST_BE_GREATER_THAN, $minpass) . '<br />'; |
||
195 | } |
||
196 | } |
||
197 | return $stop; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get client IP |
||
202 | * |
||
203 | * Adapted from PMA_getIp() [phpmyadmin project] |
||
204 | * |
||
205 | * @param bool $asString requiring integer or dotted string |
||
206 | * |
||
207 | * @return mixed string or integer value for the IP |
||
208 | */ |
||
209 | public static function getIP($asString = false) |
||
250 | |||
251 | /** |
||
252 | * XoopsUserUtility::getUnameFromIds() |
||
253 | * |
||
254 | * @param array $uids array of int ids |
||
255 | * @param bool $usereal use real names if true |
||
256 | * @param bool $linked show names as link to userinfo.php |
||
257 | * |
||
258 | * @return array of strings, names or links |
||
259 | */ |
||
260 | public static function getUnameFromIds($uids, $usereal = false, $linked = false) |
||
295 | |||
296 | /** |
||
297 | * XoopsUserUtility::getUnameFromId() |
||
298 | * |
||
299 | * @param int $userid id of user |
||
300 | * @param bool $usereal use real name if true |
||
301 | * @param bool $linked show username as link to userinfo.php |
||
302 | * |
||
303 | * @return string name or link |
||
304 | */ |
||
305 | public static function getUnameFromId($userid, $usereal = false, $linked = false) |
||
331 | } |
||
332 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: