Failed Conditions
Pull Request — master (#2)
by Arnold
06:44
created

array_functions.php ➔ array_unset()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 8
eloc 8
c 3
b 0
f 1
nc 7
nop 2
dl 0
loc 14
ccs 12
cts 12
cp 1
crap 8
rs 7.7777
1
<?php
2
3
namespace Jasny;
4
5
/**
6
 * Get items from an array.
7
 * Set default values using [key => value].
8
 *
9
 * <code>
10
 *   list($foo, $bar, $useAll) = extract_keys($_GET, ['foo', 'bar', 'all' => false]);
11
 * </cody>
12
 *
13
 * @param array $array
14
 * @param array $keys
15
 * @return array
16
 */
17
function extract_keys(array $array, array $keys)
0 ignored issues
show
Best Practice introduced by
The function Jasny\extract_keys() has been defined more than once; this definition is ignored, only the first definition in global.php (L15-18) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
18
{
19 1
    $values = [];
20
21 1
    foreach ($keys as $i => $v) {
22 1
        $key = is_int($i) ? $v : $i;
23 1
        $default = is_int($i) ? null : $v;
24
        
25 1
        $values[] = isset($array[$key]) ? $array[$key] : $default;
26 1
    }
27
28 1
    return $values;
29
}
30
31
/**
32
 * Walk through the array and unset an item with the key
33
 *
34
 * @param array        $array  Array with objects or arrays
35
 * @param string|array $key
36
 */
37
function array_unset(array &$array, $key)
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_unset() has been defined more than once; this definition is ignored, only the first definition in global.php (L26-29) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
38
{
39 2
    foreach ($array as &$item) {
40 2
        if (is_object($item)) {
41 1
            foreach ((array)$key as $k) {
42 1
                if (isset($item->$k)) unset($item->$k);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
43 1
            }
44 2
        } elseif (is_array($item)) {
45 1
            foreach ((array)$key as $k) {
46 1
                if (isset($item[$k])) unset($item[$k]);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
47 1
            }
48 1
        }
49 2
    }
50 2
}
51
52
/**
53
 * Return an array with only the specified keys.
54
 *
55
 * @param array $array
56
 * @param array $keys
57
 * @return array
58
 */
59
function array_only(array $array, array $keys)
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_only() has been defined more than once; this definition is ignored, only the first definition in global.php (L38-41) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
60
{
61 1
    $intersect = array_fill_keys($keys, null);
62 1
    return array_intersect_key($array, $intersect);
63
}
64
65
/**
66
 * Return an array without the specified keys.
67
 *
68
 * @param array $array
69
 * @param array $keys
70
 * @return array
71
 */
72
function array_without(array $array, array $keys)
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_without() has been defined more than once; this definition is ignored, only the first definition in global.php (L50-53) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
73
{
74 1
    $intersect = array_fill_keys($keys, null);
75 1
    return array_diff_key($array, $intersect);
76
}
77
78
/**
79
 * Check if an array contains a set of values.
80
 * 
81
 * @param array   $array
82
 * @param array   $subset
83
 * @param boolean $strict  Strict type checking
84
 * @return boolean
85
 */
86
function array_contains(array $array, array $subset, $strict = false)
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_contains() has been defined more than once; this definition is ignored, only the first definition in global.php (L63-66) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
87
{
88 3
    foreach ($subset as $value) {
89 3
        if (!in_array($value, $array, $strict)) {
90 3
            return false;
91
        }
92 3
    }
93
    
94 3
    return true;
95
}
96
97
/**
98
 * Check if an array contains a set of values with index check.
99
 * 
100
 * @param array $array
101
 * @param array $subset
102
 * @param boolean $strict  Strict type checking
103
 * @return boolean
104
 */
105
function array_contains_assoc(array $array, array $subset, $strict = false)
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_contains_assoc() has been defined more than once; this definition is ignored, only the first definition in global.php (L76-79) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
106
{
107
    foreach ($subset as $key => $value) {
108
        if (
109
            !array_key_exists($key, $array) ||
110
            isset($value) !== isset($array[$key]) ||
111
            ($strict ? $value !== $array[$key] : $value != $array[$key])
112
        ) {
113
            return false;
114
        }
115
    }
116
    
117
    return true;
118
}
119
120
/**
121
 * Flatten a nested associative array, concatenating the keys.
122
 * 
123
 * @param array  $array
124
 * @param string $glue
125
 * @return array
126
 */
127
function array_flatten(array $array, $glue = '.')
0 ignored issues
show
Best Practice introduced by
The function Jasny\array_flatten() has been defined more than once; this definition is ignored, only the first definition in global.php (L88-91) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
128
{
129 3
    foreach ($array as $key => &$value) {
130 3
        if (!is_associative_array($value)) {
131 3
            continue;
132
        }
133
134 3
        unset($array[$key]);
135 3
        $value = array_flatten($value, $glue);
136
137 3
        foreach ($value as $subkey => $subvalue) {
138 3
            $array[$key . $glue . $subkey] = $subvalue;
139 3
        }
140 3
    }
141
    
142 3
    return $array;
143
}
144