ArrayHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 40 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 30
loc 75
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make_sub() 0 14 4
A csplit() 15 15 4
A ksplit() 15 15 4
A getValues() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\helpers;
12
13
class ArrayHelper extends \yii\helpers\ArrayHelper
14
{
15
    public static function make_sub($array, $v_key, $k_key = null)
16
    {
17
        $res = [];
18
        foreach ($array as $k => $v) {
19
            $res[$k][$v_key] = $v;
20
        }
21
        if ($k_key) {
22
            foreach ($array as $k => $v) {
23
                $res[$k][$k_key] = $k;
24
            }
25
        }
26
27
        return $res;
28
    }
29
30
    /**
31
     * Parses data, exploding the string by comma, trying to create array.
32
     *
33
     * @param $string
34
     * @param string $delimiter
35
     * @return array
36
     */
37 View Code Duplication
    public static function csplit($string, $delimiter = ',')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39
        if (is_array($string)) {
40
            return $string;
41
        }
42
        $res = [];
43
        foreach (explode($delimiter, $string) as $k => $v) {
44
            $v = trim($v);
45
            if (strlen($v)) {
46
                array_push($res, $v);
47
            }
48
        }
49
50
        return $res;
51
    }
52
53 View Code Duplication
    public static function ksplit($string, $delimiter = ',')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55
        if (is_array($string)) {
56
            return $string;
57
        }
58
        $res = [];
59
        foreach (explode($delimiter, $string) as $k => $v) {
60
            $v = trim($v);
61
            if (strlen($v)) {
62
                $res[$v] = $v;
63
            }
64
        }
65
66
        return $res;
67
    }
68
69
    /**
70
     * Retrieves the values of an array elements or object properties with the given key or property name.
71
     * Uses [[getValue]] method.
72
     *
73
     * @param array|object $array array or object to extract value from
74
     * @param array [string] $keys keys names of the array elements, or properties of the object
75
     * @return array
76
     * @see getValue()
77
     */
78
    public static function getValues($array, $keys = [])
79
    {
80
        $result = [];
81
        foreach ($keys as $key) {
82
            $result[$key] = static::getValue($array, $key);
83
        }
84
85
        return $result;
86
    }
87
}
88