array_is_subset_of()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.648

Importance

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