Passed
Push — master ( e4e2c9...124fdd )
by Gabriel
01:32
created

class_uses_recursive()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 4
nop 1
crap 12
1
<?php
2
3
use Nip\Utility\Arr;
4
5
if (!function_exists('env')) {
6
    /**
7
     * Gets the value of an environment variable.
8
     *
9
     * @param  string $key
10
     * @param  mixed $default
11
     * @return mixed
12
     */
13
    function env($key, $default = null)
14
    {
15
        $value = getenv($key);
16
        if ($value === false) {
17
            return value($default);
18
        }
19
        switch (strtolower($value)) {
20
            case 'true':
21
            case '(true)':
22
                return true;
23
            case 'false':
24
            case '(false)':
25
                return false;
26
            case 'empty':
27
            case '(empty)':
28
                return '';
29
            case 'null':
30
            case '(null)':
31
                return;
32
        }
33
//        if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
34
//            return substr($value, 1, -1);
35
//        }
36
        return $value;
37
    }
38
}
39
40
if (!function_exists('value')) {
41
    /**
42
     * Return the default value of the given value.
43
     *
44
     * @param mixed $value
45
     * @return mixed
46
     */
47
    function value($value)
48
    {
49 1
        return $value instanceof Closure ? $value() : $value;
50
    }
51
}
52
53
if (! function_exists('data_get')) {
54
    /**
55
     * Get an item from an array or object using "dot" notation.
56
     *
57
     * @param  mixed   $target
58
     * @param  string|array|int  $key
59
     * @param  mixed   $default
60
     * @return mixed
61
     */
62
    function data_get($target, $key, $default = null)
63
    {
64 1
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
65
            return $target;
66
        }
67 1
        $key = is_array($key) ? $key : explode('.', $key);
68
69 1
        while (! is_null($segment = array_shift($key))) {
70 1
            if ($segment === '*') {
71
                if (is_object($target) && method_exists($target, 'all')) {
72
                    $target = $target->all();
73
                } elseif (! is_array($target)) {
74
                    return value($default);
75
                }
76
                $result = [];
77
                foreach ($target as $item) {
78
                    $result[] = data_get($item, $key);
79
                }
80
                return in_array('*', $key) ? Arr::collapse($result) : $result;
81
            }
82 1
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
83 1
                $target = $target[$segment];
84 1
            } elseif (is_object($target) && isset($target->{$segment})) {
85
                $target = $target->{$segment};
86
            } else {
87 1
                return value($default);
88
            }
89
        }
90 1
        return $target;
91
    }
92
}
93
94
if (!function_exists('class_uses_recursive')) {
95
    /**
96
     * Returns all traits used by a class, its parent classes and trait of their traits.
97
     *
98
     * @param object|string $class
99
     * @return array
100
     */
101
    function class_uses_recursive($class)
102
    {
103
        if (is_object($class)) {
104
            $class = get_class($class);
105
        }
106
107
        $results = [];
108
109
        foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
110
            $results += trait_uses_recursive($class);
111
        }
112
113
        return array_unique($results);
114
    }
115
}
116
117
if (!function_exists('trait_uses_recursive')) {
118
    /**
119
     * Returns all traits used by a trait and its traits.
120
     *
121
     * @param string $trait
122
     * @return array
123
     */
124
    function trait_uses_recursive($trait)
125
    {
126
        $traits = class_uses($trait);
127
128
        foreach ($traits as $trait) {
0 ignored issues
show
introduced by
$trait is overwriting one of the parameters of this function.
Loading history...
129
            $traits += trait_uses_recursive($trait);
130
        }
131
132
        return $traits;
133
    }
134
}