Completed
Push — master ( 2d9929...91c709 )
by Antonio Carlos
01:45
created

helpers.php ➔ collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
use IlluminateAgnostic\Arr\Support\Arr;
4
use IlluminateAgnostic\Arr\Support\Collection;
5
use IlluminateAgnostic\Arr\Support\Debug\Dumper;
6
7
if (!class_exists(Illuminate\Support\Collection::class)) {
8
    if (!function_exists('collect')) {
9
        /**
10
         * Create a collection from the given value.
11
         *
12
         * @param  mixed  $value
13
         * @return \IlluminateAgnostic\ArrAgnostic\Arr\Support\Collection
14
         */
15
        function collect($value = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
        {
17 5
            return new Collection($value);
18
        }
19
    }
20
21
    if (!function_exists('data_get')) {
22
        /**
23
         * Get an item from an array or object using "dot" notation.
24
         *
25
         * @param  mixed   $target
26
         * @param  string|array  $key
27
         * @param  mixed   $default
28
         * @return mixed
29
         */
30
        function data_get($target, $key, $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
        {
32 98
            if (is_null($key)) {
33 26
                return $target;
34
            }
35
36 86
            $key = is_array($key) ? $key : explode('.', $key);
37
38 86
            while (!is_null($segment = array_shift($key))) {
39 86
                if ($segment === '*') {
40 1
                    if ($target instanceof Collection) {
41
                        $target = $target->all();
42 1
                    } elseif (!is_array($target)) {
43
                        return value($default);
44
                    }
45
46 1
                    $result = Arr::pluck($target, $key);
47
48 1
                    return in_array('*', $key) ? Arr::collapse($result) : $result;
49
                }
50
51 86
                if (Arr::accessible($target) && Arr::exists($target, $segment)) {
52 72
                    $target = $target[$segment];
53 29
                } elseif (is_object($target) && isset($target->{$segment})) {
54 25
                    $target = $target->{$segment};
55
                } else {
56 8
                    return value($default);
57
                }
58
            }
59
60 86
            return $target;
61
        }
62
    }
63
64
    if (!function_exists('data_set')) {
65
        /**
66
         * Set an item on an array or object using dot notation.
67
         *
68
         * @param  mixed  $target
69
         * @param  string|array  $key
70
         * @param  mixed  $value
71
         * @param  bool  $overwrite
72
         * @return mixed
73
         */
74
        function data_set(&$target, $key, $value, $overwrite = true)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
        {
76
            $segments = is_array($key) ? $key : explode('.', $key);
77
78
            if (($segment = array_shift($segments)) === '*') {
79
                if (!Arr::accessible($target)) {
80
                    $target = [];
81
                }
82
83
                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...
84
                    foreach ($target as &$inner) {
85
                        data_set($inner, $segments, $value, $overwrite);
86
                    }
87
                } elseif ($overwrite) {
88
                    foreach ($target as &$inner) {
89
                        $inner = $value;
90
                    }
91
                }
92
            } elseif (Arr::accessible($target)) {
93
                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...
94
                    if (!Arr::exists($target, $segment)) {
95
                        $target[$segment] = [];
96
                    }
97
98
                    data_set($target[$segment], $segments, $value, $overwrite);
99
                } elseif ($overwrite || !Arr::exists($target, $segment)) {
100
                    $target[$segment] = $value;
101
                }
102
            } elseif (is_object($target)) {
103
                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...
104
                    if (!isset($target->{$segment})) {
105
                        $target->{$segment} = [];
106
                    }
107
108
                    data_set($target->{$segment}, $segments, $value, $overwrite);
109
                } elseif ($overwrite || !isset($target->{$segment})) {
110
                    $target->{$segment} = $value;
111
                }
112
            } else {
113
                $target = [];
114
115
                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...
116
                    data_set($target[$segment], $segments, $value, $overwrite);
117
                } elseif ($overwrite) {
118
                    $target[$segment] = $value;
119
                }
120
            }
121
122
            return $target;
123
        }
124
    }
125
126
    if (!function_exists('dd')) {
127
        /**
128
         * Dump the passed variables and end the script.
129
         *
130
         * @param  mixed  $args
131
         * @return void
132
         */
133
        function dd(...$args)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
        {
135
            http_response_code(500);
136
137
            foreach ($args as $x) {
138
                (new Dumper())->dump($x);
139
            }
140
141
            die(1);
142
        }
143
    }
144
145
    if (!function_exists('value')) {
146
        /**
147
         * Return the default value of the given value.
148
         *
149
         * @param  mixed  $value
150
         * @return mixed
151
         */
152
        function value($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
        {
154 31
            return $value instanceof Closure ? $value() : $value;
155
        }
156
    }
157
}
158