Passed
Push — master ( 67253e...1a1f73 )
by Arthur
04:53 queued 11s
created

getAuthenticatedUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
if (!function_exists('getAuthenticatedUserId')) {
5
    function getAuthenticatedUserId()
6
    {
7
        return getAuthenticatedUser()->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('getAuthenticatedUser')) {
12
13
    /**
14
     * @return \Modules\User\Entities\User
15
     */
16
    function getAuthenticatedUser(): \Illuminate\Contracts\Auth\Authenticatable
17
    {
18 3
        if (Auth::user() !== null) {
19 2
            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('getShortClassName')) {
27
    function getShortClassName($class)
28
    {
29 17
        if (!is_string($class))
30 17
            $class = get_class($class);
31 17
        return substr(strrchr($class, '\\'), 1);
32
    }
33
}
34
35
if (!function_exists('getRandomArrayElement')) {
36
    function getRandomArrayElement(array $array)
37
    {
38 1
        if (empty($array))
39
            return null;
40 1
        $randomIndex = random_int(0, sizeof($array) - 1);
41 1
        return $array[$randomIndex];
42
    }
43
}
44
if (!function_exists('createArrayFromFactory')) {
45
    function createArrayFromFactory(string $modelClass, $amount = 1, ?string $state = null)
46
    {
47
        if ($amount < 1)
48
            return false;
49
50
        $factory = factory($modelClass, $amount);
51
52
        if ($state !== null)
53
            $factory->state($state);
54
55
        return $factory->raw();
56
    }
57
}
58
59
if (!function_exists('createFromFactory')) {
60
    function createFromFactory(string $modelClass, ?string $state = null)
61
    {
62
        $factory = factory($modelClass);
63
64
        if ($state !== null)
65
            $factory->state($state);
66
67
        return $factory->raw();
68
    }
69
}
70
71
if (!function_exists('classImplementsInterface')) {
72
    function classImplementsInterface($class, $interface)
73
    {
74 17
        return in_array($interface, class_implements($class));
75
    }
76
}
77
78
if (!function_exists('classUsesTrait')) {
79
    function classUsesTrait($class, string $trait)
80
    {
81 1
        if (!is_string($class))
82
            $class = get_class($class);
83
84 1
        $traits = array_flip(class_uses_recursive($class));
85
86 1
        return isset($traits[$trait]);
87
    }
88
}
89
90
91