ArrayHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A nextKey() 0 12 3
A prevKey() 0 12 3
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-helpers
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\helpers;
9
10
use yii\helpers\BaseArrayHelper;
11
12
/**
13
 * Extended default Array helper.
14
 *
15
 * @author Vladimir Kuprienko <[email protected]>
16
 * @since 1.0
17
 */
18
class ArrayHelper extends BaseArrayHelper
19
{
20
    /**
21
     * This class should not be instantiate.
22
     */
23
    private function __construct()
24
    {
25
    }
26
27
    /**
28
     * Returns next key of element in array.
29
     *
30
     * @param int|string    $currentKey
31
     * @param array         $array
32
     *
33
     * @return int|string|null Returns null if element with current key was last.
34
     */
35
    public static function nextKey($currentKey, array $array)
36
    {
37
        reset($array);
38
        do {
39
            $tmpKey = key($array);
40
            if (false === next($array)) {
41
                return null;
42
            }
43
        } while ($tmpKey !== $currentKey);
44
45
        return key($array);
46
    }
47
48
    /**
49
     * Returns prev key of element in array.
50
     *
51
     * @param int|string    $currentKey
52
     * @param array         $array
53
     *
54
     * @return int|string|null Returns null if element with current key was first.
55
     */
56
    public static function prevKey($currentKey, array $array)
57
    {
58
        end($array);
59
        do {
60
            $tmpKey = key($array);
61
            if (false === prev($array)) {
62
                return null;
63
            }
64
        } while ($tmpKey !== $currentKey);
65
66
        return key($array);
67
    }
68
}
69