Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

array_helper.php ➔ pluralize()   C

Complexity

Conditions 14
Paths 10

Size

Total Lines 25
Code Lines 16

Duplication

Lines 12
Ratio 48 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 14
eloc 16
c 2
b 2
f 0
nc 10
nop 2
dl 12
loc 25
rs 5.0864

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
if (!function_exists('my_print_r')) {
8
9
//@codingStandardsIgnoreStart
10
    function my_print_r($array = []) {
11
        echo "<pre>";
12
        print_r($array);
13
        echo "</pre>";
14
    }
15
16
//@codingStandardsIgnoreEnd
17
}
18
19
if (!function_exists('is_true_array')) {
20
21
    /**
22
     *
23
     * @param array $array
24
     * @return boolean
25
     */
26
    function is_true_array($array) {
27
        if ($array == false) {
28
            return false;
29
        }
30
        $arraySize = count($array);
31
        return $arraySize > 0;
32
    }
33
34
}
35
36
if (!function_exists('result_column')) {
37
38
    /**
39
     * For
40
     * @param array $result array of arrays
41
     * @return array
42
     */
43
    function result_column($result) {
44
45
        if (count($result) == 0) {
46
            return [];
47
        }
48
49
        $key = key($result[0]);
50
        $countResult = count($result);
51
        for ($i = 0; $i < $countResult; $i++) {
52
            $result[$i] = $result[$i][$key];
53
        }
54
55
        return $result;
56
    }
57
58
}
59
60
if (!function_exists('array_key_exists_recursive')) {
61
62
    /**
63
     * Recursive search key in associative array (depth does not matter)
64
     * @param string $key
65
     * @param array $array
66
     * @param boolean $return (optional) if true then result will be returned (false default)
67
     * @return boolean|mixed
68
     */
69
    function array_key_exists_recursive($key, $array, $return = FALSE) {
70
        foreach ($array as $key_ => $value_) {
71
            if (is_array($value_) && $key_ !== $key) {
72
                if (FALSE !== $value = array_key_exists_recursive($key, $value_, $return)) {
73
                    return $return === FALSE ? TRUE : $value;
74
                }
75
            } else {
76
                if ($key_ == $key) {
77
                    return $return === FALSE ? TRUE : $array[$key_];
78
                }
79
            }
80
        }
81
        return FALSE;
82
    }
83
84
}
85
86
if (!function_exists('array_to_file')) {
87
88
    /**
89
     * Write array in file.
90
     *
91
     * @param string $file
92
     * @param array $array
93
     * @return integer
94
     */
95
    function array_to_file($file, $array) {
96
        return file_put_contents($file, '<?php $arr = ' . var_export($array, true) . ';');
97
    }
98
99
}
100
101
if (!function_exists('user_function_sort')) {
102
103
    /**
104
     *
105
     * @param array $arr
106
     * @param string $key
107
     * @return array
108
     */
109
    function user_function_sort($arr, $key = 'value') {
110
        usort(
111
            $arr,
112
            function ($a, $b) use ($key) {
113
                    return strnatcmp($a[$key], $b[$key]);
114
            }
115
        );
116
        return $arr;
117
    }
118
119
}
120
121
122
if (!function_exists('pluralize')) {
123
124
    /**
125
     *
126
     * @param integer $count
127
     * @param array $words
128
     * @return string
129
     */
130
    function pluralize($count = 0, array $words = []) {
131
132
        if (empty($words)) {
133
            $words = [
134
                      ' ',
135
                      ' ',
136
                      ' ',
137
                     ];
138
        }
139
140
        $numeric = (int) abs($count);
141 View Code Duplication
        if ($numeric % 100 == 1 || ($numeric % 100 > 20) && ($numeric % 10 == 1)) {
142
            return $words[0];
143
        }
144 View Code Duplication
        if ($numeric % 100 == 2 || ($numeric % 100 > 20) && ($numeric % 10 == 2)) {
145
            return $words[1];
146
        }
147 View Code Duplication
        if ($numeric % 100 == 3 || ($numeric % 100 > 20) && ($numeric % 10 == 3)) {
148
            return $words[1];
149
        }
150 View Code Duplication
        if ($numeric % 100 == 4 || ($numeric % 100 > 20) && ($numeric % 10 == 4)) {
151
            return $words[1];
152
        }
153
        return $words[2];
154
    }
155
156
}
157
158
if (!function_exists('array_column')) {
159
160
    /**
161
     *
162
     * @param array $array
163
     * @param string $column_name
164
     * @return array
165
     */
166
    function array_column($array, $column_name) {
167
        return array_map(
168
            function($element) use($column_name) {
169
                    return $element[$column_name];
170
            },
171
            $array
172
        );
173
    }
174
175
}