helpers.php ➔ data_set()   C
last analyzed

Complexity

Conditions 14
Paths 22

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 64.518

Importance

Changes 0
Metric Value
cc 14
eloc 24
nc 22
nop 4
dl 0
loc 38
ccs 8
cts 22
cp 0.3636
crap 64.518
rs 5.0864
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 declare(strict_types=1);
2
3
namespace HSkrasek\OpenAPI;
4
5
/**
6
 * Get an item from an array or object using "dot" notation.
7
 *
8
 * @param  mixed $target
9
 * @param  string|array $key
10
 * @param  mixed $default
11
 *
12
 * @return mixed
13
 *
14
 * @source Laravel (Thanks Taylor!)
15
 */
16
function data_get($target, $key, $default = null)
17
{
18 138
    if (null === $key) {
19
        return $target;
20
    }
21
22 138
    $key = \is_array($key) ? $key : explode('.', $key);
23
24 138
    while (null !== $segment = array_shift($key)) {
25 138
        if (\is_array($target) && array_key_exists($segment, $target)) {
26
            $target = $target[$segment];
27 138
        } elseif (\is_object($target) && isset($target->{$segment})) {
28 138
            $target = $target->{$segment};
29
        } else {
30 138
            return $default;
31
        }
32
    }
33
34 138
    return $target;
35
}
36
37
/**
38
 * Set an item on an array or object using dot notation.
39
 *
40
 * @param  mixed $target
41
 * @param  string|array $key
42
 * @param  mixed $value
43
 * @param  bool $overwrite
44
 *
45
 * @return mixed
46
 *
47
 * @source Laravel (Thanks Taylor!)
48
 */
49
function data_set(&$target, $key, $value, $overwrite = true)
50
{
51 138
    $segments = \is_array($key) ? $key : explode('.', $key);
52
53 138
    $segment = array_shift($segments);
54
55 138
    if (\is_array($target)) {
56
        if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            if (!array_key_exists($segment, $target)) {
58
                $target[$segment] = [];
59
            }
60
61
            data_set($target[$segment], $segments, $value, $overwrite);
62
        } elseif ($overwrite || !array_key_exists($segment, $target)) {
63
            $target[$segment] = $value;
64
        }
65 138
    } elseif (\is_object($target)) {
66 138
        if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
            if (!isset($target->{$segment})) {
68
                $target->{$segment} = [];
69
            }
70
71
            data_set($target->{$segment}, $segments, $value, $overwrite);
72 138
        } elseif ($overwrite || !isset($target->{$segment})) {
73 138
            $target->{$segment} = $value;
74
        }
75
    } else {
76
        $target = [];
77
78
        if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79
            data_set($target[$segment], $segments, $value, $overwrite);
80
        } elseif ($overwrite) {
81
            $target[$segment] = $value;
82
        }
83
    }
84
85 138
    return $target;
86
}
87