Completed
Push — 6.0 ( c0e9d9...b80043 )
by liu
02:35
created

debug()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 6
rs 10
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
//------------------------
14
// ThinkPHP 助手函数
15
//-------------------------
16
17
use think\App;
18
use think\Container;
19
use think\exception\HttpException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use think\exception\HttpResponseException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpResponseException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use think\facade\Cache;
22
use think\facade\Config;
23
use think\facade\Cookie;
24
use think\facade\Db;
25
use think\facade\Env;
26
use think\facade\Event;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
use think\facade\Lang;
28
use think\facade\Log;
29
use think\facade\Request;
30
use think\facade\Route;
31
use think\facade\Session;
32
use think\Model;
33
use think\model\Collection as ModelCollection;
34
use think\Response;
35
use think\route\RuleItem;
36
37
if (!function_exists('abort')) {
38
    /**
39
     * 抛出HTTP异常
40
     * @param integer|Response $code    状态码 或者 Response对象实例
41
     * @param string           $message 错误信息
42
     * @param array            $header  参数
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
44
    function abort($code, string $message = null, array $header = [])
45
    {
46
        if ($code instanceof Response) {
47
            throw new HttpResponseException($code);
48
        } else {
49
            throw new HttpException($code, $message, null, $header);
50
        }
51
    }
52
}
53
54
if (!function_exists('app')) {
55
    /**
56
     * 快速获取容器中的实例 支持依赖注入
57
     * @param string $name        类名或标识 默认获取当前应用实例
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @param array  $args        参数
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
59
     * @param bool   $newInstance 是否每次创建新的实例
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
60
     * @return object|App
61
     */
62
    function app(string $name = App::class, array $args = [], bool $newInstance = false)
63
    {
64
        return Container::pull($name, $args, $newInstance);
65
    }
66
}
67
68
if (!function_exists('bind')) {
69
    /**
70
     * 绑定一个类到容器
71
     * @param  string|array $abstract 类标识、接口(支持批量绑定)
72
     * @param  mixed        $concrete 要绑定的类、闭包或者实例
73
     * @return Container
74
     */
75
    function bind($abstract, $concrete = null)
76
    {
77
        return Container::getInstance()->bind($abstract, $concrete);
78
    }
79
}
80
81
if (!function_exists('cache')) {
82
    /**
83
     * 缓存管理
84
     * @param  mixed  $name    缓存名称,如果为数组表示进行缓存设置
85
     * @param  mixed  $value   缓存值
86
     * @param  mixed  $options 缓存参数
87
     * @param  string $tag     缓存标签
88
     * @return mixed
89
     */
90
    function cache($name, $value = '', $options = null, $tag = null)
91
    {
92
        if (is_array($name)) {
93
            // 缓存初始化
94
            return Cache::connect($name);
95
        }
96
97
        if ('' === $value) {
98
            // 获取缓存
99
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
100
        } elseif (is_null($value)) {
101
            // 删除缓存
102
            return Cache::rm($name);
103
        }
104
105
        // 缓存数据
106
        if (is_array($options)) {
107
            $expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
108
        } else {
109
            $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
110
        }
111
112
        if (is_null($tag)) {
113
            return Cache::set($name, $value, $expire);
114
        } else {
115
            return Cache::tag($tag)->set($name, $value, $expire);
116
        }
117
    }
118
}
119
120
if (!function_exists('call')) {
121
    /**
122
     * 调用反射执行callable 支持依赖注入
123
     * @param  mixed $callable 支持闭包等callable写法
124
     * @param  array $args     参数
125
     * @return mixed
126
     */
127
    function call(callable $callable, array $args = [])
128
    {
129
        return Container::getInstance()->invoke($callable, $args);
130
    }
131
}
132
133
if (!function_exists('class_basename')) {
134
    /**
135
     * 获取类名(不包含命名空间)
136
     *
137
     * @param  mixed $class 类名
138
     * @return string
139
     */
140
    function class_basename($class)
0 ignored issues
show
Coding Style introduced by
Function name "class_basename" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "class_basename" is invalid; consider "Class_basename" instead
Loading history...
141
    {
142
        $class = is_object($class) ? get_class($class) : $class;
143
        return basename(str_replace('\\', '/', $class));
144
    }
145
}
146
147
if (!function_exists('trait_uses_recursive')) {
148
    /**
149
     * 获取一个trait里所有引用到的trait
150
     *
151
     * @param  string $trait
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
152
     * @return array
153
     */
154
    function trait_uses_recursive($trait)
0 ignored issues
show
Coding Style introduced by
Function name "trait_uses_recursive" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "trait_uses_recursive" is invalid; consider "Trait_Uses_recursive" instead
Loading history...
155
    {
156
        $traits = class_uses($trait);
157
158
        foreach ($traits as $trait) {
0 ignored issues
show
introduced by
$trait is overwriting one of the parameters of this function.
Loading history...
159
            $traits += trait_uses_recursive($trait);
160
        }
161
162
        return $traits;
163
    }
164
}
165
166
if (!function_exists('class_uses_recursive')) {
167
    /**
168
     *获取一个类里所有用到的trait,包括父类的
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
169
     *
170
     * @param  mixed $class 类名
171
     * @return array
172
     */
173
    function class_uses_recursive($class)
0 ignored issues
show
Coding Style introduced by
Function name "class_uses_recursive" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "class_uses_recursive" is invalid; consider "Class_Uses_recursive" instead
Loading history...
174
    {
175
        if (is_object($class)) {
176
            $class = get_class($class);
177
        }
178
179
        $results = [];
180
        $classes = array_merge([$class => $class], class_parents($class));
181
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
182
            $results += trait_uses_recursive($class);
183
        }
184
185
        return array_unique($results);
186
    }
