Conditions | 47 |
Paths | 18436 |
Total Lines | 143 |
Code Lines | 111 |
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 |
||
252 | function I($name, $default = '', $filter = null, $datas = null) |
||
253 | { |
||
254 | static $_PUT = null; |
||
255 | if (strpos($name, '/')) { |
||
256 | // 指定修饰符 |
||
257 | list($name, $type) = explode('/', $name, 2); |
||
258 | } else{ |
||
259 | // 默认强制转换为字符串 |
||
260 | $type = 's'; |
||
261 | } |
||
262 | if (strpos($name, '.')) { |
||
263 | // 指定参数来源 |
||
264 | list($method, $name) = explode('.', $name, 2); |
||
265 | } else { |
||
266 | // 默认为自动判断 |
||
267 | $method = 'param'; |
||
268 | } |
||
269 | switch (strtolower($method)) { |
||
270 | case 'get': |
||
271 | $input = &$_GET; |
||
272 | break; |
||
273 | case 'post': |
||
274 | $input = &$_POST; |
||
275 | break; |
||
276 | case 'put': |
||
277 | if (is_null($_PUT)) { |
||
278 | parse_str(file_get_contents('php://input'), $_PUT); |
||
279 | } |
||
280 | $input = $_PUT; |
||
281 | break; |
||
282 | case 'param': |
||
283 | switch ($_SERVER['REQUEST_METHOD']) { |
||
284 | case 'POST': |
||
285 | $input = $_POST; |
||
286 | break; |
||
287 | case 'PUT': |
||
288 | if (is_null($_PUT)) { |
||
289 | parse_str(file_get_contents('php://input'), $_PUT); |
||
290 | } |
||
291 | $input = $_PUT; |
||
292 | break; |
||
293 | default: |
||
294 | $input = $_GET; |
||
295 | } |
||
296 | break; |
||
297 | case 'path': |
||
298 | $input = array(); |
||
299 | if (!empty($_SERVER['PATH_INFO'])) { |
||
300 | $depr = C('URL_PATHINFO_DEPR'); |
||
301 | $input = explode($depr, trim($_SERVER['PATH_INFO'], $depr)); |
||
302 | } |
||
303 | break; |
||
304 | case 'request': |
||
305 | $input = &$_REQUEST; |
||
306 | break; |
||
307 | case 'session': |
||
308 | $input = &$_SESSION; |
||
309 | break; |
||
310 | case 'cookie': |
||
311 | $input = &$_COOKIE; |
||
312 | break; |
||
313 | case 'server': |
||
314 | $input = &$_SERVER; |
||
315 | break; |
||
316 | case 'globals': |
||
317 | $input = &$GLOBALS; |
||
318 | break; |
||
319 | case 'data': |
||
320 | $input = &$datas; |
||
321 | break; |
||
322 | default: |
||
323 | return null; |
||
324 | } |
||
325 | if ('' == $name) { |
||
326 | // 获取全部变量 |
||
327 | $data = $input; |
||
328 | $filters = isset($filter) ? $filter : 'htmlspecialchars'; |
||
329 | if ($filters) { |
||
330 | if (is_string($filters)) { |
||
331 | $filters = explode(',', $filters); |
||
332 | } |
||
333 | foreach ($filters as $filter) { |
||
334 | $data = array_map_recursive($filter, $data); // 参数过滤 |
||
335 | } |
||
336 | } |
||
337 | } elseif (isset($input[$name])) { |
||
338 | // 取值操作 |
||
339 | $data = $input[$name]; |
||
340 | $filters = isset($filter) ? $filter : 'htmlspecialchars'; |
||
341 | if ($filters) { |
||
342 | if (is_string($filters)) { |
||
343 | if (0 === strpos($filters, '/')) { |
||
344 | if (1 !== preg_match($filters, (string) $data)) { |
||
345 | // 支持正则验证 |
||
346 | return isset($default) ? $default : null; |
||
347 | } |
||
348 | } else { |
||
349 | $filters = explode(',', $filters); |
||
350 | } |
||
351 | } elseif (is_int($filters)) { |
||
352 | $filters = array($filters); |
||
353 | } |
||
354 | |||
355 | if (is_array($filters)) { |
||
356 | foreach ($filters as $filter) { |
||
357 | $filter = trim($filter); |
||
358 | if (function_exists($filter)) { |
||
359 | $data = is_array($data) ? array_map_recursive($filter, $data) : $filter($data); // 参数过滤 |
||
360 | } else { |
||
361 | $data = filter_var($data, is_int($filter) ? $filter : filter_id($filter)); |
||
362 | if (false === $data) { |
||
363 | return isset($default) ? $default : null; |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | if (!empty($type)) { |
||
370 | switch (strtolower($type)) { |
||
371 | case 'a': // 数组 |
||
372 | $data = (array) $data; |
||
373 | break; |
||
374 | case 'd': // 数字 |
||
375 | $data = (int) $data; |
||
376 | break; |
||
377 | case 'f': // 浮点 |
||
378 | $data = (float) $data; |
||
379 | break; |
||
380 | case 'b': // 布尔 |
||
381 | $data = (boolean) $data; |
||
382 | break; |
||
383 | case 's':// 字符串 |
||
384 | default: |
||
385 | $data = (string) $data; |
||
386 | } |
||
387 | } |
||
388 | } else { |
||
389 | // 变量默认值 |
||
390 | $data = isset($default) ? $default : null; |
||
391 | } |
||
392 | is_array($data) && array_walk_recursive($data, 'think_filter'); |
||
393 | return $data; |
||
394 | } |
||
395 | function array_map_recursive($filter, $data) |
||
437 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: