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

Passed
Push — CrudButton-fixees ( 8e21aa...0a7a26 )
by Pedro
36:42 queued 21:25
created

backpack_form_input()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 34
c 2
b 1
f 0
nc 11
nop 0
dl 0
loc 62
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @return string
10
     */
11
    function backpack_url($path = null, $parameters = [], $secure = null)
12
    {
13
        $path = ! $path || (substr($path, 0, 1) == '/') ? $path : '/'.$path;
14
15
        return url(config('backpack.base.route_prefix', 'admin').$path, $parameters, $secure);
16
    }
17
}
18
19
if (! function_exists('backpack_authentication_column')) {
20
    /**
21
     * Return the username column name.
22
     * The Laravel default (and Backpack default) is 'email'.
23
     *
24
     * @return string
25
     */
26
    function backpack_authentication_column()
27
    {
28
        return config('backpack.base.authentication_column', 'email');
29
    }
30
}
31
32
if (! function_exists('backpack_email_column')) {
33
    /**
34
     * Return the email column name.
35
     * The Laravel default (and Backpack default) is 'email'.
36
     *
37
     * @return string
38
     */
39
    function backpack_email_column()
40
    {
41
        return config('backpack.base.email_column', 'email');
42
    }
43
}
44
45
if (! function_exists('backpack_form_input')) {
46
    /**
47
     * Parse the submitted input in request('form') to an usable array.
48
     * Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones.
49
     *
50
     *
51
     * @return array
52
     */
53
    function backpack_form_input()
54
    {
55
        $input = request('form') ?? [];
56
        $result = [];
57
58
        foreach ($input as $row) {
59
            $repeatableRowKey = null;
60
61
            // regular fields don't need any aditional parsing
62
            if (strpos($row['name'], '[') === false) {
63
                $result[$row['name']] = $row['value'];
64
65
                continue;
66
            }
67
68
            $isMultiple = substr($row['name'], -2, 2) === '[]';
69
70
            if ($isMultiple && substr_count($row['name'], '[') === 1) {
71
                $result[substr($row['name'], 0, -2)][] = $row['value'];
72
                continue;
73
            }
74
75
            // dot notation fields
76
            if (substr_count($row['name'], '[') === 1) {
77
                // start in the first occurence since it's HasOne/MorphOne with dot notation (address[street] in request) to get the input name (address)
78
                $inputNameStart = strpos($row['name'], '[') + 1;
79
            } else {
80
                // repeatable fields, we need to get the input name and the row number
81
                // start on the second occurence since it's a repeatable and we want to bypass the row number (repeatableName[rowNumber][inputName])
82
                $inputNameStart = strpos($row['name'], '[', strpos($row['name'], '[') + 1) + 1;
83
84
                // get the array key (aka repeatable row) from field name
85
                $startKey = strpos($row['name'], '[') + 1;
86
                $endKey = strpos($row['name'], ']', $startKey);
87
                $lengthKey = $endKey - $startKey;
88
                $repeatableRowKey = substr($row['name'], $startKey, $lengthKey);
89
            }
90
91
            $inputNameEnd = strpos($row['name'], ']', $inputNameStart);
92
            $inputNameLength = $inputNameEnd - $inputNameStart;
93
            $inputName = substr($row['name'], $inputNameStart, $inputNameLength);
94
            $parentInputName = substr($row['name'], 0, strpos($row['name'], '['));
95
96
            if (isset($repeatableRowKey)) {
97
                if ($isMultiple) {
98
                    $result[$parentInputName][$repeatableRowKey][$inputName][] = $row['value'];
99
                    continue;
100
                }
101
102
                $result[$parentInputName][$repeatableRowKey][$inputName] = $row['value'];
103
104
                continue;
105
            }
106
107
            if ($isMultiple) {
108
                $result[$parentInputName][$inputName][] = $row['value'];
109
                continue;
110
            }
111
            $result[$parentInputName][$inputName] = $row['value'];
112
        }
113
114
        return $result;
115
    }
116
}
117
118
if (! function_exists('backpack_users_have_email')) {
119
    /**
120
     * Check if the email column is present on the user table.
121
     *
122
     * @return string
123
     */
124
    function backpack_users_have_email()
125
    {
126
        $user_model_fqn = config('backpack.base.user_model_fqn');
127
        $user = new $user_model_fqn();
128
129
        return \Schema::hasColumn($user->getTable(), config('backpack.base.email_column') ?? 'email');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Schema::hasColumn...il_column') ?? 'email') returns the type boolean which is incompatible with the documented return type string.
Loading history...
130
    }
131
}
132
133
if (! function_exists('backpack_avatar_url')) {
134
    /**
135
     * Returns the avatar URL of a user.
136
     *
137
     * @param $user
138
     * @return string
139
     */
140
    function backpack_avatar_url($user)
141
    {
142
        switch (config('backpack.base.avatar_type')) {
143
            case 'gravatar':
144
                if (backpack_users_have_email() && ! empty($user->email)) {
145
                    return Gravatar::fallback(config('backpack.base.gravatar_fallback'))->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...
146
                }
147
                break;
148
            default:
149
                return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')};
150
                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...
151
        }
152
    }
153
}
154
155
if (! function_exists('backpack_middleware')) {
156
    /**
157
     * Return the key of the middleware used across Backpack.
158
     * That middleware checks if the visitor is an admin.
159
     *
160
     * @param $path
161
     * @return string
162
     */
163
    function backpack_middleware()
164
    {
165
        return config('backpack.base.middleware_key', 'admin');
166
    }
167
}
168
169
if (! function_exists('backpack_guard_name')) {
170
    /*
171
     * Returns the name of the guard defined
172
     * by the application config
173
     */
174
    function backpack_guard_name()
175
    {
176
        return config('backpack.base.guard', config('auth.defaults.guard'));
177
    }
178
}
179
180
if (! function_exists('backpack_auth')) {
181
    /*
182
     * Returns the user instance if it exists
183
     * of the currently authenticated admin
184
     * based off the defined guard.
185
     */
186
    function backpack_auth()
187
    {
188
        return \Auth::guard(backpack_guard_name());
189
    }
190
}
191
192
if (! function_exists('backpack_user')) {
193
    /*
194
     * Returns back a user instance without
195
     * the admin guard, however allows you
196
     * to pass in a custom guard if you like.
197
     */
198
    function backpack_user()
199
    {
200
        return backpack_auth()->user();
201
    }
202
}
203
204
if (! function_exists('mb_ucfirst')) {
205
    /**
206
     * Capitalize the first letter of a string,
207
     * even if that string is multi-byte (non-latin alphabet).
208
     *
209
     * @param  string  $string  String to have its first letter capitalized.
210
     * @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...
211
     * @return string String with first letter capitalized.
212
     */
213
    function mb_ucfirst($string, $encoding = false)
214
    {
215
        $encoding = $encoding ? $encoding : mb_internal_encoding();
216
217
        $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

217
        $strlen = mb_strlen($string, /** @scrutinizer ignore-type */ $encoding);
Loading history...
218
        $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

218
        $firstChar = mb_substr($string, 0, 1, /** @scrutinizer ignore-type */ $encoding);
Loading history...
219
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
220
221
        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

221
        return mb_strtoupper($firstChar, /** @scrutinizer ignore-type */ $encoding).$then;
Loading history...
222
    }
223
}
224
225
if (! function_exists('backpack_view')) {
226
    /**
227
     * Returns a new displayable view based on the configured backpack view namespace.
228
     * If that view doesn't exist, it will load the one from the original theme.
229
     *
230
     * @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...
231
     * @return string
232
     */
233
    function backpack_view($view)
234
    {
235
        $originalTheme = 'backpack::';
236
        $theme = config('backpack.base.view_namespace');
237
238
        if (is_null($theme)) {
239
            $theme = $originalTheme;
240
        }
241
242
        $returnView = $theme.$view;
243
244
        if (! view()->exists($returnView)) {
245
            $returnView = $originalTheme.$view;
246
        }
247
248
        return $returnView;
249
    }
250
}
251
252
if (! function_exists('square_brackets_to_dots')) {
253
    /**
254
     * Turns a string from bracket-type array to dot-notation array.
255
     * Ex: array[0][property] turns into array.0.property.
256
     *
257
     * @param $path
258
     * @return string
259
     */
260
    function square_brackets_to_dots($string)
261
    {
262
        $string = str_replace(['[', ']'], ['.', ''], $string);
263
264
        return $string;
265
    }
266
}
267
268
if (! function_exists('old_empty_or_null')) {
269
    /**
270
     * This method is an alternative to Laravel's old() helper, which mistakenly
271
     * returns NULL it two cases:
272
     * - if there is an old value, and it was empty or null
273
     * - if there is no old value
274
     * (this is because of the ConvertsEmptyStringsToNull middleware).
275
     *
276
     * In contrast, this method will return:
277
     * - the old value, if there actually is an old value for that key;
278
     * - the second parameter, if there is no old value for that key, but it was empty string or null;
279
     * - null, if there is no old value at all for that key;
280
     *
281
     * @param  string  $key
282
     * @param  array|string  $empty_value
283
     * @return mixed
284
     */
285
    function old_empty_or_null($key, $empty_value = '')
286
    {
287
        $key = square_brackets_to_dots($key);
288
        $old_inputs = session()->getOldInput();
289
290
        // if the input name is present in the old inputs we need to return earlier and not in a coalescing chain
291
        // otherwise `null` aka empty will not pass the condition and the field value would be returned.
292
        if (\Arr::has($old_inputs, $key)) {
293
            return \Arr::get($old_inputs, $key) ?? $empty_value;
294
        }
295
296
        return null;
297
    }
298
}
299
300
if (! function_exists('is_multidimensional_array')) {
301
    /**
302
     * If any of the items inside a given array is an array, the array is considered multidimensional.
303
     *
304
     * @param  array  $array
305
     * @return bool
306
     */
307
    function is_multidimensional_array(array $array)
308
    {
309
        foreach ($array as $item) {
310
            if (is_array($item)) {
311
                return true;
312
            }
313
        }
314
315
        return false;
316
    }
317
}
318
319
if (! function_exists('backpack_pro')) {
320
    /**
321
     * Check if the backpack/pro package is installed.
322
     *
323
     * @return bool
324
     */
325
    function backpack_pro()
326
    {
327
        if (app()->runningUnitTests()) {
0 ignored issues
show
introduced by
The method runningUnitTests() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

327
        if (app()->/** @scrutinizer ignore-call */ runningUnitTests()) {
Loading history...
328
            return true;
329
        }
330
        if (! \Composer\InstalledVersions::isInstalled('backpack/pro')) {
331
            return false;
332
        }
333
334
        return \PackageVersions\Versions::getVersion('backpack/pro');
0 ignored issues
show
Bug Best Practice introduced by
The expression return PackageVersions\V...Version('backpack/pro') returns the type string which is incompatible with the documented return type boolean.
Loading history...
335
    }
336
}
337