GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#28)
by t
02:49
created

I   F

Complexity

Total Complexity 80

Size/Duplication

Total Lines 412
Duplicated Lines 0 %

Test Coverage

Coverage 67.12%

Importance

Changes 28
Bugs 6 Features 0
Metric Value
eloc 151
c 28
b 6
f 0
dl 0
loc 412
ccs 98
cts 146
cp 0.6712
rs 2
wmc 80

15 Methods

Rating   Name   Duplication   Size   Complexity  
D get() 0 52 22
B set() 0 19 7
A def() 0 3 2
A displayErrors() 0 4 3
A isYii2() 0 3 1
A isEmpty() 0 3 1
A ini() 0 6 2
C setAlias() 0 39 13
A isWin() 0 3 1
A obj() 0 20 6
C getAlias() 0 36 12
A isExt() 0 6 3
A phpini() 0 3 3
A call() 0 6 3
A hasFlag() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like I often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use I, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Class I
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php;
10
11
use Exception;
12
use icy2003\php\ihelpers\Strings;
13
use ReflectionClass;
14
15
/**
16
 * I 类
17
 */
18
class I
19
{
20
21
    /**
22
     * 获取值
23
     *
24
     * 支持类型:数组对象和 null、数字和字符串、布尔值、回调函数,依据数据类型有不同的含义(但是都很合理)
25
     *
26
     * @param mixed $mixed 混合类型
27
     *      - 当 $mixed 为**数组或对象**时,此方法用于按照层级获取值,用法如下:
28
     *          1. 对于一个多维数组 $array 来说,a.b.cd_ef 会拿 $array['a']['b']['cd_ef'] 的值
29
     *          2. 如果 $array['a'] 是对象,则先检查 getB 方法,然后检查 b 属性
30
     *          3. 如果 $array['a']['b'] 是对象,则检查 getCdEf 方法,然后检查 cd_ef 属性
31
     *      - 当 $mixed 为**布尔值**(即表达式)时,等价于三元操作符,例如 I::get(1 > 2, '真', '假')
32
     *      - 当 $mixed 为**字符串**时,等价于 Strings::sub,截取字符串
33
     *      - 当 $mixed 为 **null** 时,含义可被描述为:在使用 I::get($array, 'a.b', 1),$array 意外的是 null,返回 1 是理所当然的
34
     *      - 当 $mixed 为**回调函数**,$mixed 的执行结果将作为 I::get 的返回值
35
     * @param mixed $keyString 取决于 $mixed 的类型:
36
     *      - 当 $mixed 为**数组或对象**时,$keyString 表示:点(.)分割代表层级的字符串,下划线用于对象中转化成驼峰方法,支持数组和对象嵌套
37
     *      - 当 $mixed 为**布尔值**(即表达式)时,$keyString 表示:$mixed 为 true 时返回的值
38
     *      - 当 $mixed 为**字符串**时,$keyString 强制转为整型,表示:截取 $mixed 时,子串的起始位置
39
     *      - 当 $mixed 为 **null** 时,此参数无效
40
     *      - 当 $mixed 为**回调函数**,如果 $mixed 的返回值代表 true(如:1),则执行此回调
41
     * @param mixed $defaultValue 取决于 $mixed 的类型:
42
     *      - 当 $mixed 为**数组或对象**时,$defaultValue 表示:拿不到值时会直接返回该默认值
43
     *      - 当 $mixed 为**布尔值**(即表达式)时,$defaultValue 表示:$mixed 为 false 时返回的值
44
     *      - 当 $mixed 为**字符串**时,$defaultValue 表示:截取 $mixed 时,子串的长度,null 时表示长度为 1
45
     *      - 当 $mixed 为 **null** 时,返回 $defaultValue
46
     *      - 当 $mixed 为**回调函数**,如果 $mixed 的返回值代表 false(如:0),则执行此回调
47
     *
48
     * @return mixed
49
     */
50 50
    public static function get($mixed, $keyString, $defaultValue = null)
51
    {
52 50
        if (is_bool($mixed)) { // 布尔类型
53 1
            return true === $mixed ? $keyString : $defaultValue;
54 50
        } elseif (is_callable($mixed)) { // 回调
55 1
            $result = self::call($mixed);
56 1
            if ($result) {
57 1
                self::call($keyString);
58
            } else {
59 1
                self::call($defaultValue);
60
            }
61 1
            return $result;
62 50
        } elseif (is_array($mixed) || is_object($mixed)) { // 数组和对象
63 50
            if (false === is_string($keyString) && is_callable($keyString)) {
64
                $mixed = self::call($keyString, [$mixed]);
65
            } else {
66 50
                $keyArray = explode('.', $keyString);
67 50
                foreach ($keyArray as $key) {
68 50
                    if (is_array($mixed)) {
69 50
                        if (array_key_exists($key, $mixed) && null !== $mixed[$key]) {
70 49
                            $mixed = $mixed[$key];
71
                        } else {
72 50
                            return $defaultValue;
73
                        }
74 2
                    } elseif (is_object($mixed)) {
75 2
                        $method = 'get' . ucfirst(Strings::toCamel($key));
76 2
                        if (method_exists($mixed, $method)) {
77 1
                            $mixed = $mixed->$method();
78 2
                        } elseif (property_exists($mixed, $key) && null !== $mixed->$key) {
79 2
                            $mixed = $mixed->$key;
80
                        } else {
81
                            try {
82 1
                                $mixed = $mixed->$key;
83 1
                            } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84 2
                                return $defaultValue;
85
                            }
86
87
                        }
88
                    } else {
89 1
                        return self::get($mixed, $key, $defaultValue);
90
                    }
91
                }
92
            }
93 49
            return $mixed;
94 1
        } elseif (is_string($mixed) || is_numeric($mixed)) { // 字符串或数字
95 1
            $pos = (int)$keyString;
96 1
            $length = null === $defaultValue ? 1 : (int)$defaultValue;
97 1
            return Strings::sub($mixed, $pos, $length);
98 1
        } elseif (null === $mixed) { // null
99 1
            return $defaultValue;
100
        } else { // 资源
101 1
            return $defaultValue;
102
        }
103
    }