187
}
188
189
if (!function_exists('config')) {
190
    /**
191
     * 获取和设置配置参数
192
     * @param  string|array $name  参数名
193
     * @param  mixed        $value 参数值
194
     * @return mixed
195
     */
196
    function config($name = '', $value = null)
197
    {
198
        if (is_array($name)) {
199
            return Config::set($name, $value);
200
        }
201
202
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
203
    }
204
}
205
206
if (!function_exists('cookie')) {
207
    /**
208
     * Cookie管理
209
     * @param  string $name   cookie名称
210
     * @param  mixed  $value  cookie值
211
     * @param  mixed  $option 参数
212
     * @return mixed
213
     */
214
    function cookie(string $name, $value = '', $option = null)
215
    {
216
        if (is_null($value)) {
217
            // 删除
218
            return Cookie::delete($name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of think\facade\Cookie::delete($name) targeting think\facade\Cookie::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
219
        } elseif ('' === $value) {
220
            // 获取
221
            return 0 === strpos($name, '?') ? Request::has(substr($name, 1), 'cookie') : Request::cookie($name);
222
        } else {
223
            // 设置
224
            return Cookie::set($name, $value, $option);
225
        }
226
    }
227
}
228
229
if (!function_exists('download')) {
230
    /**
231
     * 获取\think\response\Download对象实例
232
     * @param  string $filename 要下载的文件
233
     * @param  string $name     显示文件名
234
     * @param  bool   $content  是否为内容
235
     * @param  int    $expire   有效期(秒)
236
     * @return \think\response\File
237
     */
238
    function download(string $filename, string $name = '', bool $content = false, int $expire = 180)
239
    {
240
        return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire);
0 ignored issues
show
Bug introduced by
The method name() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
        return Response::create($filename, 'file')->/** @scrutinizer ignore-call */ name($name)->isContent($content)->expire($expire);
Loading history...
241
    }
242
}
243
244
if (!function_exists('dump')) {
245
    /**
246
     * 浏览器友好的变量输出
247
     * @param  mixed  $var   变量
248
     * @param  bool   $echo  是否输出 默认为true 如果为false 则返回输出字符串
249
     * @param  string $label 标签 默认为空
250
     * @return void|string
251
     */
252
    function dump($var, bool $echo = true, string $label = null)
253
    {
254
        $label = (null === $label) ? '' : rtrim($label) . ':';
255
        if ($var instanceof Model || $var instanceof ModelCollection) {
256
            $var = $var->toArray();
257
        }
258
259
        ob_start();
260
        var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var) looks like debug code. Are you sure you do not want to remove it?
Loading history...
261
262
        $output = ob_get_clean();
263
        $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
264
265
        if (PHP_SAPI == 'cli') {
266
            $output = PHP_EOL . $label . $output . PHP_EOL;
267
        } else {
268
            if (!extension_loaded('xdebug')) {
269
                $output = htmlspecialchars($output, ENT_SUBSTITUTE);
270
            }
271
            $output = '<pre>' . $label . $output . '</pre>';
272
        }
273
274
        if ($echo) {
275
            echo $output;
276
            return;
277
        }
278
279
        return $output;
280
    }
