Passed
Push — master ( 24edcc...0ebf8c )
by Jean-Christophe
23:59
created

UArray   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 43
eloc 76
dl 0
loc 197
ccs 55
cts 90
cp 0.6111
rs 8.96
c 1
b 1
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A iRemove() 0 9 3
A iRemoveOne() 0 5 2
A update() 0 5 2
A iSearch() 0 2 1
A removeByKeys() 0 6 2
A isAssociative() 0 2 1
A removeOne() 0 5 2
A getValue() 0 7 3
A getRecursive() 0 11 4
A removeRecursive() 0 6 4
A removeByKey() 0 5 2
A extractKeys() 0 8 3
A save() 0 3 1
A remove() 0 9 3
A getDefaultValue() 0 2 1
A doubleBackSlashes() 0 3 1
A implodeAsso() 0 10 3
A toJSON() 0 13 5

How to fix   Complexity   

Complex Class

Complex classes like UArray often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UArray, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
use Ubiquity\utils\base\traits\UArrayAsTrait;
6
7
/**
8
 * Array utilities.
9
 * Ubiquity\utils\base$UArray
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.6
14
 *
15
 */
16
class UArray {
17
	use UArrayAsTrait;
18
19
	/**
20
	 * Tests if array is associative
21
	 *
22
	 * @param array $array
23
	 * @return boolean
24
	 */
25 1
	public static function isAssociative($array) {
26 1
		return (\array_keys ( $array ) !== \range ( 0, \count ( $array ) - 1 ));
27
	}
28
29
	/**
30
	 * Returns a new array with the keys $keys
31
	 *
32
	 * @param array $array an associative array
33
	 * @param array $keys some keys
34
	 * @return array
35
	 */
36 1
	public static function extractKeys($array, $keys) {
37 1
		$result = [ ];
38 1
		foreach ( $keys as $key ) {
39 1
			if (isset ( $array [$key] )) {
40 1
				$result [$key] = $array [$key];
41
			}
42
		}
43 1
		return $result;
44
	}
45
46
	/**
47
	 *
48
	 * @param array $array
49
	 * @param string $key
50
	 * @param int $pos
51
	 * @return mixed|null
52
	 */
53 1
	public static function getValue($array, $key, $pos) {
54 1
		if (\array_key_exists ( $key, $array )) {
55 1
			return $array [$key];
56
		}
57 1
		$values = \array_values ( $array );
58 1
		if ($pos < \count ( $values ))
59 1
			return $values [$pos];
60 1
	}
61
62
	/**
63
	 *
64
	 * @param array $array
65
	 * @param string|int $key
66
	 * @param mixed $default
67
	 * @return mixed
68
	 */
69 18
	public static function getRecursive($array, $key, $default = null) {
70 18
		if (\array_key_exists ( $key, $array )) {
71 18
			return $array [$key];
72
		} else {
73 18
			foreach ( $array as $item ) {
74 18
				if (is_array ( $item )) {
75 18
					return self::getRecursive ( $item, $key, $default );
76
				}
77
			}
78
		}
79 1
		return $default;
80
	}
81
82 1
	public static function getDefaultValue($array, $key, $default) {
83 1
		return $array [$key] ?? $default;
84
	}
85
86
	/**
87
	 * Save a php array to the disk.
88
	 *
89
	 * @param array $array The array to save
90
	 * @param string $filename The path of the file to save in
91
	 * @return int
92
	 */
93
	public static function save($array, $filename) {
94
		$content = "<?php\nreturn " . self::asPhpArray ( $array, "array", 1, true ) . ";";
95
		return UFileSystem::save ( $filename, $content );
96
	}
97
98 2
	public static function remove($array, $search) {
99 2
		if (\is_array ( $search )) {
100 1
			foreach ( $search as $val ) {
101 1
				$array = self::removeOne ( $array, $val );
102
			}
103
		} else {
104 2
			$array = self::removeOne ( $array, $search );
105
		}
106 2
		return \array_values ( $array );
107
	}
108
109
	/**
110
	 * Removes from array by key
111
	 *
112
	 * @param array $array
113
	 * @param int|string $key
114
	 * @return array
115
	 */
116 1
	public static function removeByKey($array, $key) {
117 1
		if (isset ( $array [$key] )) {
118 1
			unset ( $array [$key] );
119
		}
120 1
		return $array;
121
	}
122
123 17
	public static function removeRecursive(&$array, $key) {
124 17
		foreach ( $array as &$item ) {
125 17
			if (\array_key_exists ( $key, $item )) {
126 17
				unset ( $item [$key] );
127 17
			} elseif (\is_array ( $item )) {
128 17
				self::removeRecursive ( $item, $key );
129
			}
130
		}
131 17
	}
132
133
	public static function removeByKeys($array, $keys) {
134
		$assocKeys = [ ];
135
		foreach ( $keys as $key ) {
136
			$assocKeys [$key] = true;
137
		}
138
		return \array_diff_key ( $array, $assocKeys );
139
	}
140
141 3
	public static function removeOne($array, $search) {
142 3
		if (($key = \array_search ( $search, $array )) !== false) {
143 3
			unset ( $array [$key] );
144
		}
145 3
		return $array;
146
	}
147
148
	public static function update(&$array, $search, $newValue) {
149
		if (($key = \array_search ( $search, $array )) !== false) {
150
			$array [$key] = $newValue;
151
		}
152
		return $array;
153
	}
154
155
	/**
156
	 *
157
	 * @param array $array
158
	 * @return boolean
159
	 */
160
	public static function doubleBackSlashes(&$array) {
161
		return \array_walk ( $array, function (&$value) {
162
			$value = UString::doubleBackSlashes ( $value );
163
		} );
164
	}
165
166 2
	public static function iSearch($needle, $haystack, $strict = null) {
167 2
		return \array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict );
168
	}
169
170
	public static function iRemove($array, $search) {
171
		if (\is_array ( $search )) {
172
			foreach ( $search as $val ) {
173
				$array = self::iRemoveOne ( $array, $val );
174
			}
175
		} else {
176
			$array = self::iRemoveOne ( $array, $search );
177
		}
178
		return \array_values ( $array );
179
	}
180
181 2
	public static function iRemoveOne($array, $search) {
182 2
		if (($key = self::iSearch ( $search, $array )) !== false) {
183 2
			unset ( $array [$key] );
184
		}
185 2
		return $array;
186
	}
187
188 9
	public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') {
189 9
		$res = [ ];
190 9
		foreach ( $array as $k => $v ) {
191
			if(\is_string($k)){
192
				$res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}";
193
			}else{
194
				$res [] = "{$quoteKey}{$v}{$quoteKey}";
195
			}
196
		}
197 9
		return \implode ( $glue, $res );
198
	}
199
200
	public static function toJSON($array) {
201
		$result = [ ];
202
		foreach ( $array as $k => $v ) {
203
			if (\is_array ( $v )) {
204
				$v = self::toJSON ( $v );
205
			} elseif ($v != null && UString::startswith ( $v, 'js:' )) {
206
				$v = \substr ( $v, 3 );
207
			} else {
208
				$v = '"' . $v . '"';
209
			}
210
			$result [] = '"' . $k . '": ' . $v;
211
		}
212
		return '{' . \implode ( ',', $result ) . '}';
213
	}
214
}
215