104
105
    /**
106
     * 设置值
107
     *
108
     * @param array|object $mixed 对象或数组
109
     * @param string $key 键
110
     * @param mixed $value 值
111
     * @param boolean $overWrite 如果对应的值存在,是否用给定的值覆盖,默认 true,即:是
112
     *
113
     * @return mixed
114
     */
115 1
    public static function set(&$mixed, $key, $value, $overWrite = true)
116
    {
117 1
        $get = self::get($mixed, $key);
118 1
        if (null === $get || true === $overWrite) {
119 1
            if (is_array($mixed)) {
120 1
                $mixed[$key] = $value;
121 1
            } elseif (is_object($mixed)) {
122 1
                $method = 'set' . ucfirst(Strings::toCamel($key));
123 1
                if (method_exists($mixed, $method)) {
124 1
                    $mixed->$method($value);
125 1
                } elseif (property_exists($mixed, $key)) {
126 1
                    $mixed->$key = $value;
127
                } else {
128 1
                    throw new Exception('无法设置值');
129
                }
130
            }
131 1
            return $value;
132
        }
133 1
        return $get;
134
    }
135
136
    /**
137
     * 触发回调
138
     *
139
     * @param callback|true $callback 回调函数,true 是为了简化某些表达式
140
     * @param array $params 回调参数
141
     * @return mixed
142
     */
143 10
    public static function call($callback, $params = [])
144
    {
145 10
        $result = false;
146 10
        is_callable($callback) && $result = call_user_func_array($callback, $params);
0 ignored issues
show
Bug introduced by
It seems like $callback can also be of type true; however, parameter $function of call_user_func_array() does only seem to accept callable, 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

146
        is_callable($callback) && $result = call_user_func_array(/** @scrutinizer ignore-type */ $callback, $params);
Loading history...
147 10
        true === $callback && $result = true;
148 10
        return $result;
149
    }
150
151
    /**
152
     * 定义一个常量
153
     *
154
     * @param string $constant 常量名
155
     * @param mixed $value 值
156
     *
157
     * @return void
158
     */
159 1
    public static function def($constant, $value)
160
    {
161 1
        defined($constant) || define($constant, $value);
162 1
    }
