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 |
||
248 | function I($name, $default='', $filter=null, $datas=null) |
||
249 | { |
||
250 | static $_PUT=null; |
||
251 | if (strpos($name, '/')) { |
||
252 | // 指定修饰符 |
||
253 | list($name, $type)=explode('/', $name, 2); |
||
254 | } else { |
||
255 | // 默认强制转换为字符串 |
||
256 | $type='s'; |
||
257 | } |
||
258 | if (strpos($name, '.')) { |
||
259 | // 指定参数来源 |
||
260 | list($method, $name)=explode('.', $name, 2); |
||
261 | } else { |
||
262 | // 默认为自动判断 |
||
263 | $method='param'; |
||
264 | } |
||
265 | switch (strtolower($method)) { |
||
266 | case 'get': |
||
267 | $input=&$_GET; |
||
268 | break; |
||
269 | case 'post': |
||
270 | $input=&$_POST; |
||
271 | break; |
||
272 | case 'put': |
||
273 | if (is_null($_PUT)) { |
||
274 | parse_str(file_get_contents('php://input'), $_PUT); |
||
275 | } |
||
276 | $input=$_PUT; |
||
277 | break; |
||
278 | case 'param': |
||
279 | switch ($_SERVER['REQUEST_METHOD']) { |
||
280 | case 'POST': |
||
281 | $input=$_POST; |
||
282 | break; |
||
283 | case 'PUT': |
||
284 | if (is_null($_PUT)) { |
||
285 | parse_str(file_get_contents('php://input'), $_PUT); |
||
286 | } |
||
287 | $input=$_PUT; |
||
288 | break; |
||
289 | default: |
||
290 | $input=$_GET; |
||
291 | } |
||
292 | break; |
||
293 | case 'path': |
||
294 | $input=array(); |
||
295 | if (!empty($_SERVER['PATH_INFO'])) { |
||
296 | $depr=C('URL_PATHINFO_DEPR'); |
||
297 | $input=explode($depr, trim($_SERVER['PATH_INFO'], $depr)); |
||
298 | } |
||
299 | break; |
||
300 | case 'request': |
||
301 | $input=&$_REQUEST; |
||
302 | break; |
||
303 | case 'session': |
||
304 | $input=&$_SESSION; |
||
305 | break; |
||
306 | case 'cookie': |
||
307 | $input=&$_COOKIE; |
||
308 | break; |
||
309 | case 'server': |
||
310 | $input=&$_SERVER; |
||
311 | break; |
||
312 | case 'globals': |
||
313 | $input=&$GLOBALS; |
||
314 | break; |
||
315 | case 'data': |
||
316 | $input=&$datas; |
||
317 | break; |
||
318 | default: |
||
319 | return null; |
||
320 | } |
||
321 | if ('' == $name) { |
||
322 | // 获取全部变量 |
||
323 | $data=$input; |
||
324 | $filters=isset($filter) ? $filter : 'htmlspecialchars'; |
||
325 | if ($filters) { |
||
326 | if (is_string($filters)) { |
||
327 | $filters=explode(',', $filters); |
||
328 | } |
||
329 | else if (is_array($filters)) { |
||
330 | foreach ($filters as $filter) { |
||
331 | $data=array_map_recursive($filter, $data); // 参数过滤 |
||
332 | } |
||
333 | } else { |
||
334 | throw new \RuntimeException('$filters must be an array or string.'); |
||
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) |
||
505 | } |
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: