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