helpers.php ➔ get_request_ip()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
if (!function_exists('get_user_timezone')) {
4
    function get_user_timezone()
0 ignored issues
show
Coding Style introduced by
function get_user_timezone() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
5
    {
6
        return 'UTC';
7
    }
8
}
9
10
if (!function_exists('get_request_ip')) {
11
    function get_request_ip()
0 ignored issues
show
Coding Style introduced by
function get_request_ip() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
get_request_ip uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
12
    {
13
        if (isset($_SERVER['X_FORWARDED_FOR'])) {
14
            return $_SERVER['X_FORWARDED_FOR'];
15
        } elseif (isset($_SERVER['CLIENT_IP'])) {
16
            return $_SERVER['CLIENT_IP'];
17
        }
18
19
        return $_SERVER['REMOTE_ADDR'];
20
    }
21
}
22
23
24
if (!function_exists('to_name_value')) {
25
    function to_name_value($array, $keys = null)
0 ignored issues
show
Coding Style introduced by
function to_name_value() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
26
    {
27
        $data = [];
28
        foreach ($array as $name => $value) {
29
            $row = ['name' => $name, 'value' => $value];
30
            if (isset($keys)) $row = array_merge($row, $keys);
31
            array_push($data, $row);
32
        }
33
34
        return $data;
35
    }
36
}
37
38
if (!function_exists('sorting_by_key')) {
39
    /**
40
     * Sorting callable helper
41
     *
42
     * @param string $key
43
     * @param string $order
44
     *
45
     * @return Closure
46
     */
47
    function sorting_by_key($key, $order = 'ASC')
0 ignored issues
show
Coding Style introduced by
function sorting_by_key() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
48
    {
49
        return function ($a, $b) use ($key, $order) {
50
            if ($a[$key] === $b[$key]) {
51
                return 0;
52
            }
53
54
            $value = $a[$key] < $b[$key] ? -1 : 1;
55
            if ($order === 'DESC') {
56
                $value *= -1;
57
            }
58
59
            return $value;
60
        };
61
    }
62
}
63
64
if (!function_exists('get_columns_flat_at')) {
65
    /**
66
     * Get all the columns name in the given level
67
     *
68
     * @param array $columns
69
     * @param int $level
70
     *
71
     * @return array
72
     */
73
    function get_columns_flat_at(array $columns, $level = 0)
0 ignored issues
show
Coding Style introduced by
function get_columns_flat_at() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
74
    {
75
        $names = [];
76
77
        foreach ($columns as $column) {
78
            $parts = explode('.', $column);
79
80
            if (isset($parts[$level])) {
81
                $names[] = $parts[$level];
82
            }
83
        }
84
85
        return $names;
86
    }
87
}
88
89
if (!function_exists('get_csv_flat_columns')) {
90
    /**
91
     * Gets a CSV flat columns list from the given array
92
     *
93
     * @param array $columns
94
     * @param null $prefix
95
     *
96
     * @return string
97
     */
98
    function get_csv_flat_columns(array $columns, $prefix = null)
0 ignored issues
show
Coding Style introduced by
function get_csv_flat_columns() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
99
    {
100
        $flatColumns = [];
101
        $prefix = $prefix === null ? '' : $prefix . '.';
102
103
        foreach ($columns as $key => $value) {
104
            if (is_array($value)) {
105
                $value = get_csv_flat_columns($value, $prefix . $key);
106
            } else {
107
                $value = $prefix . $key;
108
            }
109
110
            $flatColumns[] = $value;
111
        }
112
113
        return implode(',', $flatColumns);
114
    }
115
}
116
117
if (!function_exists('get_array_flat_columns')) {
118
    /**
119
     * Gets an array flat columns list from the given array
120
     *
121
     * @param $columns
122
     *
123
     * @return array
124
     */
125
    function get_array_flat_columns($columns)
0 ignored issues
show
Coding Style introduced by
function get_array_flat_columns() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
126
    {
127
        // TODO: make sure array is passed???
128
        return explode(',', get_csv_flat_columns($columns ?: []));
129
    }
130
}
131
if (!function_exists('get_unflat_columns')) {
132
    /**
133
     * Gets the unflat version of flat (dot-notated) column list
134
     *
135
     * @param string|array $columns
136
     *
137
     * @return array
138
     */
139
    function get_unflat_columns($columns)
0 ignored issues
show
Coding Style introduced by
function get_unflat_columns() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
140
    {
141
        $names = [];
142
143
        if (!is_array($columns)) {
144
            $columns = explode(',', $columns);
145
        }
146
147
        foreach ($columns as $column) {
148
            $parts = explode('.', $column, 2);
149
150
            if (isset($parts[0])) {
151 View Code Duplication
                if (!isset($names[$parts[0]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
                    $names[$parts[0]] = null;
153
                }
154
155
                if (isset($parts[1])) {
156 View Code Duplication
                    if ($names[$parts[0]] === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
                        $names[$parts[0]] = [];
158
                    }
159
160
                    $child = get_unflat_columns($parts[1]);
161
                    $names[$parts[0]][key($child)] = current($child);
162
                };
163
            }
164
        }
165
166
        return $names;
167
    }
168
}
169