Conditions | 67 |
Paths | 208 |
Total Lines | 187 |
Code Lines | 137 |
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 |
||
43 | public function cleanVars($object) |
||
44 | { |
||
45 | $myts = \MyTextSanitizer::getInstance(); |
||
46 | $errors = []; |
||
47 | |||
48 | $vars = $object->getVars(); |
||
49 | $object->cleanVars = []; |
||
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 | |||
79 | $cleanv = $myts->censorString($cleanv); |
||
80 | |||
81 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
82 | break; |
||
83 | |||
84 | case XOBJ_DTYPE_UNICODE_TXTAREA: |
||
85 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
86 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
87 | continue 2; |
||
88 | } |
||
89 | $cleanv = xoops_convert_encode($cleanv); |
||
90 | $cleanv = $myts->censorString($cleanv); |
||
91 | |||
92 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
93 | break; |
||
94 | |||
95 | case XOBJ_DTYPE_TXTBOX: |
||
96 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
97 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
98 | continue 2; |
||
99 | } |
||
100 | if (isset($v['maxlength']) && strlen($cleanv) > (int)$v['maxlength']) { |
||
101 | $errors[] = sprintf(_XOBJ_ERR_SHORTERTHAN, $k, (int)$v['maxlength']); |
||
102 | continue 2; |
||
103 | } |
||
104 | |||
105 | $cleanv = $myts->censorString($cleanv); |
||
106 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
107 | break; |
||
108 | |||
109 | case XOBJ_DTYPE_TXTAREA: |
||
110 | if ($v['required'] && $cleanv != '0' && $cleanv == '') { |
||
111 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
112 | continue 2; |
||
113 | } |
||
114 | |||
115 | $cleanv = $myts->censorString($cleanv); |
||
116 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
117 | break; |
||
118 | |||
119 | case XOBJ_DTYPE_SOURCE: |
||
120 | $cleanv = trim($cleanv); |
||
121 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
122 | break; |
||
123 | // Should not be used! |
||
124 | case XOBJ_DTYPE_UNICODE_EMAIL: |
||
125 | $cleanv = trim($cleanv); |
||
126 | if ($v['required'] && $cleanv == '') { |
||
127 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
128 | continue 2; |
||
129 | } |
||
130 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
131 | break; |
||
132 | |||
133 | case XOBJ_DTYPE_EMAIL: |
||
134 | $cleanv = trim($cleanv); |
||
135 | if ($v['required'] && $cleanv == '') { |
||
136 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
137 | continue 2; |
||
138 | } |
||
139 | if ($cleanv != '' && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $cleanv)) { |
||
140 | $errors[] = 'Invalid Email'; |
||
141 | continue 2; |
||
142 | } |
||
143 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
144 | break; |
||
145 | |||
146 | // Should not be used! |
||
147 | case XOBJ_DTYPE_UNICODE_URL: |
||
148 | $cleanv = trim($cleanv); |
||
149 | if ($v['required'] && $cleanv == '') { |
||
150 | $errors[] = sprintf(_XOBJ_ERR_REQUIRED, $k); |
||
151 | continue 2; |
||
152 | } |
||
153 | if ($cleanv != '' && !preg_match("/^http[s]*:\/\//i", $cleanv)) { |
||
154 | $cleanv = XOOPS_PROT . $cleanv; |
||
155 | } |
||
156 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
157 | break; |
||
158 | case XOBJ_DTYPE_URL: |
||
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("/^http[s]*:\/\//i", $cleanv)) { |
||
165 | $cleanv = XOOPS_PROT . $cleanv; |
||
166 | } |
||
167 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
168 | break; |
||
169 | |||
170 | // Should not be used! |
||
171 | case XOBJ_DTYPE_UNICODE_OTHER: |
||
172 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote(xoops_convert_encode($cleanv))); |
||
173 | break; |
||
174 | |||
175 | case XOBJ_DTYPE_OTHER: |
||
176 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
177 | break; |
||
178 | |||
179 | case XOBJ_DTYPE_INT: |
||
180 | $cleanv = (int)$cleanv; |
||
181 | break; |
||
182 | |||
183 | case XOBJ_DTYPE_FLOAT: |
||
184 | $cleanv = (float)$cleanv; |
||
185 | break; |
||
186 | |||
187 | case XOBJ_DTYPE_DECIMAL: |
||
188 | $cleanv = (float)$cleanv; |
||
189 | break; |
||
190 | |||
191 | // Should not be used! |
||
192 | case XOBJ_DTYPE_UNICODE_ARRAY: |
||
193 | if (!$v['not_gpc']) { |
||
194 | $cleanv = array_map([&$myts, 'stripSlashesGPC'], $cleanv); |
||
195 | } |
||
196 | foreach (array_keys($cleanv) as $key) { |
||
197 | $cleanv[$key] = str_replace('\\"', '"', addslashes($cleanv[$key])); |
||
198 | } |
||
199 | // TODO: Not encoding safe, should try base64_encode -- phppp |
||
200 | $cleanv = "'" . serialize(array_walk($cleanv, 'xoops_aw_encode')) . "'"; |
||
201 | break; |
||
202 | |||
203 | case XOBJ_DTYPE_ARRAY: |
||
204 | $cleanv = (array)$cleanv; |
||
205 | if (!$v['not_gpc']) { |
||
206 | $cleanv = array_map([&$myts, 'stripSlashesGPC'], $cleanv); |
||
207 | } |
||
208 | // TODO: Not encoding safe, should try base64_encode -- phppp |
||
209 | $cleanv = $this->handler->db->quote(serialize($cleanv)); |
||
210 | break; |
||
211 | |||
212 | case XOBJ_DTYPE_STIME: |
||
213 | case XOBJ_DTYPE_MTIME: |
||
214 | case XOBJ_DTYPE_LTIME: |
||
215 | $cleanv = !is_string($cleanv) ? (int)$cleanv : strtotime($cleanv); |
||
216 | break; |
||
217 | |||
218 | default: |
||
219 | $cleanv = str_replace('\\"', '"', $this->handler->db->quote($cleanv)); |
||
220 | break; |
||
221 | } |
||
222 | $object->cleanVars[$k] = $cleanv; |
||
223 | } |
||
224 | if (!empty($errors)) { |
||
225 | $object->setErrors($errors); |
||
226 | } |
||
227 | $object->unsetDirty(); |
||
228 | |||
229 | return empty($errors) ? true : false; |
||
230 | } |
||
375 |