Passed
Push — develop ( 0eb5d9...226eab )
by Andrew
05:27
created

ArrayHelper::strictMerge()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 16
nop 2
dl 0
loc 35
rs 5.2653
c 0
b 0
f 0

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
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use yii\helpers\ReplaceArrayValue;
15
use yii\helpers\UnsetArrayValue;
16
17
/**
18
 * @author    nystudio107
19
 * @package   Seomatic
20
 * @since     3.0.0
21
 */
22
class ArrayHelper extends \craft\helpers\ArrayHelper
23
{
24
    // Public Static Methods
25
    // =========================================================================
26
27
    /**
28
     * This is the same as Yii2's [[merge]] except that it ensures uniqueness of
29
     * non-associative array values
30
     *
31
     * Merges two or more arrays into one recursively.
32
     * If each array has an element with the same string key value, the latter
33
     * will overwrite the former (different from array_merge_recursive).
34
     * Recursive merging will be conducted if both arrays have an element of array
35
     * type and are having the same key.
36
     * For integer-keyed elements, the elements from the latter array will
37
     * be appended to the former array.
38
     * You can use [[UnsetArrayValue]] object to unset value from previous array or
39
     * [[ReplaceArrayValue]] to force replace former value instead of recursive merging.
40
     * @param array $a array to be merged to
41
     * @param array $b array to be merged from. You can specify additional
42
     * arrays via third argument, fourth argument etc.
43
     * @return array the merged array (the original arrays are not changed.)
44
     */
45
    public static function strictMerge($a, $b)
46
    {
47
        $args = func_get_args();
48
        $res = array_shift($args);
49
        while (!empty($args)) {
50
            foreach (array_shift($args) as $k => $v) {
51
                if ($v instanceof UnsetArrayValue) {
52
                    unset($res[$k]);
53
                } elseif ($v instanceof ReplaceArrayValue) {
54
                    $res[$k] = $v->value;
55
                } elseif (is_int($k)) {
56
                    if (array_key_exists($k, $res)) {
57
                        $res[] = $v;
58
                    } else {
59
                        $res[$k] = $v;
60
                    }
61
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
62
                    $res[$k] = self::merge($res[$k], $v);
63
                } else {
64
                    $res[$k] = $v;
65
                }
66
            }
67
        }
68
69
        /**
70
         * If this is a sequential 0-based indexed array, strip out any non-unique
71
         * array values
72
         *
73
         * aaw -- 2018.03.18
74
         */
75
        if (array_keys($res) === range(0, count($res) - 1)) {
76
            $res = array_values(array_unique($res));
77
        }
78
79
        return $res;
80
    }
81
82
    /**
83
     * @param array    $array
84
     * @param callable $callback
85
     *
86
     * @return array
87
     */
88
    public static function arrayFilterRecursive(array $array, callable $callback)
89
    {
90
        foreach ($array as $k => $v) {
91
            if (is_array($v)) {
92
                $array[$k] = self::arrayFilterRecursive($v, $callback);
93
            } else {
94
                if ($callback($v, $k)) {
95
                    unset($array[$k]);
96
                }
97
            }
98
        }
99
100
        return $array;
101
    }
102
103
    /**
104
     * @param $value
105
     * @param $key
106
     *
107
     * @return bool
108
     */
109
    public static function unsetEmptyChildren($value, $key)
1 ignored issue
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

109
    public static function unsetEmptyChildren($value, /** @scrutinizer ignore-unused */ $key)

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

Loading history...
110
    {
111
        if (is_bool($value)) {
112
            return false;
113
        } else {
114
            return empty($value) ? true : false;
115
        }
116
    }
117
118
    /**
119
     * @param $value
120
     * @param $key
121
     *
122
     * @return bool
123
     */
124
    public static function unsetNullChildren($value, $key)
1 ignored issue
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

124
    public static function unsetNullChildren($value, /** @scrutinizer ignore-unused */ $key)

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

Loading history...
125
    {
126
        return $value == null ? true : false;
127
    }
128
}
129