Passed
Push — master ( 617e52...ff4e9f )
by Arthur
35:51
created

instance_without_constructor()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.9666
c 0
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 22
        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 27
        if (Auth::user() !== null) {
37 27
            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 127
        if (!is_string($class)) {
48 87
            $class = get_class($class);
49
        }
50
51 127
        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 71
        if (empty($array)) {
59
            return;
60
        }
61 71
        $randomIndex = random_int(0, count($array) - 1);
62
63 71
        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 127
        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 127
        if (!is_string($class)) {
107
            $class = get_class($class);
108
        }
109
110 127
        $traits = array_flip(class_uses_recursive($class));
111
112 127
        return isset($traits[$trait]);
113
    }
114
}
115
if (!function_exists('array_keys_exists')) {
116
    function array_keys_exists(array $keys, array $arr)
117
    {
118 61
        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 61
        $arrayAssociative = is_associative_array($array);
126 61
        $subsetAssociative = is_associative_array($subset);
127
128 61
        if ($subsetAssociative && $arrayAssociative) {
129 61
            $patched = \array_replace_recursive($array, $subset);
130
131 61
            if ($strict) {
132
                $result = $array === $patched;
133
            } else {
134 61
                $result = $array == $patched;
135
            }
136
137 61
            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 62
        if ([] === $arr) {
157
            return false;
158
        }
159
160 62
        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 127
        if (!is_string($class)) {
168
            $class = get_class($class);
169
        }
170
171
        try {
172 127
            $reflectionClass = new \ReflectionClass($class);
173 127
            $property = $reflectionClass->getProperty($property);
174 127
            $property->setAccessible(true);
175 127
        } catch (ReflectionException $e) {
176 127
            return;
177
        }
178
179 127
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
180
    }
181
}
182
183
if (!function_exists('instance_without_constructor')) {
184
    function instance_without_constructor($class)
185
    {
186 127
        if (!is_string($class)) {
187
            $class = get_class($class);
188
        }
189
190
        try {
191 127
            $reflectionClass = new \ReflectionClass($class);
192 127
        } catch (ReflectionException $e) {
193 127
            return;
194
        }
195 127
        if ($reflectionClass->isAbstract()) {
196 127
            return;
197
        }
198
199 127
        return $reflectionClass->newInstanceWithoutConstructor();
200
    }
201
}
202
203
if (!function_exists('call_class_function')) {
204
    function call_class_function($class, string $methodName)
205
    {
206 127
        return instance_without_constructor($class)->$methodName();
207
    }
208
}
209
210
if (!function_exists('get_class_constants')) {
211
    function get_class_constants($class)
212
    {
213 2
        if (!is_string($class)) {
214
            $class = get_class($class);
215
        }
216
217
        try {
218 2
            $reflectionClass = new \ReflectionClass($class);
219
        } catch (ReflectionException $e) {
220
            return;
221
        }
222
223 2
        return $reflectionClass->getConstants();
224
    }
225
}
226
227
if (!function_exists('split_caps_to_underscore')) {
228
    function split_caps_to_underscore(string $string)
229
    {
230 4
        $splittedString = "";
231 4
        $splittedInCapsName = preg_split('/(?=[A-Z])/', $string);
232 4
        $i = 0;
233 4
        foreach ($splittedInCapsName as $word) {
234 4
            if ($i !== 0)
235 4
                $splittedString = $splittedString . $word . '_';
236 4
            $i++;
237
        }
238
239 4
        return strtolower(rtrim($splittedString, '_'));
240
    }
241
}
242
243