Passed
Push — master ( b1742c...4b0d05 )
by Arthur
101:17 queued 94:42
created

call_class_function()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

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 3
cts 6
cp 0.5
crap 4.125
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 27
        if (!is_string($class)) {
30 18
            $class = get_class($class);
31
        }
32
33 27
        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 22
        if (empty($array)) {
41
            return;
42
        }
43 22
        $randomIndex = random_int(0, count($array) - 1);
44
45 22
        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 46
        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 46
        if (!is_string($class)) {
89
            $class = get_class($class);
90
        }
91
92 46
        $traits = array_flip(class_uses_recursive($class));
93
94 46
        return isset($traits[$trait]);
95
    }
96
}
97
if (!function_exists('array_keys_exists')) {
98
    function array_keys_exists(array $keys, array $arr)
99
    {
100 19
        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 19
        $arrayAssociative = is_associative_array($array);
108 19
        $subsetAssociative = is_associative_array($subset);
109
110 19
        if ($subsetAssociative && $arrayAssociative) {
111 19
            $patched = \array_replace_recursive($array, $subset);
112
113 19
            if ($strict) {
114
                $result = $array === $patched;
115
            } else {
116 19
                $result = $array == $patched;
117
            }
118
119 19
            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 20
        if ([] === $arr) {
139
            return false;
140
        }
141
142 20
        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 2
        } catch (ReflectionException $e) {
158 2
            return;
159
        }
160
161 4
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
162
    }
163
}
164
165
if (!function_exists('call_class_function')) {
166
    function call_class_function($class, string $methodName)
167
    {
168 46
        if (!is_string($class)) {
169
            $class = get_class($class);
170
        }
171
172
        try {
173 46
            $reflectionClass = new \ReflectionClass($class);
174
        } catch (ReflectionException $e) {
175
            return;
176
        }
177
178 46
        return $reflectionClass->newInstanceWithoutConstructor()->$methodName();
179
    }
180
}
181
182
if (!function_exists('get_class_constants')) {
183
    function get_class_constants($class)
184
    {
185 46
        if (!is_string($class)) {
186
            $class = get_class($class);
187
        }
188
189
        try {
190 46
            $reflectionClass = new \ReflectionClass($class);
191
        } catch (ReflectionException $e) {
192
            return;
193
        }
194
195 46
        return $reflectionClass->getConstants();
196
    }
197
}
198