281
}
282
283
if (!function_exists('env')) {
284
    /**
285
     * 获取环境变量值
286
     * @access public
287
     * @param  string $name    环境变量名(支持二级 .号分割)
288
     * @param  string $default 默认值
289
     * @return mixed
290
     */
291
    function env(string $name = null, $default = null)
292
    {
293
        return Env::get($name, $default);
294
    }
295
}
296
297
if (!function_exists('event')) {
298
    /**
299
     * 触发事件
300
     * @param  mixed $event 事件名(或者类名)
301
     * @param  mixed $args  参数
302
     * @return mixed
303
     */
304
    function event($event, $args = null)
305
    {
306
        return Event::trigger($event, $args);
307
    }
308
}
309
310
if (!function_exists('exception')) {
311
    /**
312
     * 抛出异常处理
313
     *
314
     * @param string $msg       异常消息
315
     * @param int    $code      异常代码 默认为0
316
     * @param string $exception 异常类
317
     *
318
     * @throws Exception
319
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
320
    function exception(string $msg, int $code = 0, string $exception = '')
321
    {
322
        $e = $exception ?: '\think\Exception';
323
        throw new $e($msg, $code);
324
    }
325
}
326
327
if (!function_exists('halt')) {
328
    /**
329
     * 调试变量并且中断输出
330
     * @param mixed $var 调试变量或者信息
331
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
332
    function halt($var)
333
    {
334
        dump($var);
335
336
        throw new HttpResponseException(new Response);
337
    }
338
}
339
340
if (!function_exists('input')) {
341
    /**
342
     * 获取输入数据 支持默认值和过滤
343
     * @param  string $key     获取的变量名
344
     * @param  mixed  $default 默认值
345
     * @param  string $filter  过滤方法
346
     * @return mixed
347
     */
348
    function input(string $key = '', $default = null, $filter = '')
349
    {
350
        if (0 === strpos($key, '?')) {
351
            $key = substr($key, 1);
352
            $has = true;
353
        }
354
355
        if ($pos = strpos($key, '.')) {
356
            // 指定参数来源
357
            $method = substr($key, 0, $pos);
358
            if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
359
                $key = substr($key, $pos + 1);
360
            } else {
361
                $method = 'param';
362
            }
363
        } else {
364
            // 默认为自动判断
365
            $method = 'param';
366
        }
367
368
        if (isset($has)) {
369
            return request()->has($key, $method, $default);
370
        } else {
371
            return request()->$method($key, $default, $filter);
372
        }
373
    }
374
}
375
376
if (!function_exists('json')) {
377
    /**
378
     * 获取\think\response\Json对象实例
379
     * @param  mixed $data    返回的数据
380
     * @param  int   $code    状态码
381
     * @param  array $header  头部
382
     * @param  array $options 参数
383
     * @return \think\response\Json
384
     */
385
    function json($data = [], $code = 200, $header = [], $options = [])
386
    {
387
        return Response::create($data, 'json', $code)->header($header)->options($options);
388
    }
389
}
390
391
if (!function_exists('jsonp')) {
392
    /**
393
     * 获取\think\response\Jsonp对象实例
394
     * @param  mixed $data    返回的数据
395
     * @param  int   $code    状态码
396
     * @param  array $header  头部
397
     * @param  array $options 参数
398
     * @return \think\response\Jsonp
399
     */
400
    function jsonp($data = [], $code = 200, $header = [], $options = [])
401
    {
402
        return Response::create($data, 'jsonp', $code)->header($header)->options($options);
403
    }
404
}
405
406
if (!function_exists('lang')) {
407
    /**
408
     * 获取语言变量值
409
     * @param  string $name 语言变量名
410
     * @param  array  $vars 动态变量值
411
     * @param  string $lang 语言
412
     * @return mixed
413
     */
414
    function lang(string $name, array $vars = [], string $lang = '')
415
    {
416
        return Lang::get($name, $vars, $lang);
417
    }
418
}
419
420
if (!function_exists('parse_name')) {
421
    /**
422
     * 字符串命名风格转换
423
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
424
     * @param  string $name    字符串
425
     * @param  int    $type    转换类型
426
     * @param  bool   $ucfirst 首字母是否大写(驼峰规则)
427
     * @return string
428
     */
429
    function parse_name(string $name, int $type = 0, bool $ucfirst = true)
0 ignored issues
show
Coding Style introduced by
Function name "parse_name" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "parse_name" is invalid; consider "Parse_name" instead
Loading history...
430
    {
431
        if ($type) {
432
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
433
                return strtoupper($match[1]);
434
            }, $name);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
435
436
            return $ucfirst ? ucfirst($name) : lcfirst($name);
437
        }
