Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Failed
Pull Request — master (#3113)
by Cristian
25:27 queued 10:27
created

backpack_form_input()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 22
c 2
b 1
f 0
nc 13
nop 0
dl 0
loc 34
rs 8.6346
1
<?php
2
3
if (! function_exists('backpack_url')) {
4
    /**
5
     * Appends the configured backpack prefix and returns
6
     * the URL using the standard Laravel helpers.
7
     *
8
     * @param $path
9
     *
10
     * @return string
11
     */
12
    function backpack_url($path = null, $parameters = [], $secure = null)
13
    {
14
        $path = ! $path || (substr($path, 0, 1) == '/') ? $path : '/'.$path;
15
16
        return url(config('backpack.base.route_prefix', 'admin').$path, $parameters, $secure);
17
    }
18
}
19
20
if (! function_exists('backpack_authentication_column')) {
21
    /**
22
     * Return the username column name.
23
     * The Laravel default (and Backpack default) is 'email'.
24
     *
25
     * @return string
26
     */
27
    function backpack_authentication_column()
28
    {
29
        return config('backpack.base.authentication_column', 'email');
30
    }
31
}
32
33
if (! function_exists('backpack_form_input')) {
34
    /**
35
     * Parse the submitted input in request('form') to an usable array.
36
     * Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones.
37
     *
38
     *
39
     * @return array
40
     */
41
    function backpack_form_input()
42
    {
43
        $input = request('form');
44
45
        $result = [];
46
        foreach ($input as $row) {
47
            // parse the input name to extract the "arg" when using HasOne/MorphOne (address[street]) returns street as arg, address as key
48
            $start = strpos($row['name'], '[');
49
            $input_arg = null;
50
            if ($start !== false) {
51
                $end = strpos($row['name'], ']', $start + 1);
52
                $length = $end - $start;
53
54
                $input_arg = substr($row['name'], $start + 1, $length - 1);
55
                $input_arg = strlen($input_arg) >= 1 ? $input_arg : null;
56
                $input_key = substr($row['name'], 0, $start);
57
            } else {
58
                $input_key = $row['name'];
59
            }
60
61
            if (is_null($input_arg)) {
62
                if (! isset($result[$input_key])) {
63
                    $result[$input_key] = $row['value'];
64
                } else {
65
                    $result[$input_key] = ! is_array($result[$input_key]) ? [$result[$input_key]] : $result[$input_key];
66
67
                    array_push($result[$input_key], $row['value']);
68
                }
69
            } else {
70
                $result[$input_key][$input_arg] = $row['value'];
71
            }
72
        }
73
74
        return $result;
75
    }
76
}
77
78
if (! function_exists('backpack_users_have_email')) {
79
    /**
80
     * Check if the email column is present on the user table.
81
     *
82
     * @return string
83
     */
84
    function backpack_users_have_email()
85
    {
86
        $user_model_fqn = config('backpack.base.user_model_fqn');
87
        $user = new $user_model_fqn();
88
89
        return \Schema::hasColumn($user->getTable(), 'email');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Schema::hasColumn...r->getTable(), 'email') returns the type boolean which is incompatible with the documented return type string.
Loading history...
90
    }
91
}
92
93
if (! function_exists('backpack_avatar_url')) {
94
    /**
95
     * Returns the avatar URL of a user.
96
     *
97
     * @param $user
98
     *
99
     * @return string
100
     */
101
    function backpack_avatar_url($user)
102
    {
103
        $firstLetter = $user->getAttribute('name') ? mb_substr($user->name, 0, 1, 'UTF-8') : 'A';
104
        $placeholder = 'https://placehold.it/160x160/00a65a/ffffff/&text='.$firstLetter;
105
106
        switch (config('backpack.base.avatar_type')) {
107
            case 'gravatar':
108
                if (backpack_users_have_email()) {
109
                    return Gravatar::fallback('https://placehold.it/160x160/00a65a/ffffff/&text='.$firstLetter)->get($user->email);
0 ignored issues
show
Bug introduced by
The type Gravatar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
110
                } else {
111
                    return $placeholder;
112
                }
113
                break;
114
115
            case 'placehold':
116
                return $placeholder;
117
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
118
119
            default:
120
                return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')};
121
                break;
122
        }
123
    }
124
}
125
126
if (! function_exists('backpack_middleware')) {
127
    /**
128
     * Return the key of the middleware used across Backpack.
129
     * That middleware checks if the visitor is an admin.
130
     *
131
     * @param $path
132
     *
133
     * @return string
134
     */
135
    function backpack_middleware()
136
    {
137
        return config('backpack.base.middleware_key', 'admin');
138
    }
139
}
140
141
if (! function_exists('backpack_guard_name')) {
142
    /*
143
     * Returns the name of the guard defined
144
     * by the application config
145
     */
146
    function backpack_guard_name()
147
    {
148
        return config('backpack.base.guard', config('auth.defaults.guard'));
149
    }
150
}
151
152
if (! function_exists('backpack_auth')) {
153
    /*
154
     * Returns the user instance if it exists
155
     * of the currently authenticated admin
156
     * based off the defined guard.
157
     */
158
    function backpack_auth()
159
    {
160
        return \Auth::guard(backpack_guard_name());
161
    }
162
}
163
164
if (! function_exists('backpack_user')) {
165
    /*
166
     * Returns back a user instance without
167
     * the admin guard, however allows you
168
     * to pass in a custom guard if you like.
169
     */
170
    function backpack_user()
171
    {
172
        return backpack_auth()->user();
173
    }
174
}
175
176
if (! function_exists('mb_ucfirst')) {
177
    /**
178
     * Capitalize the first letter of a string,
179
     * even if that string is multi-byte (non-latin alphabet).
180
     *
181
     * @param string   $string   String to have its first letter capitalized.
182
     * @param encoding $encoding Character encoding
0 ignored issues
show
Bug introduced by
The type encoding was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
     *
184
     * @return string String with first letter capitalized.
185
     */
186
    function mb_ucfirst($string, $encoding = false)
187
    {
188
        $encoding = $encoding ? $encoding : mb_internal_encoding();
189
190
        $strlen = mb_strlen($string, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of mb_strlen() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

190
        $strlen = mb_strlen($string, /** @scrutinizer ignore-type */ $encoding);
Loading history...
191
        $firstChar = mb_substr($string, 0, 1, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of mb_substr() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
        $firstChar = mb_substr($string, 0, 1, /** @scrutinizer ignore-type */ $encoding);
Loading history...
192
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
193
194
        return mb_strtoupper($firstChar, $encoding).$then;
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of mb_strtoupper() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
        return mb_strtoupper($firstChar, /** @scrutinizer ignore-type */ $encoding).$then;
Loading history...
195
    }
196
}
197
198
if (! function_exists('backpack_view')) {
199
    /**
200
     * Returns a new displayable view based on the configured backpack view namespace.
201
     * If that view doesn't exist, it will load the one from the original theme.
202
     *
203
     * @param string (see config/backpack/base.php)
0 ignored issues
show
Documentation Bug introduced by
The doc comment (see at position 1 could not be parsed: Expected ')' at position 1, but found 'see'.
Loading history...
204
     *
205
     * @return string
206
     */
207
    function backpack_view($view)
208
    {
209
        $originalTheme = 'backpack::';
210
        $theme = config('backpack.base.view_namespace');
211
212
        if (is_null($theme)) {
213
            $theme = $originalTheme;
214
        }
215
216
        $returnView = $theme.$view;
217
218
        if (! view()->exists($returnView)) {
219
            $returnView = $originalTheme.$view;
220
        }
221
222
        return $returnView;
223
    }
224
}
225
226
if (! function_exists('square_brackets_to_dots')) {
227
    /**
228
     * Turns a string from bracket-type array to dot-notation array.
229
     * Ex: array[0][property] turns into array.0.property.
230
     *
231
     * @param $path
232
     *
233
     * @return string
234
     */
235
    function square_brackets_to_dots($string)
236
    {
237
        $string = str_replace(['[', ']'], ['.', ''], $string);
238
239
        return $string;
240
    }
241
}
242
243
if (! function_exists('is_countable')) {
244
    /**
245
     * We need this because is_countable was only introduced in PHP 7.3,
246
     * and in PHP 7.2 you should check if count() argument is really countable.
247
     * This function may be removed in future if PHP >= 7.3 becomes a requirement.
248
     *
249
     * @param $obj
250
     *
251
     * @return bool
252
     */
253
    function is_countable($obj)
254
    {
255
        return is_array($obj) || $obj instanceof Countable;
256
    }
257
}
258