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
Pull Request — master (#3054)
by
unknown
12:52
created

oldValueDefaultOrFallback()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 10
c 1
b 1
f 0
nc 4
nop 5
dl 0
loc 22
rs 9.9332
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] = $start ? [$row['value']] : $row['value'];
64
                } else {
65
                    array_push($result[$input_key], $row['value']);
66
                }
67
            } else {
68
                $result[$input_key][$input_arg] = $row['value'];
69
            }
70
        }
71
72
        return $result;
73
    }
74
}
75
76
if (! function_exists('backpack_users_have_email')) {
77
    /**
78
     * Check if the email column is present on the user table.
79
     *
80
     * @return string
81
     */
82
    function backpack_users_have_email()
83
    {
84
        $user_model_fqn = config('backpack.base.user_model_fqn');
85
        $user = new $user_model_fqn();
86
87
        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...
88
    }
89
}
90
91
if (! function_exists('backpack_avatar_url')) {
92
    /**
93
     * Returns the avatar URL of a user.
94
     *
95
     * @param $user
96
     *
97
     * @return string
98
     */
99
    function backpack_avatar_url($user)
100
    {
101
        $firstLetter = $user->getAttribute('name') ? mb_substr($user->name, 0, 1, 'UTF-8') : 'A';
102
        $placeholder = 'https://placehold.it/160x160/00a65a/ffffff/&text='.$firstLetter;
103
104
        switch (config('backpack.base.avatar_type')) {
105
            case 'gravatar':
106
                if (backpack_users_have_email()) {
107
                    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...
108
                } else {
109
                    return $placeholder;
110
                }
111
                break;
112
113
            case 'placehold':
114
                return $placeholder;
115
                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...
116
117
            default:
118
                return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')};
119
                break;
120
        }
121
    }
122
}
123
124
if (! function_exists('backpack_middleware')) {
125
    /**
126
     * Return the key of the middleware used across Backpack.
127
     * That middleware checks if the visitor is an admin.
128
     *
129
     * @param $path
130
     *
131
     * @return string
132
     */
133
    function backpack_middleware()
134
    {
135
        return config('backpack.base.middleware_key', 'admin');
136
    }
137
}
138
139
if (! function_exists('backpack_guard_name')) {
140
    /*
141
     * Returns the name of the guard defined
142
     * by the application config
143
     */
144
    function backpack_guard_name()
145
    {
146
        return config('backpack.base.guard', config('auth.defaults.guard'));
147
    }
148
}
149
150
if (! function_exists('backpack_auth')) {
151
    /*
152
     * Returns the user instance if it exists
153
     * of the currently authenticated admin
154
     * based off the defined guard.
155
     */
156
    function backpack_auth()
157
    {
158
        return \Auth::guard(backpack_guard_name());
159
    }
160
}
161
162
if (! function_exists('backpack_user')) {
163
    /*
164
     * Returns back a user instance without
165
     * the admin guard, however allows you
166
     * to pass in a custom guard if you like.
167
     */
168
    function backpack_user()
169
    {
170
        return backpack_auth()->user();
171
    }
172
}
173
174
if (! function_exists('mb_ucfirst')) {
175
    /**
176
     * Capitalize the first letter of a string,
177
     * even if that string is multi-byte (non-latin alphabet).
178
     *
179
     * @param string   $string   String to have its first letter capitalized.
180
     * @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...
181
     *
182
     * @return string String with first letter capitalized.
183
     */
184
    function mb_ucfirst($string, $encoding = false)
185
    {
186
        $encoding = $encoding ? $encoding : mb_internal_encoding();
187
188
        $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

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

189
        $firstChar = mb_substr($string, 0, 1, /** @scrutinizer ignore-type */ $encoding);
Loading history...
190
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
191
192
        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

192
        return mb_strtoupper($firstChar, /** @scrutinizer ignore-type */ $encoding).$then;
Loading history...
193
    }
194
}
195
196
if (! function_exists('backpack_view')) {
197
    /**
198
     * Returns a new displayable view based on the configured backpack view namespace.
199
     * If that view doesn't exist, it will load the one from the original theme.
200
     *
201
     * @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...
202
     *
203
     * @return string
204
     */
205
    function backpack_view($view)
206
    {
207
        $originalTheme = 'backpack::';
208
        $theme = config('backpack.base.view_namespace');
209
210
        if (is_null($theme)) {
211
            $theme = $originalTheme;
212
        }
213
214
        $returnView = $theme.$view;
215
216
        if (! view()->exists($returnView)) {
217
            $returnView = $originalTheme.$view;
218
        }
219
220
        return $returnView;
221
    }
222
}
223
224
if (! function_exists('square_brackets_to_dots')) {
225
    /**
226
     * Turns a string from bracket-type array to dot-notation array.
227
     * Ex: array[0][property] turns into array.0.property.
228
     *
229
     * @param $path
230
     *
231
     * @return string
232
     */
233
    function square_brackets_to_dots($string)
234
    {
235
        $string = str_replace(['[', ']'], ['.', ''], $string);
236
237
        return $string;
238
    }
239
}
240
241
if (! function_exists('is_countable')) {
242
    /**
243
     * We need this because is_countable was only introduced in PHP 7.3,
244
     * and in PHP 7.2 you should check if count() argument is really countable.
245
     * This function may be removed in future if PHP >= 7.3 becomes a requirement.
246
     *
247
     * @param $obj
248
     *
249
     * @return bool
250
     */
251
    function is_countable($obj)
252
    {
253
        return is_array($obj) || $obj instanceof Countable;
254
    }
255
}
256
257
if (! function_exists('oldOrFallback')) {
258
    /**
259
     * This function allows us to setup a default value in case there is an old value, but is forcelly null
260
     * by Laravel middleware ConvertEmptyStringsToNull.
261
     *
262
     * @param string $field_name
263
     * @param mixed $fallback
264
     *
265
     * @return mixed
266
     */
267
    function oldValueDefaultOrFallback($field, $fallback, $field_name = null, $default = null, $value = null)
268
    {
269
        $field_name = $field_name ?? $field['name'] ?? null;
270
        $default = $default ?? $field['default'] ?? null;
271
        $value = $value ?? $field['value'] ?? null;
272
273
        // When you submit your field empty, internaly laravel will convert it to «null».
274
        // Using old('field') with coalescing operator (??) we can't match it even if we have it in the old() session (as null).
275
        // This checks that the value is null, but exists in the session old array, so it was submited empty|null, but exists!
276
        if(! empty(session()->getOldInput())) {
277
            if (is_null(old(square_brackets_to_dots($field_name)))) {
278
                return $fallback;
279
            }
280
281
            return old(square_brackets_to_dots($field_name));
282
        }
283
284
        if (array_key_exists('value', $field)) {
285
            return $value;
286
        }
287
288
        return $default ?? $fallback;
289
    }
290
}
291
292