Completed
Push — master ( 3197d6...49bd80 )
by Ryan
05:46
created

_helpers.php ➔ data()   C

Complexity

Conditions 14
Paths 21

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 21
nc 21
nop 3
dl 0
loc 35
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
2
3
use Anomaly\Streams\Platform\Support\Parser;
4
use Anomaly\Streams\Platform\Support\Template;
5
use Anomaly\Streams\Platform\Support\Value;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
9
if (!function_exists('parse')) {
10
11
    /**
12
     * Parse the target with data.
13
     *
14
     * @param       $target
15
     * @param array $data
16
     * @return mixed    The parsed target.
17
     */
18
    function parse($target, array $data = [])
19
    {
20
        return app(Parser::class)->parse($target, $data);
21
    }
22
}
23
24
if (!function_exists('render')) {
25
26
    /**
27
     * Render the string template.
28
     *
29
     * @param       $template
30
     * @param array $payload
31
     * @return string
32
     */
33
    function render($template, array $payload = [])
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...
34
    {
35
        return app(Template::class)->render($template, $payload);
36
    }
37
}
38
39
if (!function_exists('valuate')) {
40
41
    /**
42
     * Make a valuation.
43
     *
44
     * @param        $parameters
45
     * @param        $entry
46
     * @param string $term
47
     * @param array  $payload
48
     * @return mixed
49
     */
50
    function valuate($parameters, $entry, $term = 'entry', $payload = [])
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...
51
    {
52
        return app(Value::class)->make($parameters, $entry, $term, $payload);
53
    }
54
}
55
56
if (!function_exists('data')) {
57
58
    /**
59
     * Get an item from an array or object using "dot" notation.
60
     *
61
     * @param  mixed        $target
62
     * @param  string|array $key
63
     * @param  mixed        $default
64
     * @return mixed
65
     */
66
    function data($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...
67
    {
68
        if (is_null($key)) {
69
            return $target;
70
        }
71
72
        $key = is_array($key) ? $key : explode('.', $key);
73
74
        while (!is_null($segment = array_shift($key))) {
75
            if ($segment === '*') {
76
                if ($target instanceof Collection) {
77
                    $target = $target->all();
78
                } elseif (!is_array($target)) {
79
                    return value($default);
80
                }
81
82
                $result = Arr::pluck($target, $key);
83
84
                return in_array('*', $key) ? Arr::collapse($result) : $result;
85
            }
86
87
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
88
                $target = $target[$segment];
89
            } elseif (is_object($target) && isset($target->{$segment})) {
90
                $target = $target->{$segment};
91
            } elseif (is_object($target) && method_exists($target, $segment)) {
92
                // This is different than laravel..
93
                $target = $target->{$segment};
94
            } else {
95
                return value($default);
96
            }
97
        }
98
99
        return $target;
100
    }
101
}
102