Completed
Push — master ( f08ee6...e9f4a6 )
by Welling
01:33
created

helpers.php ➔ sorting_by_key()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 1
nop 2
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 20
rs 9.2
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