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.
Completed
Pull Request — master (#5)
by t
63:46
created

ArraysTrait::exceptedKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Class Arrays
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use icy2003\php\I;
13
14
/**
15
 * Arrays 扩展函数
16
 */
17
trait ArraysTrait
18
{
19
20
    /**
21
     * 获取指定某些键的项的值
22
     *
23
     * @param array $array
24
     * @param array|string $keys 数组或逗号字符串
25
     *
26
     * @return array
27
     *
28
     * @tested
29
     */
30
    public static function values($array, $keys = null)
31
    {
32
        return array_values(self::some($array, $keys));
33
    }
34
35
    /**
36
     * 获取指定某些键的项
37
     *
38
     * @param array $array
39
     * @param array|string $keys 数组或都好字符串
40
     *
41
     * @return array
42
     *
43
     * @tested
44
     */
45
    public static function some($array, $keys = null)
46
    {
47
        if (null === $keys) {
48
            return $array;
49
        }
50
        $keys = Strings::toArray($keys);
51
        return array_intersect_key($array, array_flip($keys));
52
    }
53
54
    /**
55
     * 获取指定除了某些键的项
56
     *
57
     * @param array $array
58
     * @param array|string $keys
59
     *
60
     * @return array
61
     *
62
     * @tested
63
     */
64
    public static function exceptedKeys($array, $keys)
65
    {
66
        $keys = Strings::toArray($keys);
67
        return array_diff_key($array, array_flip($keys));
68
    }
69
70
    /**
71
     * 检查数组里是否有指定的所有键名或索引
72
     *
73
     * - array_key_exists:检测一个指定的键
74
     * - Arrays::keyExistsOne:检测数组里是否存在指定的某些键
75
     *
76
     * @param array $keys 要检查的键
77
     * @param array $array
78
     * @param array $diff 引用返回不包含的键
79
     *
80
     * @return boolean
81
     *
82
     * @test icy2003\php_tests\ihelpers\ArraysTest::testKeyExistsAll
83
     */
84
    public static function keyExistsAll($keys, $array, &$diff = null)
85
    {
86
87
        return I::isEmpty($diff = array_diff($keys, array_keys($array)));
88
    }
89
90
    /**
91
     * 检查数组里是否有指定的某些键名或索引
92
     *
93
     * @param array $keys 要检查的键
94
     * @param array $array
95
     * @param array $find 引用返回包含的键
96
     *
97
     * @return boolean
98
     */
99
    public static function keyExistsSome($keys, $array, &$find = null)
100
    {
101
        return !I::isEmpty($find = array_intersect($keys, array_keys($array)));
102
    }
103
104
    /**
105
     * 检查数组里是否有指定的所有值
106
     *
107
     * @param array $values 要检查的值
108
     * @param array $array
109
     * @param array $diff 引用返回不包含的值
110
     *
111
     * @return boolean
112
     */
113
    public static function valueExistsAll($values, $array, &$diff = null)
114
    {
115
        return I::isEmpty($diff = array_diff($values, array_values($array)));
116
    }
117
118
    /**
119
     * 检查数组里是否有指定的某些值
120
     *
121
     * @param array $values 要检查的值
122
     * @param array $array
123
     * @param array $find 引用返回包含的值
124
     *
125
     * @return boolean
126
     */
127
    public static function valueExistsSome($values, $array, &$find = null)
128
    {
129
        return !I::isEmpty($find = array_intersect($values, array_values($array)));
130
    }
131
132
    /**
133
     * 参照 PHP 的 array_combine 函数,array_combine 得到的是一行记录的格式,该函数得到多行
134
     *
135
     * - arrays 里的每个数组会和 keys 使用 self::combine 合并,最终合并成为一个二维数组
136
     *
137
     * @param array $keys 作为键的字段
138
     * @param array $arrays
139
     *
140
     * @return array
141
     *
142
     * @test icy2003\php_tests\ihelpers\ArraysTest::testCombines
143
     */
144
    public static function combines($keys, $arrays)
145
    {
146
        $result = [];
147
        foreach ($arrays as $k => $array) {
148
            $result[$k] = self::combine($keys, $array);
0 ignored issues
show
Bug introduced by
The method combine() does not exist on icy2003\php\ihelpers\ArraysTrait. Did you maybe mean combines()? ( Ignorable by Annotation )

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

148
            /** @scrutinizer ignore-call */ 
149
            $result[$k] = self::combine($keys, $array);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
        }
150
151
        return $result;
152
    }
153
154
    /**
155
     * 把数组里逗号字符串拆分,并且去掉重复的部分
156
     *
157
     * @param array $array
158
     *
159
     * @return array
160
     *
161
     * @test icy2003\php_tests\ihelpers\ArraysTest::testToPart
162
     */
163
    public static function toPart($array)
164
    {
165
        return array_values(
166
            array_filter(
167
                array_keys(
168
                    array_flip(
169
                        explode(',', implode(',', $array))
170
                    )
171
                )
172
            )
173
        );
174
    }
175
176
    /**
177
     * 矩阵转置
178
     *
179
     * @param array $array 待转置的矩阵
180
     *
181
     * @return array
182
     *
183
     * @test icy2003\php_tests\ihelpers\ArraysTest::testTransposed
184
     */
185
    public static function transposed($array)
186
    {
187
        $data = [];
188
        foreach ($array as $r => $row) {
189
            foreach ($row as $c => $col) {
190
                $data[$c][$r] = $col;
191
            }
192
        }
193
        return $data;
194
    }
195
196
    /**
197
     * 普通二维数组转化成 Excel 单元格二维数组
198
     *
199
     * @param array $array
200
     *
201
     * @return array
202
     *
203
     * @test icy2003\php_tests\ihelpers\ArraysTest::testToCellArray
204
     */
205
    public static function toCellArray($array)
206
    {
207
        $data = [];
208
        $rowIndex = 0;
209
        foreach ($array as $row) {
210
            $rowIndex++;
211
            $colIndex = 'A';
212
            foreach ($row as $col) {
213
                $data[$rowIndex][$colIndex++] = $col;
214
            }
215
        }
216
        return $data;
217
    }
218
219
    /**
220
     * 返回矩阵的列数和行数
221
     *
222
     * - 返回两个元素的一维数组,第一个元素表示矩阵的列数,第二个元素表示矩阵的行数
223
     *
224
     * @param array $array
225
     *
226
     * @return array
227
     */
228
    public static function colRowCount($array)
229
    {
230
        return [self::count(self::first($array)), self::count($array)];
231
    }
232
233
    /**
234
     * 用给定的值填充数组
235
     *
236
     * - array_fill:第一参数在为负的时候,生成的数组的第二个元素是从 0 开始的!
237
     *
238
     * @param int $startIndex 返回的数组的第一个索引值
239
     * @param int $num 插入元素的数量。如果为 0 或者负数,则返回空数组
240
     * @param mixed $value 用来填充的值
241
     *
242
     * @return array
243
     */
244
    public static function fill($startIndex, $num, $value)
245
    {
246
        if ($num <= 0) {
247
            return [];
248
        }
249
        $array = [];
250
        foreach (self::rangeGenerator($startIndex, $startIndex + $num - 1) as $key) {
251
            $array[$key] = $value;
252
        }
253
        return $array;
254
    }
255
256
    /**
257
     * 让 var_export 返回 `[]` 的格式
258
     *
259
     * @param mixed $expression 变量
260
     * @param bool $return 默认值 为 true,即返回字符串而不是输出
261
     *
262
     * @return mixed
263
     */
264
    public static function export($expression, $return = true)
265
    {
266
        $export = var_export($expression, true);
267
        $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
268
        $array = preg_split("/\r\n|\n|\r/", $export);
269
        $array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array);
270
        $export = implode(PHP_EOL, array_filter(["["] + $array));
271
        if (true === $return) {
272
            return $export;
273
        } else {
274
            echo $export;
275
        }
276
    }
277
278
    /**
279
     * 将 CSV 文本转成数组
280
     *
281
     * @param string $csvString
282
     *
283
     * @return array
284
     */
285
    public static function fromCsv($csvString)
286
    {
287
        $lines = explode(PHP_EOL, $csvString);
288
        $array = [];
289
        foreach ($lines as $line) {
290
            $array[] = explode(',', $line);
291
        }
292
        return $array;
293
    }
294
295
    /**
296
     * 在数组中搜索给定的值,如果成功则返回首个相应的键名
297
     *
298
     * - 第一参数如果是布尔值,则此方法等同于 array_search
299
     * - 第一参数如果是回调函数,则找到的条件为:回调值为 true
300
     * - 第三参数如果是 false,则回调值只需要 true 值即可(例如:1)
301
     *
302
     * @param mixed|callback $search 搜索的值
303
     * @param array $array 这个数组
304
     * @param boolean $isStrict 是否检查完全相同的元素
305
     *
306
     * @return mixed
307
     */
308
    public static function search($search, $array, $isStrict = false)
309
    {
310
        if (false === is_callable($search)) {
311
            return array_search($search, $array, $isStrict);
312
        }
313
        foreach ($array as $key => $row) {
314
            $result = I::trigger($search, [$row]);
315
            if (true === $isStrict && true === $result || false === $isStrict && true == $result) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (true === $isStrict && t...rict && true == $result, Probably Intended Meaning: true === $isStrict && (t...ict && true == $result)
Loading history...
316
                return $key;
317
            }
318
        }
319
        return false;
320
    }
321
322
    /**
323
     * 递增数组的一个值并返回
324
     *
325
     * - 如果该值不存在,则默认为 0
326
     *
327
     * @param array $array 引用返回数组
328
     * @param string $key
329
     * @param integer $step 步长,默认 1
330
     *
331
     * @return double|integer
332
     */
333
    public function increment(&$array, $key, $step = 1)
334
    {
335
        $array[$key] = I::get($array, $key, 0) + $step;
336
        return $array[$key];
337
    }
338
339
    /**
340
     * 递减数组的一个值并返回
341
     *
342
     * - 如果该值不存在,则默认为 0
343
     *
344
     * @param array $array 引用返回数组
345
     * @param string $key
346
     * @param integer $step 步长,默认 1
347
     *
348
     * @return double|integer
349
     */
350
    public function decrement($array, $key, $step = 1)
351
    {
352
        $array[$key] = I::get($array, $key, 0) - $step;
353
        return $array[$key];
354
    }
355
}
356