Passed
Push — master ( cf340d...4e06a8 )
by Arthur
06:10
created

get_class_property()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
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 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('get_short_class_name')) {
27
    function get_short_class_name($class)
28
    {
29 22
        if (!is_string($class)) {
30 22
            $class = get_class($class);
31
        }
32
33 22
        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 1
        if (empty($array)) {
41
            return;
42
        }
43 1
        $randomIndex = random_int(0, count($array) - 1);
44
45 1
        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 22
        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 22
        if (!is_string($class)) {
89
            $class = get_class($class);
90
        }
91
92 22
        $traits = array_flip(class_uses_recursive($class));
93
94 22
        return isset($traits[$trait]);
95
    }
96
}
97
if (!function_exists('array_keys_exists')) {
98
    function array_keys_exists(array $keys, array $arr)
99
    {
100 1
        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 1
        $arrayAssociative = is_associative_array($array);
108 1
        $subsetAssociative = is_associative_array($subset);
109
110 1
        if ($subsetAssociative && $arrayAssociative) {
111 1
            $patched = \array_replace_recursive($array, $subset);
112
113 1
            if ($strict) {
114
                $result = $array === $patched;
115
            } else {
116 1
                $result = $array == $patched;
117
            }
118 1
            return $result;
119 1
        } elseif (($subsetAssociative && !$arrayAssociative) ||
120 1
            (!$subsetAssociative && $arrayAssociative)) {
121
            return false;
122
        }
123
124 1
        $result = array_intersect($subset, $array);
125
126 1
        if ($strict)
127
            return $result === $subset;
128
129 1
        return $result == $subset;
130
    }
131
}
132
133
if (!function_exists('is_associative_array')) {
134
    function is_associative_array(array $arr)
135
    {
136 2
        if (array() === $arr) return false;
137 2
        return array_keys($arr) !== range(0, count($arr) - 1);
138
    }
139
}
140
141
if (!function_exists('get_class_property')) {
142
    function get_class_property(string $class, string $property)
143
    {
144
        try {
145 3
            $reflectionClass = new \ReflectionClass($class);
146
        } catch (ReflectionException $e) {
147
            return null;
148
        }
149 3
        $property = $reflectionClass->getProperty($property);
150 3
        $property->setAccessible(true);
151 3
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
152
    }
153
}
154
155
156