Passed
Push — master ( c7459e...5c3821 )
by Arthur
22:05
created

split_caps_to_underscore()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
if (!function_exists('get_module_path')) {
4
    function get_module_path(string $module)
5
    {
6
        $module = ucfirst($module);
7
        $path = base_path('src/Modules') . '/' . $module;
8
        if (file_exists($path)) {
9
            return $path;
10
        }
11
        throw new \Foundation\Exceptions\Exception('Module not found', 500);
12
    }
13
}
14
15
if (!function_exists('get_foundation_path')) {
16
    function get_foundation_path()
17
    {
18
        return base_path('src/Foundation');
19
    }
20
}
21
22
if (!function_exists('get_authenticated_user_id')) {
23
    function get_authenticated_user_id()
24
    {
25 14
        return get_authenticated_user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
    }
27
}
28
29
if (!function_exists('get_authenticated_user')) {
30
31
    /**
32
     * @return \Modules\User\Entities\User
33
     */
34
    function get_authenticated_user(): \Illuminate\Contracts\Auth\Authenticatable
35
    {
36 19
        if (Auth::user() !== null) {
37 19
            return Auth::user();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Auth::user() could return the type null which is incompatible with the type-hinted return Illuminate\Contracts\Auth\Authenticatable. Consider adding an additional type-check to rule them out.
Loading history...
38
        } else {
39
            throw new \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException('no authorized user');
40
        }
41
    }
42
}
43
44
if (!function_exists('get_short_class_name')) {
45
    function get_short_class_name($class)
46
    {
47 102
        if (!is_string($class)) {
48 75
            $class = get_class($class);
49
        }
50
51 102
        return substr(strrchr($class, '\\'), 1);
52
    }
53
}
54
55
if (!function_exists('get_random_array_element')) {
56
    function get_random_array_element(array $array)
57
    {
58 64
        if (empty($array)) {
59
            return;
60
        }
61 64
        $randomIndex = random_int(0, count($array) - 1);
62
63 64
        return $array[$randomIndex];
64
    }
65
}
66
if (!function_exists('create_multiple_from_factory')) {
67
    function create_multiple_from_factory(string $modelClass, $amount = 1, ?string $state = null)
68
    {
69
        if ($amount < 1) {
70
            return false;
71
        }
72
73
        $factory = factory($modelClass, $amount);
74
75
        if ($state !== null) {
76
            $factory->state($state);
77
        }
78
79
        return $factory->raw();
80
    }
81
}
82
83
if (!function_exists('create_from_factory')) {
84
    function create_from_factory(string $modelClass, ?string $state = null)
85
    {
86
        $factory = factory($modelClass);
87
88
        if ($state !== null) {
89
            $factory->state($state);
90
        }
91
92
        return $factory->raw();
93
    }
94
}
95
96
if (!function_exists('class_implements_interface')) {
97
    function class_implements_interface($class, $interface)
98
    {
99 102
        return in_array($interface, class_implements($class));
100
    }
101
}
102
103
if (!function_exists('class_uses_trait')) {
104
    function class_uses_trait($class, string $trait)
105
    {
106 102
        if (!is_string($class)) {
107
            $class = get_class($class);
108
        }
109
110 102
        $traits = array_flip(class_uses_recursive($class));
111
112 102
        return isset($traits[$trait]);
113
    }
114
}
115
if (!function_exists('array_keys_exists')) {
116
    function array_keys_exists(array $keys, array $arr)
117
    {
118 56
        return !array_diff_key(array_flip($keys), $arr);
119
    }
120
}
121
122
if (!function_exists('array_is_subset_of')) {
123
    function array_is_subset_of(array $subset, array $array, bool $strict = false)
124
    {
125 56
        $arrayAssociative = is_associative_array($array);
126 56
        $subsetAssociative = is_associative_array($subset);
127
128 56
        if ($subsetAssociative && $arrayAssociative) {
129 56
            $patched = \array_replace_recursive($array, $subset);
130
131 56
            if ($strict) {
132
                $result = $array === $patched;
133
            } else {
134 56
                $result = $array == $patched;
135
            }
136
137 56
            return $result;
138 1
        } elseif (($subsetAssociative && !$arrayAssociative) ||
139 1
            (!$subsetAssociative && $arrayAssociative)) {
140
            return false;
141
        }
142
143 1
        $result = array_intersect($subset, $array);
144
145 1
        if ($strict) {
146
            return $result === $subset;
147
        }
148
149 1
        return $result == $subset;
150
    }
151
}
152
153
if (!function_exists('is_associative_array')) {
154
    function is_associative_array(array $arr)
155
    {
156 57
        if ([] === $arr) {
157
            return false;
158
        }
159
160 57
        return array_keys($arr) !== range(0, count($arr) - 1);
161
    }
162
}
163
164
if (!function_exists('get_class_property')) {
165
    function get_class_property($class, string $property)
166
    {
167 102
        if (!is_string($class)) {
168
            $class = get_class($class);
169
        }
170
171
        try {
172 102
            $reflectionClass = new \ReflectionClass($class);
173 102
            $property = $reflectionClass->getProperty($property);
174 102
            $property->setAccessible(true);
175 102
        } catch (ReflectionException $e) {
176 102
            return;
177
        }
178
179 102
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
180
    }
181
}
182
183
if (!function_exists('instance_without_constructor')) {
184
    function instance_without_constructor($class)
185
    {
186 102
        if (!is_string($class)) {
187
            $class = get_class($class);
188
        }
189
190
        try {
191 102
            $reflectionClass = new \ReflectionClass($class);
192
        } catch (ReflectionException $e) {
193
            return;
194
        }
195 102
        if ($reflectionClass->isAbstract()) {
196 102
            return;
197
        }
198
199 102
        return $reflectionClass->newInstanceWithoutConstructor();
200
    }
201
}
202
203
if (!function_exists('call_class_function')) {
204
    function call_class_function($class, string $methodName)
205
    {
206 102
        return instance_without_constructor($class)->$methodName();
207
    }
208
}
209
210
if (!function_exists('get_class_constants')) {
211
    function get_class_constants($class)
212
    {
213 76
        if (!is_string($class)) {
214
            $class = get_class($class);
215
        }
216
217
        try {
218 76
            $reflectionClass = new \ReflectionClass($class);
219
        } catch (ReflectionException $e) {
220
            return;
221
        }
222
223 76
        return $reflectionClass->getConstants();
224
    }
225
}
226
227
if (!function_exists('split_caps_to_underscore')) {
228
    function split_caps_to_underscore(string $string)
229
    {
230
        $splittedString = "";
231
        $splittedInCapsName = preg_split('/(?=[A-Z])/', $string);
232
        $i = 0;
233
        foreach ($splittedInCapsName as $word) {
234
            if ($i !== 0)
235
                $splittedString = $splittedString . $word . '_';
236
            $i++;
237
        }
238
239
        return strtolower(rtrim($splittedString, '_'));
240
    }
241
}
242
243