Passed
Push — master ( 07ff08...756a96 )
by Arthur
05:20
created

run_class_function()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 12
1
<?php
2
3
4
if (!function_exists('get_authenticated_user_id')) {
5
    function get_authenticated_user_id()
6
    {
7
        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...
8
    }
9
}
10
11
if (!function_exists('get_authenticated_user')) {
12
13
    /**
14
     * @return \Modules\User\Entities\User
15
     */
16
    function get_authenticated_user(): \Illuminate\Contracts\Auth\Authenticatable
17
    {
18
        if (Auth::user() !== null) {
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...
20
        } else {
21
            throw new \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException('no authorized user');
22
        }
23
    }
24
}
25
26
if (!function_exists('get_short_class_name')) {
27
    function get_short_class_name($class)
28
    {
29
        if (!is_string($class)) {
30
            $class = get_class($class);
31
        }
32
33
        return substr(strrchr($class, '\\'), 1);
34
    }
35
}
36
37
if (!function_exists('get_random_array_element')) {
38
    function get_random_array_element(array $array)
39
    {
40
        if (empty($array)) {
41
            return;
42
        }
43
        $randomIndex = random_int(0, count($array) - 1);
44
45
        return $array[$randomIndex];
46
    }
47
}
48
if (!function_exists('create_multiple_from_factory')) {
49
    function create_multiple_from_factory(string $modelClass, $amount = 1, ?string $state = null)
50
    {
51
        if ($amount < 1) {
52
            return false;
53
        }
54
55
        $factory = factory($modelClass, $amount);
56
57
        if ($state !== null) {
58
            $factory->state($state);
59
        }
60
61
        return $factory->raw();
62
    }
63
}
64
65
if (!function_exists('create_from_factory')) {
66
    function create_from_factory(string $modelClass, ?string $state = null)
67
    {
68
        $factory = factory($modelClass);
69
70
        if ($state !== null) {
71
            $factory->state($state);
72
        }
73
74
        return $factory->raw();
75
    }
76
}
77
78
if (!function_exists('class_implements_interface')) {
79
    function class_implements_interface($class, $interface)
80
    {
81
        return in_array($interface, class_implements($class));
82
    }
83
}
84
85
if (!function_exists('class_uses_trait')) {
86
    function class_uses_trait($class, string $trait)
87
    {
88
        if (!is_string($class)) {
89
            $class = get_class($class);
90
        }
91
92
        $traits = array_flip(class_uses_recursive($class));
93
94
        return isset($traits[$trait]);
95
    }
96
}
97
if (!function_exists('array_keys_exists')) {
98
    function array_keys_exists(array $keys, array $arr)
99
    {
100
        return !array_diff_key(array_flip($keys), $arr);
101
    }
102
}
103
104
if (!function_exists('array_is_subset_of')) {
105
    function array_is_subset_of(array $subset, array $array, bool $strict = false)
106
    {
107
        $arrayAssociative = is_associative_array($array);
108
        $subsetAssociative = is_associative_array($subset);
109
110
        if ($subsetAssociative && $arrayAssociative) {
111
            $patched = \array_replace_recursive($array, $subset);
112
113
            if ($strict) {
114
                $result = $array === $patched;
115
            } else {
116
                $result = $array == $patched;
117
            }
118
119
            return $result;
120
        } elseif (($subsetAssociative && !$arrayAssociative) ||
121
            (!$subsetAssociative && $arrayAssociative)) {
122
            return false;
123
        }
124
125
        $result = array_intersect($subset, $array);
126
127
        if ($strict) {
128
            return $result === $subset;
129
        }
130
131
        return $result == $subset;
132
    }
133
}
134
135
if (!function_exists('is_associative_array')) {
136
    function is_associative_array(array $arr)
137
    {
138
        if ([] === $arr) {
139
            return false;
140
        }
141
142
        return array_keys($arr) !== range(0, count($arr) - 1);
143
    }
144
}
145
146
if (!function_exists('get_class_property')) {
147
    function get_class_property($class, string $property)
148
    {
149
        if (!is_string($class)) {
150
            $class = get_class($class);
151
        }
152
153
        try {
154
            $reflectionClass = new \ReflectionClass($class);
155
            $property = $reflectionClass->getProperty($property);
156
            $property->setAccessible(true);
157
        } catch (ReflectionException $e) {
158
            return;
159
        }
160
161
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
162
    }
163
}
164
165
if (!function_exists('run_class_function')) {
166
    function run_class_function($class, string $methodName)
167
    {
168
        if (!is_string($class)) {
169
            $class = get_class($class);
170
        }
171
172
        try {
173
            $reflectionClass = new \ReflectionClass($class);
174
        } catch (ReflectionException $e) {
175
            return;
176
        }
177
178
        return $reflectionClass->newInstanceWithoutConstructor()->$methodName();
179
    }
180
}
181
182
if (!function_exists('get_class_constants')) {
183
    function get_class_constants($class)
184
    {
185
        if (!is_string($class)) {
186
            $class = get_class($class);
187
        }
188
189
        try {
190
            $reflectionClass = new \ReflectionClass($class);
191
        } catch (ReflectionException $e) {
192
            return;
193
        }
194
195
        return $reflectionClass->getConstants();
196
    }
197
}
198