Passed
Push — master ( f2b86f...ddc97c )
by Mikael
08:52
created

functions.php ➔ mergesort()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 14
nop 2
dl 0
loc 37
ccs 0
cts 0
cp 0
crap 72
rs 8.0835
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Global functions used in various modules.
5
 */
6
7
8
9
/**
10
 * i18n, internationalization, send all strings though this function to enable i18n. Inspired by Drupal´s t()-function.
11
 *
12
 * @param string $str  the string to check up for translation.
13
 * @param array  $args associative array with arguments to be
14
 *                     replaced in the str.
15
 *                      - !variable: Inserted as is. Use this for text
16
 *                        that has already been sanitized.
17
 *                      - @variable: Escaped to HTML using htmlEnt(). Use
18
 *                        this for anything displayed on a page on the site.
19
 *
20
 * @return string the translated string.
21
 */
22
function t($str, $args = [])
23
{
24
    /*
25
    if (CLydia::Instance()->config["i18n"]) {
26
        $str = gettext($str);
27
    }
28
    */
29
30
    // santitize and replace arguments
31 3
    if (!empty($args)) {
32 2
        foreach ($args as $key => $val) {
33 2
            switch ($key[0]) {
34 2
                case "@":
35 1
                    $args[$key] = htmlentities($val);
36 1
                    break;
37
38 1
                case "!":
39
                default: /* pass through */
40 1
                    break;
41
            }
42
        }
43 2
        return strtr($str, $args);
44
    }
45 1
    return $str;
46
}
47
48
49
50
/**
51
 * Sort array but maintain index when compared items are equal.
52
 * http://www.php.net/manual/en/function.usort.php#38827
53
 *
54
 * @param array    &$array       input array
55
 * @param callable $cmpFunction custom function to compare values
56
 *
57
 * @return void
58
 *
59
 * @codeCoverageIgnore
60
 */
61
function mergesort(&$array, $cmpFunction)
62
{
63
    // Arrays of size < 2 require no action.
64
    if (count($array) < 2) {
65
        return;
66
    }
67
    // Split the array in half
68
    $halfway = count($array) / 2;
69
    $array1 = array_slice($array, 0, $halfway);
70
    $array2 = array_slice($array, $halfway);
71
    // Recurse to sort the two halves
72
    mergesort($array1, $cmpFunction);
73
    mergesort($array2, $cmpFunction);
74
    // If all of $array1 is <= all of $array2, just append them.
75
    if (call_user_func($cmpFunction, end($array1), $array2[0]) < 1) {
76
        $array = array_merge($array1, $array2);
77
        return;
78
    }
79
    // Merge the two sorted arrays into a single sorted array
80
    $array = array();
81
    $ptr1 = $ptr2 = 0;
82
    while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
83
        if (call_user_func($cmpFunction, $array1[$ptr1], $array2[$ptr2]) < 1) {
84
            $array[] = $array1[$ptr1++];
85
        } else {
86
            $array[] = $array2[$ptr2++];
87
        }
88
    }
89
    // Merge the remainder
90
    while ($ptr1 < count($array1)) {
91
        $array[] = $array1[$ptr1++];
92
    }
93
    while ($ptr2 < count($array2)) {
94
        $array[] = $array2[$ptr2++];
95
    }
96
    return;
97
}
98
99
100
101
/**
102
 * Glob recursivly.
103
 * http://in.php.net/manual/en/function.glob.php#106595
104
 *
105
 * @param string $pattern  pattern to search for
106
 * @param int    $flags    flags to use, as in glob()
107
 *
108
 * @return void
109
 *
110
 * @codeCoverageIgnore
111
 */
112
function glob_recursive($pattern, $flags = 0)
113
{
114
    $files = glob($pattern, $flags);
115
    foreach (glob(dirname($pattern) . "/*", GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
116
        $files = array_merge($files, glob_recursive($dir .  "/" . basename($pattern), $flags));
117
    }
118
    return $files;
119
}
120
121
122
123
/**
124
* array_merge_recursive does indeed merge arrays, but it converts values
125
* with duplicate keys to arrays rather than overwriting the value in the
126
* first array with the duplicate value in the second array, as array_merge
127
* does. I.e., with array_merge_recursive, this happens (documented behavior):
128
*
129
* array_merge_recursive(['key' => 'org value'], ['key' => 'new value']);
130
*     => ['key' => ['org value', 'new value']];
131
*
132
* array_merge_recursive_distinct does not change the datatypes of the values
133
* in the arrays. Matching keys' values in the second array overwrite those
134
* in the first array, as is the case with array_merge, i.e.:
135
*
136
* array_merge_recursive_distinct(['key' => 'org value'], ['key' => 'new value']);
137
*     => ['key' => ['new value']];
138
*
139
* Parameters are passed by reference, though only for performance reasons.
140
* They're not altered by this function.
141
*
142
* @param array $array1
143
* @param array $array2
144
* @return array
145
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
146
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
147
*
148
* @codeCoverageIgnore
149
*/
150
function array_merge_recursive_distinct(array &$array1, array &$array2)
151
{
152
    $merged = $array1;
153
154
    $value = null;
155
    foreach ($array2 as $key => &$value) {
156
        if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) {
157
            $merged [$key] = array_merge_recursive_distinct($merged [$key], $value);
158
        } else {
159
            $merged [$key] = $value;
160
        }
161
    }
162
163
    return $merged;
164
}
165