| Conditions | 78 |
| Paths | 260 |
| Total Lines | 221 |
| Code Lines | 165 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 43 | public function cleanVars(&$object) |
||
| 44 | { |
||
| 45 | $ts = MyTextSanitizer::getInstance(); |
||
| 46 | $errors = array(); |
||
| 47 | |||
| 48 | $vars = $object->getVars(); |
||
| 49 | $object->cleanVars = array(); |
||
| 50 | foreach ($vars as $k => $v) { |
||
| 51 | if (!$v['changed']) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | $cleanv = $v['value']; |
||
| 55 | switch ($v['data_type']) { |
||
| 56 | case XOBJ_DTYPE_TIMESTAMP: |
||
| 57 | $cleanv = !is_string($cleanv) && is_numeric($cleanv) ? date(_DBTIMESTAMPSTRING, $cleanv) : date(_DBTIMESTAMPSTRING, strtotime($cleanv)); |
||
| 58 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 59 | break; |
||
| 60 | case XOBJ_DTYPE_TIME: |
||
| 61 | $cleanv = !is_string($cleanv) && is_numeric($cleanv) ? date(_DBTIMESTRING, $cleanv) : date(_DBTIMESTRING, strtotime($cleanv)); |
||
| 62 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 63 | break; |
||
| 64 | case XOBJ_DTYPE_DATE: |
||
| 65 | $cleanv = !is_string($cleanv) && is_numeric($cleanv) ? date(_DBDATESTRING, $cleanv) : date(_DBDATESTRING, strtotime($cleanv)); |
||
| 66 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 67 | break; |
||
| 68 | case XOBJ_DTYPE_UNICODE_TXTBOX: |
||
| 69 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
| 70 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 71 | continue 2; |
||
| 72 | } |
||
| 73 | $cleanv = xoops_convert_encode($cleanv); |
||
| 74 | if (isset($v['maxlength']) && strlen($cleanv) > (int)$v['maxlength']) { |
||
| 75 | $errors[] = sprintf(_XOBJ_ERR_SHORTERTHAN, $k, (int)$v['maxlength']); |
||
| 76 | continue 2; |
||
| 77 | } |
||
| 78 | if (!$v['not_gpc']) { |
||
| 79 | $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv)); |
||
| 80 | } else { |
||
| 81 | $cleanv = $ts->censorString($cleanv); |
||
| 82 | } |
||
| 83 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 84 | break; |
||
| 85 | |||
| 86 | case XOBJ_DTYPE_UNICODE_TXTAREA: |
||
| 87 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
| 88 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 89 | continue 2; |
||
| 90 | } |
||
| 91 | $cleanv = xoops_convert_encode($cleanv); |
||
| 92 | if (!$v['not_gpc']) { |
||
| 93 | if (!empty($vars['dohtml']['value'])) { |
||
| 94 | $cleanv = $ts->textFilter($cleanv); |
||
| 95 | } |
||
| 96 | $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv)); |
||
| 97 | } else { |
||
| 98 | $cleanv = $ts->censorString($cleanv); |
||
| 99 | } |
||
| 100 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 101 | break; |
||
| 102 | |||
| 103 | case XOBJ_DTYPE_TXTBOX: |
||
| 104 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
| 105 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 106 | continue 2; |
||
| 107 | } |
||
| 108 | if (isset($v['maxlength']) && strlen($cleanv) > (int)$v['maxlength']) { |
||
| 109 | $errors[] = sprintf(_XOBJ_ERR_SHORTERTHAN, $k, (int)$v['maxlength']); |
||
| 110 | continue 2; |
||
| 111 | } |
||
| 112 | if (!$v['not_gpc']) { |
||
| 113 | $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv)); |
||
| 114 | } else { |
||
| 115 | $cleanv = $ts->censorString($cleanv); |
||
| 116 | } |
||
| 117 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 118 | break; |
||
| 119 | |||
| 120 | case XOBJ_DTYPE_TXTAREA: |
||
| 121 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
| 122 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 123 | continue 2; |
||
| 124 | } |
||
| 125 | if (!$v['not_gpc']) { |
||
| 126 | if (!empty($vars['dohtml']['value'])) { |
||
| 127 | $cleanv = $ts->textFilter($cleanv); |
||
| 128 | } |
||
| 129 | $cleanv = $ts->stripSlashesGPC($ts->censorString($cleanv)); |
||
| 130 | } else { |
||
| 131 | $cleanv = $ts->censorString($cleanv); |
||
| 132 | } |
||
| 133 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 134 | break; |
||
| 135 | |||
| 136 | case XOBJ_DTYPE_SOURCE: |
||
| 137 | $cleanv = trim($cleanv); |
||
| 138 | if (!$v['not_gpc']) { |
||
| 139 | $cleanv = $ts->stripSlashesGPC($cleanv); |
||
| 140 | } else { |
||
| 141 | $cleanv = $cleanv; |
||
| 142 | } |
||
| 143 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 144 | break; |
||
| 145 | // Should not be used! |
||
| 146 | case XOBJ_DTYPE_UNICODE_EMAIL: |
||
| 147 | $cleanv = trim($cleanv); |
||
| 148 | if ($v['required'] && $cleanv == '') { |
||
| 149 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 150 | continue 2; |
||
| 151 | } |
||
| 152 | if (!$v['not_gpc']) { |
||
| 153 | $cleanv = $ts->stripSlashesGPC($cleanv); |
||
| 154 | } |
||
| 155 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
| 156 | break; |
||
| 157 | |||
| 158 | case XOBJ_DTYPE_EMAIL: |
||
| 159 | $cleanv = trim($cleanv); |
||
| 160 | if ($v['required'] && $cleanv == '') { |
||
| 161 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 162 | continue 2; |
||
| 163 | } |
||
| 164 | if ($cleanv != '' && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $cleanv)) { |
||
| 165 | $errors[] = 'Invalid Email'; |
||
| 166 | continue 2; |
||
| 167 | } |
||
| 168 | if (!$v['not_gpc']) { |
||
| 169 | $cleanv = $ts->stripSlashesGPC($cleanv); |
||
| 170 | } |
||
| 171 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 172 | break; |
||
| 173 | |||
| 174 | // Should not be used! |
||
| 175 | case XOBJ_DTYPE_UNICODE_URL: |
||
| 176 | $cleanv = trim($cleanv); |
||
| 177 | if ($v['required'] && $cleanv == '') { |
||
| 178 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 179 | continue 2; |
||
| 180 | } |
||
| 181 | if ($cleanv != '' && !preg_match("/^http[s]*:\/\//i", $cleanv)) { |
||
| 182 | $cleanv = XOOPS_PROT . $cleanv; |
||
| 183 | } |
||
| 184 | if (!$v['not_gpc']) { |
||
| 185 | $cleanv = $ts->stripSlashesGPC($cleanv); |
||
| 186 | } |
||
| 187 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
| 188 | break; |
||
| 189 | case XOBJ_DTYPE_URL: |
||
| 190 | $cleanv = trim($cleanv); |
||
| 191 | if ($v['required'] && $cleanv == '') { |
||
| 192 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
| 193 | continue 2; |
||
| 194 | } |
||
| 195 | if ($cleanv != '' && !preg_match("/^http[s]*:\/\//i", $cleanv)) { |
||
| 196 | $cleanv = XOOPS_PROT . $cleanv; |
||
| 197 | } |
||
| 198 | if (!$v['not_gpc']) { |
||
| 199 | $cleanv = $ts->stripSlashesGPC($cleanv); |
||
| 200 | } |
||
| 201 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 202 | break; |
||
| 203 | |||
| 204 | // Should not be used! |
||
| 205 | case XOBJ_DTYPE_UNICODE_OTHER: |
||
| 206 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
| 207 | break; |
||
| 208 | |||
| 209 | case XOBJ_DTYPE_OTHER: |
||
| 210 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 211 | break; |
||
| 212 | |||
| 213 | case XOBJ_DTYPE_INT: |
||
| 214 | $cleanv = (int)$cleanv; |
||
| 215 | break; |
||
| 216 | |||
| 217 | case XOBJ_DTYPE_FLOAT: |
||
| 218 | $cleanv = (float)$cleanv; |
||
| 219 | break; |
||
| 220 | |||
| 221 | case XOBJ_DTYPE_DECIMAL: |
||
| 222 | $cleanv = (float)$cleanv; |
||
| 223 | break; |
||
| 224 | |||
| 225 | // Should not be used! |
||
| 226 | case XOBJ_DTYPE_UNICODE_ARRAY: |
||
| 227 | if (!$v['not_gpc']) { |
||
| 228 | $cleanv = array_map(array(&$ts, 'stripSlashesGPC'), $cleanv); |
||
| 229 | } |
||
| 230 | foreach (array_keys($cleanv) as $key) { |
||
| 231 | $cleanv[$key] = str_replace('\\"', '"', addslashes($cleanv[$key])); |
||
| 232 | } |
||
| 233 | // TODO: Not encoding safe, should try base64_encode -- phppp |
||
| 234 | $cleanv = "'" . serialize(array_walk($cleanv, 'xoops_aw_encode')) . "'"; |
||
| 235 | break; |
||
| 236 | |||
| 237 | case XOBJ_DTYPE_ARRAY: |
||
| 238 | $cleanv = (array)$cleanv; |
||
| 239 | if (!$v['not_gpc']) { |
||
| 240 | $cleanv = array_map(array(&$ts, 'stripSlashesGPC'), $cleanv); |
||
| 241 | } |
||
| 242 | // TODO: Not encoding safe, should try base64_encode -- phppp |
||
| 243 | $cleanv = $this->handler->db->quote(serialize($cleanv)); |
||
| 244 | break; |
||
| 245 | |||
| 246 | case XOBJ_DTYPE_STIME: |
||
| 247 | case XOBJ_DTYPE_MTIME: |
||
| 248 | case XOBJ_DTYPE_LTIME: |
||
| 249 | $cleanv = !is_string($cleanv) ? (int)$cleanv : strtotime($cleanv); |
||
| 250 | break; |
||
| 251 | |||
| 252 | default: |
||
| 253 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
| 254 | break; |
||
| 255 | } |
||
| 256 | $object->cleanVars[$k] = $cleanv; |
||
| 257 | } |
||
| 258 | if (!empty($errors)) { |
||
| 259 | $object->setErrors($errors); |
||
| 260 | } |
||
| 261 | $object->unsetDirty(); |
||
| 262 | |||
| 263 | return empty($errors) ? true : false; |
||
| 264 | } |
||
| 409 |