Conditions | 42 |
Paths | > 20000 |
Total Lines | 127 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | 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 | 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 | } |
||
332 |