438
439
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
440
    }
441
}
442
443
if (!function_exists('raw')) {
444
    /**
445
     * 生成一个数据库的Raw对象
446
     * @param  string $sql SQL指令
447
     * @return \think\db\Raw
448
     */
449
    function raw(string $sql)
450
    {
451
        return Db::raw($sql);
452
    }
453
}
454
455
if (!function_exists('redirect')) {
456
    /**
457
     * 获取\think\response\Redirect对象实例
458
     * @param  mixed         $url    重定向地址 支持Url::build方法的地址
459
     * @param  array|integer $params 额外参数
460
     * @param  int           $code   状态码
461
     * @return \think\response\Redirect
462
     */
463
    function redirect($url = [], $params = [], $code = 302)
464
    {
465
        if (is_integer($params)) {
466
            $code   = $params;
467
            $params = [];
468
        }
469
470
        return Response::create($url, 'redirect', $code)->params($params);
0 ignored issues
show
Bug introduced by
The method params() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\Redirect. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

470
        return Response::create($url, 'redirect', $code)->/** @scrutinizer ignore-call */ params($params);
Loading history...
471
    }
472
}
473
474
if (!function_exists('request')) {
475
    /**
476
     * 获取当前Request对象实例
477
     * @return Request
478
     */
479
    function request()
480
    {
481
        return app('request');
482
    }
483
}
484
485
if (!function_exists('response')) {
486
    /**
487
     * 创建普通 Response 对象实例
488
     * @param  mixed      $data   输出数据
489
     * @param  int|string $code   状态码
490
     * @param  array      $header 头信息
491
     * @param  string     $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
492
     * @return Response
493
     */
494
    function response($data = '', $code = 200, $header = [], $type = 'html')
495
    {
496
        return Response::create($data, $type, $code)->header($header);
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type string; however, parameter $code of think\Response::create() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

496
        return Response::create($data, $type, /** @scrutinizer ignore-type */ $code)->header($header);
Loading history...
497
    }
498
}
499
500
if (!function_exists('route')) {
501
    /**
502
     * 路由注册
503
     * @param  string $rule   路由规则
504
     * @param  mixed  $route  路由地址
505
     * @param  string $method 请求类型
506
     * @return RuleItem
507
     */
508
    function route(string $rule, $route, $method = '*')
509
    {
510
        return Route::rule($rule, $route, $method);
0 ignored issues
show
Unused Code introduced by
The call to think\facade\Route::rule() has too many arguments starting with $rule. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

510
        return Route::/** @scrutinizer ignore-call */ rule($rule, $route, $method);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
511
    }
512
}
513
514
if (!function_exists('session')) {
515
    /**
516
     * Session管理
517
     * @param  string $name  session名称
518
     * @param  mixed  $value session值
519
     * @return mixed
520
     */
521
    function session(string $name = null, $value = '')
522
    {
523
        if (is_null($name)) {
524
            // 清除
525
            Session::clear();
526
        } elseif (is_null($value)) {
527
            // 删除
528
            Session::delete($name);
529
        } elseif ('' === $value) {
530
            // 判断或获取
531
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
532
        } else {
533
            // 设置
534
            Session::set($name, $value);
535
        }
536
    }
537
}
538
539
if (!function_exists('token')) {
540
    /**
541
     * 生成表单令牌
542
     * @param  string $name 令牌名称
543
     * @param  mixed  $type 令牌生成方法
544
     * @return string
545
     */
546
    function token(string $name = '__token__', string $type = 'md5'): string
547
    {
548
        $token = Request::token($name, $type);
549
550
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
551
    }
552
}
553
554
if (!function_exists('trace')) {
555
    /**
556
     * 记录日志信息
557
     * @param  mixed  $log   log信息 支持字符串和数组
558
     * @param  string $level 日志级别
559
     * @return array|void
560
     */
561
    function trace($log = '[think]', string $level = 'log')
562
    {
563
        if ('[think]' === $log) {
564
            return Log::getLog();
565
        }
566
567
        Log::record($log, $level);
568
    }
569
}
570
571
if (!function_exists('trait_uses_recursive')) {
572
    /**
573
     * 获取一个trait里所有引用到的trait
574
     *
575
     * @param  string $trait Trait
576
     * @return array
577
     */
578
    function trait_uses_recursive(string $trait)
0 ignored issues
show
Coding Style introduced by
Function name "trait_uses_recursive" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "trait_uses_recursive" is invalid; consider "Trait_Uses_recursive" instead
Loading history...
579
    {
580
        $traits = class_uses($trait);
581
        foreach ($traits as $trait) {
582
            $traits += traitUsesRecursive($trait);
0 ignored issues
show
Bug introduced by
The function traitUsesRecursive was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

582
            $traits += /** @scrutinizer ignore-call */ traitUsesRecursive($trait);
Loading history...
583
        }
584
585
        return $traits;
586
    }
587
}
588
589
if (!function_exists('url')) {
590
    /**
591
     * Url生成
592
     * @param string      $url    路由地址
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
593
     * @param array       $vars   变量
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
594
     * @param bool|string $suffix 生成的URL后缀
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
595
     * @param bool|string $domain 域名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
596
     * @return string
597
     */
598
    function url(string $url = '', array $vars = [], $suffix = true, $domain = false)
599
    {
600
        return Route::buildUrl($url, $vars, $suffix, $domain);
0 ignored issues
show
Bug introduced by
The method buildUrl() does not exist on think\facade\Route. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

600
        return Route::/** @scrutinizer ignore-call */ buildUrl($url, $vars, $suffix, $domain);
Loading history...
601
    }
602
}
603
604
if (!function_exists('validate')) {
605
    /**
606
     * 验证数据
607
     * @param  array        $data     数据
608
     * @param  string|array $validate 验证器名或者验证规则数组
609
     * @param  array        $message  提示信息
610
     * @param  bool         $batch    是否批量验证
611
     * @return true
612
     * @throws ValidateException
613
     */
614
    function validate(array $data, $validate, array $message = [], bool $batch = false)
615
    {
616
        if (is_array($validate)) {
617
            $v = new Validate();
0 ignored issues
show
Bug introduced by
The type Validate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
618
            $v->rule($validate);
619
        } else {
620
            if (strpos($validate, '.')) {
621
                // 支持场景
622
                list($validate, $scene) = explode('.', $validate);
623
            }
624
625
            $class = app()->parseClass('validate', $validate);
626
            $v     = new $class();
627
628
            if (!empty($scene)) {
629
                $v->scene($scene);
630
            }
631
        }
632
633
        return $v->message($message)->batch($batch)->failException(true)->check($data);
634
    }
635
}
636
637
if (!function_exists('view')) {
638
    /**
639
     * 渲染模板输出
640
     * @param string    $template 模板文件
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
641
     * @param array     $vars 模板变量
1 ignored issue
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
642
     * @param int       $code 状态码
1 ignored issue
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
643
     * @param callable  $filter 内容过滤
1 ignored issue
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
644
     * @return \think\response\View
645
     */
646
    function view(string $template = '', $vars = [], $code = 200, $filter = null)
647
    {
648
        return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\View. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

648
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
649
    }
650
}
651
652
if (!function_exists('xml')) {
653
    /**
654
     * 获取\think\response\Xml对象实例
655
     * @param  mixed $data    返回的数据
656
     * @param  int   $code    状态码
657
     * @param  array $header  头部
658
     * @param  array $options 参数
659
     * @return \think\response\Xml
660
     */
661
    function xml($data = [], $code = 200, $header = [], $options = [])
662
    {
663
        return Response::create($data, 'xml', $code)->header($header)->options($options);
664
    }
665
}
666
667
if (!function_exists('yaconf')) {
668
    /**
669
     * 获取yaconf配置
670
     *
671
     * @param  string $name    配置参数名
672
     * @param  mixed  $default 默认值
673
     * @return mixed
674
     */
675
    function yaconf(string $name, $default = null)
676
    {
677
        return Config::yaconf($name, $default);
0 ignored issues
show
Bug introduced by
The method yaconf() does not exist on think\facade\Config. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

677
        return Config::/** @scrutinizer ignore-call */ yaconf($name, $default);
Loading history...
678
    }
679
}
680