Completed
Pull Request — master (#28)
by Elan
01:16
created

Xhgui_Util::getMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * Common utilities
5
 */
6
class Xhgui_Util
7
{
8
    /**
9
     * Creates a simplified URL given a standard URL.
10
     * Does the following transformations:
11
     *
12
     * - Remove numeric values after =.
13
     *
14
     * @param string $url
15
     * @return string
16
     */
17
    public static function simpleUrl($url)
18
    {
19
        $callable = Xhgui_Config::read('profiler.simple_url');
20
        if (is_callable($callable)) {
21
            return call_user_func($callable, $url);
22
        }
23
        return preg_replace('/\=\d+/', '', $url);
24
    }
25
26
    /**
27
     * Serialize data for storage
28
     *
29
     * @param      $data
30
     * @param bool $profiles
31
     *
32
     * @return false|string
33
     */
34
    public static function getDataForStorage($data, $profiles = true)
35
    {
36
        if ($profiles) {
37
            $serializer = Xhgui_Config::read('save.handler.serializer', 'json');
38
        } else {
39
            $serializer = Xhgui_Config::read('save.handler.meta_serializer', 'php');
40
        }
41
42
        switch ($serializer) {
43
            case 'json':
44
                return json_encode($data);
45
                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...
46
47
            case 'igbinary_serialize':
48
            case 'igbinary_unserialize':
49
            case 'igbinary':
50
                return igbinary_serialize($data);
51
                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...
52
53
            case 'php':
54
            case 'var_export':
55
                return "<?php \n".var_export($data, true);
56
                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...
57
        }
58
    }
59
60
    /**
61
     * Get id for a record.
62
     *
63
     * By default this method will try to re-use request id from http server.
64
     * This is needed for some storage engines that don't have string/hashlike id generation.
65
     *
66
     * @param array $data
67
     * @param bool $useRequestId
68
     *
69
     * @return string
70
     */
71
    public static function getId(array $data = array(), $useRequestId = true)
72
    {
73
74
        // in some cases, like during import, we might already have id
75
        if (!empty($data['id'])) {
76
            return $data['id'];
77
        }
78
79
        // mongo compatibility
80
        if (!empty($data['_id'])) {
81
            return $data['_id'];
82
        }
83
84
        if ($useRequestId) {
85
            foreach(array('REQUEST_ID', 'HTTP_REQUEST_ID', 'HTTP_X_REQUEST_ID', 'X_CORRELATION_ID', 'HTTP_X_CORRELATION_ID') as $header) {
86
                if (array_key_exists($header, $_SERVER) !== false) {
87
                    return $_SERVER[$header];
88
                }
89
            }
90
        }
91
92
        // try php 7+ function.
93
        if (function_exists('random_bytes')) {
94
            try {
95
                return bin2hex(random_bytes(16));
96
            } catch (\Exception $e) {
97
                // entropy source is not available
98
        }
99
        }
100
101
        // try openssl. For purpose of this id we can ignore info if this value is strong or not
102
        if (function_exists('openssl_random_pseudo_bytes')) {
103
            /** @noinspection CryptographicallySecureRandomnessInspection */
104
            return bin2hex(openssl_random_pseudo_bytes(16, $strong));
105
        }
106
107
        // fallback to most generic method. Make sure it has 32 characters :)
108
        return md5(uniqid('xhgui', true).microtime());
109
    }
110
111
112
    /**
113
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
114
     * @return mixed|string
115
     */
116
    public static function getMethod() {
117
        if(PHP_SAPI ==='cli') {
118
            return 'CLI';
119
        }
120
        if (!empty($_SERVER['REQUEST_METHOD'])) {
121
            return $_SERVER['REQUEST_METHOD'];
122
        }
123
        return 'UNKNOWN';
124
    }
125
}
126