163
164
    /**
165
     * 让 empty 支持函数调用
166
     *
167
     * 注意:此函数并不比 empty 好,只是为了让 empty 支持函数调用
168
     *
169
     * 例如:empty($array[0]) 就不能用此函数代替,另外,empty 是语法结构,性能明显比函数高
170
     *
171
     * @see http://php.net/manual/zh/function.empty.php
172
     *
173
     * @param mixed $data
174
     * @return boolean
175
     */
176 5
    public static function isEmpty($data)
177
    {
178 5
        return empty($data);
179
    }
180
181
    /**
182
     * 获取 php.ini 配置值
183
     *
184
     * @param string $key 配置名
185
     * @param mixed $default 默认值
186
     *
187
     * @return mixed
188
     */
189 2
    public static function phpini($key, $default = null)
190
    {
191 2
        return false !== ($ini = ini_get($key)) ? $ini : (false !== ($ini = get_cfg_var($key)) ? $ini : $default);
192
    }
193
194
    /**
195
     * 显示 PHP 错误
196
     *
197
     * @param boolean $show 是否显示,默认是
198
     *
199
     * @return void
200
     */
201 1
    public static function displayErrors($show = true)
202
    {
203 1
        ini_set("display_errors", true === $show ? 'On' : 'Off');
204 1
        true === $show && error_reporting(E_ALL | E_STRICT);
205 1
    }
206
207
    /**
208
     * 别名列表
209
     *
210
     * @var array
211
     */
212
    public static $aliases = [
213
        '@vendor' => __DIR__ . '/../../../../vendor',
214
        '@icy2003/php_tests' => __DIR__ . '/../tests',
215
        '@icy2003/php_runtime' => __DIR__ . '/../runtime',
216
        '@icy2003/php' => __DIR__,
217
    ];
218
219
    /**
220
     * 用别名获取真实路径
221
     *
222
     * @param string $alias 别名
223
     *
224
     * @return string|boolean
225
     */
226 50
    public static function getAlias($alias)
227
    {
228 50
        $alias = Strings::replace($alias, ["\\" => '/']);
229 50
        if (strncmp($alias, '@', 1)) {
230 18
            return $alias;
231
        }
232
233 50
        $pos = 0;
234 50
        while (true) {
235 50
            $pos = strpos($alias, '/', $pos);
236 50
            $root = $pos === false ? $alias : substr($alias, 0, $pos);
237 50
            if (isset(static::$aliases[$root])) {
238 50
                if (is_string(static::$aliases[$root])) {
239 50
                    return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
240
                } elseif (is_array(static::$aliases[$root])) {
241
                    foreach (static::$aliases[$root] as $name => $path) {
242
                        if (strpos($alias . '/', $name . '/') === 0) {
243
                            return $path . substr($alias, strlen($name));
244
                        }
245
                    }
246
                } else {
247
                    return false;
248
                }
249
            }
250 50
            if ($root == $alias) {
251
                break;
252
            }
253 50
            $pos++;
254
        }
255
        // 对 Yii2 的支持
256
        if ($result = self::call(['\Yii', 'getAlias'], [$alias])) {
257
            self::setAlias($alias, $result);
258
            return $result;
259
        }
260
261
        return false;
262
    }
263
264
    /**
265
     * 是否是 Yii2 项目
266
     *
267
     * @return boolean
268
     */
269
    public static function isYii2()
270
    {
271
        return method_exists('\Yii', 'getVersion');
272
    }
273
274
    /**
275
     * 设置别名
276
     *
277
     * @param string $alias 别名
278
     * @param string|null $path 路径
279
     *
280
     * @return void
281
     */
282 1
    public static function setAlias($alias, $path)
