Passed
Push — master ( a9574a...4c3e72 )
by Arthur
07:41
created

get_authenticated_user_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
if (!function_exists('get_authenticated_user_id')) {
5
    function get_authenticated_user_id()
6
    {
7 2
        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 8
        if (Auth::user() !== null) {
19 7
            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 1
            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 26
        if (!is_string($class)) {
30 17
            $class = get_class($class);
31
        }
32
33 26
        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 21
        if (empty($array)) {
41
            return;
42
        }
43 21
        $randomIndex = random_int(0, count($array) - 1);
44
45 21
        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 41
        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 41
        if (!is_string($class)) {
89
            $class = get_class($class);
90
        }
91
92 41
        $traits = array_flip(class_uses_recursive($class));
93
94 41
        return isset($traits[$trait]);
95
    }
96
}
97
if (!function_exists('array_keys_exists')) {
98
    function array_keys_exists(array $keys, array $arr)
99
    {
100 18
        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 18
        $arrayAssociative = is_associative_array($array);
108 18
        $subsetAssociative = is_associative_array($subset);
109
110 18
        if ($subsetAssociative && $arrayAssociative) {
111 18
            $patched = \array_replace_recursive($array, $subset);
112
113 18
            if ($strict) {
114
                $result = $array === $patched;
115
            } else {
116 18
                $result = $array == $patched;
117
            }
118
119 18
            return $result;
120 1
        } elseif (($subsetAssociative && !$arrayAssociative) ||
121 1
            (!$subsetAssociative && $arrayAssociative)) {
122
            return false;
123
        }
124
125 1
        $result = array_intersect($subset, $array);
126
127 1
        if ($strict) {
128
            return $result === $subset;
129
        }
130
131 1
        return $result == $subset;
132
    }
133
}
134
135
if (!function_exists('is_associative_array')) {
136
    function is_associative_array(array $arr)
137
    {
138 19
        if ([] === $arr) {
139
            return false;
140
        }
141
142 19
        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 4
        if (!is_string($class)) {
150
            $class = get_class($class);
151
        }
152
153
        try {
154 4
            $reflectionClass = new \ReflectionClass($class);
155 4
            $property = $reflectionClass->getProperty($property);
156 4
            $property->setAccessible(true);
157
        } catch (ReflectionException $e) {
158
            return;
159
        }
160
161 4
        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 41
        if (!is_string($class)) {
169
            $class = get_class($class);
170
        }
171
172
        try {
173 41
            $reflectionClass = new \ReflectionClass($class);
174
        } catch (ReflectionException $e) {
175
            return;
176
        }
177
178 41
        return $reflectionClass->newInstanceWithoutConstructor()->$methodName();
179
    }
180
}
181
182
if (!function_exists('get_class_constants')) {
183
    function get_class_constants($class)
184
    {
185 41
        if (!is_string($class)) {
186
            $class = get_class($class);
187
        }
188
189
        try {
190 41
            $reflectionClass = new \ReflectionClass($class);
191
        } catch (ReflectionException $e) {
192
            return;
193
        }
194
195 41
        return $reflectionClass->getConstants();
196
    }
197
}
198