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
Push — master ( 1ba1f4...ea41a5 )
by Alexey
05:37
created

Arr::next()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 8.2222
cc 7
eloc 10
nc 7
nop 3
1
<?php
2
/**
3
 * @author Sergey Glagolev <[email protected]>, Alexey Tatarinov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.share.helpers
8
 */
9
class Arr
10
{
11
  /**
12
   * @param mixed $array
13
   * @param mixed $key
14
   * @param mixed $default
15
   * @param mixed $strict_default_check
16
   *
17
   * @return mixed
18
   */
19
  public static function get($array, $key, $default = null, $strict_default_check = false)
20
  {
21
    if( $strict_default_check )
22
    {
23
      return !empty($array[$key]) ? $array[$key] : $default;
24
    }
25
    else
26
    {
27
      return isset($array[$key]) ? $array[$key] : $default;
28
    }
29
  }
30
31
  /**
32
   * Вырезает ключ из массива
33
   * @static
34
   *
35
   * @param      $array
36
   * @param      $key
37
   * @param null $default
38
   * @param bool $strict_default_check
39
   *
40
   * @return null
41
   */
42
  public static function cut(&$array, $key, $default = null, $strict_default_check = false)
43
  {
44
    if( $strict_default_check )
45
    {
46
      $value = !empty($array[$key]) ? $array[$key] : $default;
47
    }
48
    else
49
    {
50
      $value = isset($array[$key]) ? $array[$key] : $default;
51
    }
52
53
    if( isset($array[$key]) )
54
      unset($array[$key]);
55
56
    return $value;
57
  }
58
59
  /**
60
   * Retrieves multiple keys from an array. If the key does not exist in the
61
   * array, the default value will be added instead.
62
   * Get the values "username", "password" from $_POST
63
   * $auth  = Arr::extract($_POST, array('username', 'password'));
64
   * $param = Arr::extract($_POST, $param);
65
   *
66
   * @param array array to extract keys from
67
   * @param mixed key or list of key names
68
   * @param mixed default value
69
   *
70
   * @return  mixed
71
   */
72
  public static function extract($array, $keys, $default = null)
73
  {
74
    $found = array();
75
76
    if( is_array($keys) )
77
    {
78
      foreach($keys as $key)
79
      {
80
        $found[$key] = isset($array[$key]) ? $array[$key] : $default;
81
      }
82
    }
83
    else
84
    {
85
      $found = isset($array[$keys]) ? $array[$keys] : $default;
86
    }
87
88
    return $found;
89
  }
90
91
  /**
92
   * Возвращает первый элемент массива
93
   *
94
   * @param mixed $array
95
   *
96
   * @return mixed $first_value
97
   */
98
  public static function reset(array $array)
99
  {
100
    $item = reset($array);
101
102
    return $item;
103
  }
104
105
  /**
106
   * Возвращает последний элемент массива
107
   *
108
   * @param mixed $array
109
   *
110
   * @return mixed $last_value
111
   */
112
  public static function end(array $array)
113
  {
114
    $item = end($array);
115
116
    return $item;
117
  }
118
119
  /**
120
   * Проверка массива на наличие ключей
121
   *
122
   * @param array $search
123
   * @param mixed $keys
124
   *
125
   * @return bool
126
   */
127
  public static function keysExists(array $search, $keys)
128
  {
129
    $result = true;
130
131
    if( is_array($keys) )
132
    {
133
      foreach($keys as $value)
134
      {
135
        if( !array_key_exists($value, $search) )
136
        {
137
          return false;
138
        }
139
      }
140
    }
141
    else
142
    {
143
      $result = array_key_exists($keys, $search);
144
    }
145
146
    return $result;
147
  }
148
149
  /**
150
   * Преобразует объект данных в ассоциативный массив
151
   *
152
   * @param $obj
153
   *
154
   * @return array
155
   */
156
  public static function fromObj($obj)
157
  {
158
    if( is_object($obj) )
159
    {
160
      return get_object_vars($obj);
161
    }
162
    else if( is_array($obj) )
163
    {
164
      foreach($obj as $key => $value)
165
      {
166
        $obj[$key] = call_user_func(__METHOD__, $value);
167
      }
168
169
      return $obj;
170
    }
171
    else
172
    {
173
      return $obj;
174
    }
175
  }
176
177
  /**
178
   * Проверим, пересекаются ли два массива
179
   *
180
   * @param mixed $arr1
181
   * @param mixed $arr2
182
   *
183
   * @return mixed
184
   */
185
  public static function isIntersec(array $arr1 = array(), array $arr2 = array())
186
  {
187
    $intersec = array_intersect($arr1, $arr2);
188
189
    return !empty($intersec);
190
  }
191
192
  /**
193
   * Выполняет trim над всеми элементами массива
194
   *
195
   * @param array $array
196
   * @param string $charlist
197
   *
198
   * @return array $array
199
   */
200
  public static function trim($array, $charlist = '')
201
  {
202
    if( is_array($array) )
203
    {
204
      foreach($array as $key => $value)
205
      {
206
        if( !empty($charlist) )
207
        {
208
          $array[$key] = self::trim($value, $charlist);
209
        }
210
        else
211
        {
212
          $array[$key] = self::trim($value);
213
        }
214
      }
215
    }
216
    elseif( is_string($array) )
217
    {
218
      if( !empty($charlist) )
219
      {
220
        $array = trim($array, $charlist);
221
      }
222
      else
223
      {
224
        $array = trim($array);
225
      }
226
    }
227
228
    return $array;
229
  }
230
231
  /**
232
   * @static
233
   *
234
   * @param $glue
235
   * @param array $array
236
   *
237
   * @return mixed
238
   */
239
  public static function implode(array $array, $glue)
240
  {
241
    foreach($array as $key => $value)
242
    {
243
      if( empty($value) )
244
      {
245
        unset($array[$key]);
246
      }
247
    }
248
249
    return preg_replace("/\s+/", " ", implode($glue, $array));
250
  }
251
252
  public static function reflect($array)
253
  {
254
    return array_combine($array, $array);
255
  }
256
257
  /**
258
   * @param mixed $data
259
   *
260
   * @return mixed
261
   */
262
  public static function reduce($data)
263
  {
264
    if( is_array($data) )
265
    {
266
      return self::reset($data);
267
    }
268
    else
269
    {
270
      return $data;
271
    }
272
  }
273
274
  /**
275
   * Объединение ассоциативных массивов.
276
   * В отличие от CMap::mergeArray перекрывает целочисленные ключи, а не увеличивает индекс элементов
277
   *
278
   * @param array $a
279
   * @param array $b
280
   *
281
   * @return array
282
   */
283
  public static function mergeAssoc(array $a, array $b)
0 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $b is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
  {
285
    $args = func_get_args();
286
    $res = array_shift($args);
287
288
    while(!empty($args))
289
    {
290
      $next = array_shift($args);
291
      foreach($next as $k => $v)
292
      {
293
        if( is_array($v) && isset($res[$k]) && is_array($res[$k]) )
294
          $res[$k] = self::mergeAssoc($res[$k], $v);
295
        else
296
          $res[$k] = $v;
297
      }
298
    }
299
300
    return $res;
301
  }
302
303
  /**
304
   * Делит массив на $countOfParts колонок
305
   * @param array $array
306
   * @param int $countOfParts
307
   * @param bool $resetIndex - не использовать ассоциативные ключи, по умолчанию true
308
   * @param bool $flipSort - сортировка элементов по горизонтали, по умолчанию false
309
   *
310
   * @return array
311
   */
312
  public static function divide(array $array, $countOfParts = 2, $resetIndex = true, $flipSort = false)
313
  {
314
    if( !count($array) )
315
      return array();
316
317
    $matrix = self::createMatrix($countOfParts, $array);
318
    $outArray = self::createArrayByMatrix($array, $matrix, $resetIndex, $flipSort);
319
320
    return $outArray;
321
  }
322
323
  /**
324
   * @param $array
325
   * @param $itemKey
326
   * @param $item
327
   */
328
  public static function push(&$array, $itemKey, $item)
329
  {
330
    if( !isset($array[$itemKey]) )
331
    {
332
      $array[$itemKey] = array();
333
    }
334
335
    $array[$itemKey][] = $item;
336
  }
337
338
  /**
339
   * @param $array
340
   * @param $after
341
   * @param $item
342
   * @param $itemKey
343
   */
344
  public static function insertAfter(&$array, $itemKey, $item, $after)
345
  {
346
    $counter = 1;
347
    foreach($array as $key => $value)
348
    {
349
      if( $key === $after )
350
      {
351
        break;
352
      }
353
354
      $counter++;
355
    }
356
357
    $array_head = array_slice($array, 0, $counter, true);
358
    $array_tail = array_slice($array, $counter, null, true);
359
    $array = self::mergeAssoc($array_head, array($itemKey => $item));
360
    $array = self::mergeAssoc($array, $array_tail);
361
  }
362
363
  /**
364
   * @param $array - массив элементы которого нужно отфильтровать
365
   * @param string|array $keys - ключ или массив ключей элементов $array которые нужно сравнить с $value
366
   * @param $value
367
   * @param $condition - OR или AND условие сравнения нескольких ключей
368
   *
369
   * @return array
370
   */
371
  public static function filter($array, $keys, $value, $condition = 'OR')
372
  {
373
    $keys = !is_array($keys) ? array($keys) : $keys;
374
    $condition = strtolower($condition);
375
376
    return array_filter($array, function ($element) use ($keys, $value, $condition)
377
    {
378
      foreach($keys as $key)
379
      {
380
        if( !isset($element[$key]) )
381
          continue;
382
383
        $result = $element[$key] === $value ? true : false;
384
385
        if( $condition == 'or' && $result )
386
          return true;
387
388
        if( $condition == 'and' && !$result )
389
          return false;
390
      }
391
392
      return false;
393
    });
394
  }
395
396
  /**
397
   * @param array $array
398
   *
399
   * @return array
400
   */
401
  public static function flatten(array $array)
402
  {
403
    $result = array();
404
    array_walk_recursive($array, function ($value, $key) use (&$result)
405
    {
406
      $result[$key] = $value;
407
    });
408
409
    return $result;
410
  }
411
412
  /**
413
   * Следующий после $current элемент массива
414
   *
415
   * @param $array
416
   * @param $current
417
   * @param bool $cycle - по кругу
418
   *
419
   * @return mixed|null
420
   */
421
  public static function next($array, $current, $cycle = false)
422
  {
423
    if( empty($array) || count($array) == 1 )
424
      return null;
425
426
    $element = reset($array);
427
428
    while( $element )
429
    {
430
      if( Utils::compareObjects($element, $current) )
431
      {
432
        $next = next($array);
433
        return $cycle && !$next ? reset($array) : $next;
434
      }
435
436
      $element = next($array);
437
    }
438
439
    return null;
440
  }
441
442
  /**
443
   * Предыдущий перед $current элемент массива
444
   * @param $array
445
   * @param $current
446
   * @param bool $cycle - по кругу
447
   *
448
   * @return mixed|null
449
   */
450
  public static function prev($array, $current, $cycle = false)
451
  {
452
    $revers = array_reverse($array);
453
454
    return self::next($revers, $current, $cycle);
455
  }
456
457
  private static function createMatrix($countColumns, $array)
458
  {
459
    $matrix = array();
460
    $countElements = count($array);
461
    $rowsCount = ceil($countElements / $countColumns);
462
463
    for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
464
    {
465
      for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
466
      {
467
        $value = array_shift($array) ? 1 : 0;
468
        $matrix[$columnIndex][$rowIndex] = $value;
469
      }
470
    }
471
472
    return $matrix;
473
  }
474
475
  private static function createArrayByMatrix($array, $matrix, $resetKeys = true, $flipSort = false)
476
  {
477
    if( empty($matrix) || empty($array) )
478
      return array();
479
480
    $countColumns = count($matrix);
481
    $rowsCount = count($matrix[0]);
482
483
    $outArray = array();
484
485
    $arrayProcess = function($columnIndex, $rowIndex) use($matrix, $resetKeys, &$outArray, &$array) {
486
      if( $matrix[$columnIndex][$rowIndex] == 1)
487
      {
488
        reset($array);
489
        $key = key($array);
490
        $value = array_shift($array);
491
        $outArray[$columnIndex][$resetKeys ? $rowIndex : $key] = $value;
492
      }
493
    };
494
495
    if( !$flipSort )
496
    {
497 View Code Duplication
      for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
498
      {
499
        for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
500
        {
501
          $arrayProcess($columnIndex, $rowIndex);
502
        }
503
      }
504
    }
505
    else
506
    {
507 View Code Duplication
      for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
508
      {
509
        for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
510
        {
511
          $arrayProcess($columnIndex, $rowIndex);
512
        }
513
      }
514
    }
515
516
    return $outArray;
517
  }
518
}