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.

Ini::toArray()   B
last analyzed

Complexity

Conditions 11
Paths 4

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 11
eloc 31
c 5
b 0
f 0
nc 4
nop 0
dl 0
loc 43
ccs 0
cts 40
cp 0
crap 132
rs 7.3166

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Class Ini
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php\ihelpers;
10
11
use icy2003\php\C;
12
use icy2003\php\I;
13
use icy2003\php\icomponents\file\LocalFile;
14
15
/**
16
 * 获取配置数组
17
 * - 支持后缀格式:ini、json、xml
18
 * - ini 规则:
19
 *      1. 所有行会经过两边去白处理
20
 *      2. 英文分号(;),井号(#)开头的会被认为是注释,直接被舍弃
21
 *      3. 配置为键值对形式:[key]=[value],等号两边空白会被舍弃
22
 *      4. 整个值如果被中括号([])包裹,则值会被英文逗号(,)分割为数组
23
 *      4. 如果一整行被中括号([])包裹,则该行被认为是个组,组名是中括号里的字符串
24
 *      5. 分组下的配置如果出现空白行,则重新分组
25
 *      6. 同名配置后者覆盖前者
26
 */
27
class Ini
28
{
29
    /**
30
     * 自动判断
31
     */
32
    const TYPE_AUTO = 'auto';
33
    /**
34
     * 自定义 ini 配置
35
     */
36
    const TYPE_INI = 'ini';
37
    /**
38
     * JSON 配置
39
     */
40
    const TYPE_JSON = 'json';
41
    /**
42
     * XML 配置
43
     */
44
    const TYPE_XML = 'xml';
45
    /**
46
     * 配置类型
47
     *
48
     * @var string
49
     */
50
    protected $_type;
51
    /**
52
     * 文件路径
53
     *
54
     * @var string
55
     */
56
    protected $_file;
57
58
    /**
59
     * 构造函数
60
     *
61
     * @param string $file 文件路径或者别名
62
     * @param string $type
63
     */
64
    public function __construct($file, $type = self::TYPE_AUTO)
65
    {
66
        $this->_file = $file;
67
        if (self::TYPE_AUTO === $type) {
68
            $local = new LocalFile();
69
            $extension = $local->getExtension($file);
70
            switch ($extension) {
71
                case 'json':
72
                    $this->_type = self::TYPE_JSON;
73
                    break;
74
                case 'xml':
75
                    $this->_type = self::TYPE_XML;
76
                    break;
77
                default:
78
                    $this->_type = self::TYPE_INI;
79
            }
80
        } else {
81
            C::assertTrue(in_array($type, ['json', 'xml', 'ini']), '不支持的后缀格式');
82
            $this->_type = $type;
83
        }
84
    }
85
86
    /**
87
     * 转成数组
88
     *
89
     * @return array
90
     */
91
    public function toArray()
92
    {
93
        $local = new LocalFile();
94
        $return = [];
95
        if (self::TYPE_INI === $this->_type) {
96
            $group = null;
97
            foreach ($local->linesGenerator($this->_file, true) as $line) {
98
                $array = [];
99
                $line = trim($line);
100
                // 如果 # 开头认为这是注释
101
                if (in_array(Strings::sub($line, 0, 1), ['#', ';'])) {
102
                    continue;
103
                }
104
                if (Strings::isVariable($line, ['[', ']'])) {
105
                    $group = Strings::sub($line, 1, -1);
106
                    continue;
107
                }
108
                if ('' === $line) {
109
                    $group = null;
110
                    continue;
111
                }
112
                list($name, $value) = Arrays::lists(explode('=', $line), 2, function($row) {
113
                    return trim($row);
114
                });
115
                $array[$name] = $value;
116
                // 如果值被 [] 包括,则以英文逗号为分割,将值变成数组
117
                if (Strings::isStartsWith($value, '[') && Strings::isEndsWith($value, ']')) {
118
                    $array[$name] = explode(',', Strings::sub($value, 1, -1));
119
                }
120
                if (null === $group) {
121
                    $return = Arrays::merge($return, $array);
122
                } else {
123
                    $return[$group] = Arrays::merge((array)I::get($return, $group, []), $array);
124
                }
125
            }
126
        } elseif (self::TYPE_JSON === $this->_type) {
127
            $content = $local->getFileContent($this->_file);
128
            $return = (array)Json::decode((string)$content);
129
        } elseif (self::TYPE_XML === $this->_type) {
130
            $content = $local->getFileContent($this->_file);
131
            $return = Xml::toArray((string)$content);
132
        }
133
        return $return;
134
    }
135
}
136