Completed
Push — master ( c141df...fb040e )
by Abdelrahman
01:14
created

helpers.php ➔ domain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Str;
6
7
if (! function_exists('domain')) {
8
    /**
9
     * Return domain host.
10
     *
11
     * @return string
12
     */
13
    function domain()
14
    {
15
        return parse_url(config('app.url'))['host'];
16
    }
17
}
18
19
if (! function_exists('intend')) {
20
    /**
21
     * Return redirect response.
22
     *
23
     * @param array $arguments
24
     * @param int   $status
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
27
     */
28
    function intend(array $arguments, int $status = 302)
29
    {
30
        $redirect = redirect($url = array_pull($arguments, 'url'), $status);
31
        $status = $status ?: (isset($arguments['withErrors']) ? 422 : 200);
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
33
        if (request()->expectsJson()) {
34
            $response = collect($arguments['withErrors'] ?? $arguments['with']);
35
36
            return response()->json([$response->flatten()->first() ?? 'OK'], 200)->header('Turbolinks-Location', $url);
37
        }
38
39
        foreach ($arguments as $key => $value) {
40
            $redirect = in_array($key, ['home', 'back']) ? $redirect->{$key}() : $redirect->{$key}($value);
41
        }
42
43
        return $redirect->header('Turbolinks-Location', $url);
44
    }
45
}
46
47
if (! function_exists('lower_case')) {
48
    /**
49
     * Convert the given string to lower-case.
50
     *
51
     * @param string $value
52
     *
53
     * @return string
54
     */
55
    function lower_case($value)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
56
    {
57
        return Str::lower($value);
58
    }
59
}
60
61
if (! function_exists('upper_case')) {
62
    /**
63
     * Convert the given string to upper-case.
64
     *
65
     * @param string $value
66
     *
67
     * @return string
68
     */
69
    function upper_case($value)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
70
    {
71
        return Str::upper($value);
72
    }
73
}
74
75
if (! function_exists('mimetypes')) {
76
    /**
77
     * Get valid mime types.
78
     *
79
     * @see https://github.com/symfony/http-foundation/blob/3.0/File/MimeType/MimeTypeExtensionGuesser.php
80
     * @see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
81
     *
82
     * @return array
83
     */
84
    function mimetypes()
85
    {
86
        return json_decode(file_get_contents(__DIR__.'/../../resources/data/mimetypes.json'), true);
87
    }
88
}
89
90
if (! function_exists('timezones')) {
91
    /**
92
     * Get valid timezones.
93
     * This list is based upon the timezone database version 2017.2.
94
     *
95
     * @see http://php.net/manual/en/timezones.php
96
     *
97
     * @return array
98
     */
99
    function timezones()
100
    {
101
        return json_decode(file_get_contents(__DIR__.'/../../resources/data/timezones.json'), true);
102
    }
103
}
104
105
if (! function_exists('array_search_recursive')) {
106
    /**
107
     * Recursively searches the array for a given value and returns the corresponding key if successful.
108
     *
109
     * @param mixed $needle
110
     * @param array $haystack
111
     *
112
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer|string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
113
     */
114
    function array_search_recursive($needle, $haystack)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
115
    {
116
        foreach ($haystack as $key => $value) {
117
            $current_key = $key;
118
            if ($needle === $value || (is_array($value) && array_search_recursive($needle, $value) !== false)) {
119
                return $current_key;
120
            }
121
        }
122
123
        return false;
124
    }
125
}
126
127
if (! function_exists('array_trim_recursive')) {
128
    /**
129
     * Recursively trim elements of the given array.
130
     *
131
     * @param mixed  $values
132
     * @param string $charlist
133
     *
134
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|object|integer|double|null|boolean|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
135
     */
136
    function array_trim_recursive($values, $charlist = " \t\n\r\0\x0B")
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
137
    {
138
        if (is_array($values)) {
139
            return array_map('array_trim_recursive', $values);
140
        }
141
142
        return is_string($values) ? trim($values, $charlist) : $values;
143
    }
144
}
145
146
if (! function_exists('array_filter_recursive')) {
147
    /**
148
     * Recursively filter empty strings and null elements of the given array.
149
     *
150
     * @param array $values
151
     * @param bool  $strOnly
152
     *
153
     * @return mixed
154
     */
155
    function array_filter_recursive($values, $strOnly = true)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
156
    {
157
        foreach ($values as &$value) {
158
            if (is_array($value)) {
159
                $value = array_filter_recursive($value);
160
            }
161
        }
162
163
        return ! $strOnly ? array_filter($values) : array_filter($values, function ($item) {
164
            return ! is_null($item) && ! ((is_string($item) || is_array($item)) && empty($item));
165
        });
166
    }
167
}
168