283
    {
284
        // 对 Yii2 的支持
285
        try {
286 1
            self::call(['\Yii', 'getAlias'], [$alias]);
287
        } catch (Exception $e) {
288
            self::call(['\Yii', 'setAlias'], [$alias, $path]);
289
        }
290 1
        if (strncmp($alias, '@', 1)) {
291
            $alias = '@' . $alias;
292
        }
293 1
        $pos = strpos($alias, '/');
294 1
        $root = $pos === false ? $alias : substr($alias, 0, $pos);
295 1
        if ($path !== null) {
296 1
            $path = strncmp($path, '@', 1) ? rtrim($path, '\\/') : static::getAlias($path);
297 1
            if (!isset(static::$aliases[$root])) {
298 1
                if ($pos === false) {
299 1
                    static::$aliases[$root] = $path;
300
                } else {
301 1
                    static::$aliases[$root] = [$alias => $path];
302
                }
303
            } elseif (is_string(static::$aliases[$root])) {
304
                if ($pos === false) {
305
                    static::$aliases[$root] = $path;
306
                } else {
307
                    static::$aliases[$root] = [
308
                        $alias => $path,
309
                        $root => static::$aliases[$root],
310
                    ];
311
                }
312
            } else {
313
                static::$aliases[$root][$alias] = $path;
314 1
                krsort(static::$aliases[$root]);
315
            }
316
        } elseif (isset(static::$aliases[$root])) {
317
            if (is_array(static::$aliases[$root])) {
318
                unset(static::$aliases[$root][$alias]);
319
            } elseif ($pos === false) {
320
                unset(static::$aliases[$root]);
321
            }
322
        }
323 1
    }
324
325
    /**
326
     * 判断给定选项值里是否设置某选项
327
     *
328
     * @param integer $flags 选项值
329
     * @param integer $flag 待判断的选项值
330
     *
331
     * @return boolean
332
     */
333 4
    public static function hasFlag($flags, $flag)
334
    {
335 4
        return $flags === ($flag | $flags);
336
    }
337
338
    /**
339
     * 创建一个对象
340
     *
341
     * @param array|string $params
342
     *      - 字符串:该字符串将被作为类名转成数组处理
343
     *      - 数组:
344
     *          1. class:表示类名
345
     *          2. 其他:该类的属性,初始化这些属性或者调用相应的 set 方法
346
     * @param array $config
347
     * - 构造函数传参
348
     *
349
     * @return object
350
     * @throws Exception
351
     */
352
    public static function obj($params, $config = [])
353
    {
354
        if (is_string($params)) {
355
            $params = ['class' => $params];
356
        }
357
        if (is_array($params) && isset($params['class'])) {
358
            try {
359
                $class = $params['class'];
360
                unset($params['class']);
361
                $reflection = new ReflectionClass($class);
362
                $object = $reflection->newInstanceArgs($config);
363
                foreach ($params as $name => $value) {
364
                    self::set($object, $name, $value);
365
                }
366
                return $object;
367
            } catch (Exception $e) {
368
                throw new Exception('初始化 ' . $class . ' 失败', $e->getCode(), $e);
369
            }
370
        }
371
        throw new Exception('必须带 class 键来指定一个类');
372
    }
373
374
    /**
375
     * 静态配置
376
     *
377
     * @var array
378
     */
379
    public static $ini = [
380
        'USE_CUSTOM' => false,
381
        'EXT_LOADED' => true,
382
    ];
383
384
    /**
385
     * 读取或设置一个全局配置
386
     *
387
     * - 该配置是利用静态类进行存储的
388
     * - 如果给定 $value,则为设置,不给则为获取
389
     * - 可选默认配置有:
390
     *      1. USE_CUSTOM:默认 false,即尝试使用 php 原生函数的实现,如果此参数为 true,则使用 icy2003/php 的实现
391
     *      2. EXT_LOADED:默认 true,即尝试检测是否有扩展,如果为 false,直接认为没有该扩展
392
     *
393
     * @param string $key
394
     * @param mixed $value
395
     *
396
     * @return void|mixed
397
     */
398 3
    public static function ini($key, $value = null)
399
    {
400 3
        if (null !== $value) {
401 3
            self::$ini[$key] = $value;
402
        } else {
403 2
            return self::get(self::$ini, $key);
404
        }
405 3
    }
406
407
    /**
408
     * 是否有加载 PHP 扩展
409
     *
410
     * @param string $extName
411
     *
412
     * @return boolean
413
     */
414
    public static function isExt($extName)
415
    {
416
        if (false === extension_loaded($extName) || false === self::ini('EXT_LOADED')) {
417
            return false;
418
        }
419
        return true;
420
    }
421
422
    /**
423
     * 判断当前操作系统是不是 windows
424
     *
425
     * @return boolean
426
     */
427
    public static function isWin()
428
    {
429
        return 'WIN' === strtoupper(substr(PHP_OS, 0, 3));
430
    }
431
}
432