Passed
Push — master ( 3495a4...f45dbf )
by Jean-Christophe
09:31
created

UArray   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 63
eloc 115
c 3
b 0
f 0
dl 0
loc 255
ccs 99
cts 132
cp 0.75
rs 3.36

22 Methods

Rating   Name   Duplication   Size   Complexity  
A toJSON() 0 13 5
A iRemove() 0 9 3
A implodeAsso() 0 6 2
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 asJSON() 0 2 1
A removeByKey() 0 5 2
A doubleBackSlashes() 0 3 1
A extractKeys() 0 8 3
A save() 0 3 1
A remove() 0 9 3
A getDefaultValue() 0 2 1
B parseValue() 0 18 11
A asPhpArray() 0 23 6
A asPhpClass() 0 12 3

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
/**
6
 * Array utilities.
7
 * Ubiquity\utils\base$UArray
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.4
12
 *
13
 */
14
class UArray {
15
16
	/**
17
	 * Tests if array is associative
18
	 *
19
	 * @param array $array
20
	 * @return boolean
21
	 */
22 1
	public static function isAssociative($array) {
23 1
		return (\array_keys ( $array ) !== \range ( 0, \count ( $array ) - 1 ));
24
	}
25
26
	/**
27
	 * Returns a new array with the keys $keys
28
	 *
29
	 * @param array $array an associative array
30
	 * @param array $keys some keys
31
	 * @return array
32
	 */
33 1
	public static function extractKeys($array, $keys) {
34 1
		$result = [ ];
35 1
		foreach ( $keys as $key ) {
36 1
			if (isset ( $array [$key] )) {
37 1
				$result [$key] = $array [$key];
38
			}
39
		}
40 1
		return $result;
41
	}
42
43
	/**
44
	 *
45
	 * @param array $array
46
	 * @param string $key
47
	 * @param int $pos
48
	 * @return mixed|null
49
	 */
50 1
	public static function getValue($array, $key, $pos) {
51 1
		if (\array_key_exists ( $key, $array )) {
52 1
			return $array [$key];
53
		}
54 1
		$values = array_values ( $array );
55 1
		if ($pos < \sizeof ( $values ))
56 1
			return $values [$pos];
57 1
	}
58
59
	/**
60
	 *
61
	 * @param array $array
62
	 * @param string|int $key
63
	 * @param mixed $default
64
	 * @return mixed
65
	 */
66 17
	public static function getRecursive($array, $key, $default = null) {
67 17
		if (\array_key_exists ( $key, $array )) {
68 17
			return $array [$key];
69
		} else {
70 17
			foreach ( $array as $item ) {
71 17
				if (is_array ( $item )) {
72 17
					return self::getRecursive ( $item, $key, $default );
73
				}
74
			}
75
		}
76 1
		return $default;
77
	}
78
79 1
	public static function getDefaultValue($array, $key, $default) {
80 1
		return $array [$key] ?? $default;
81
	}
82
83
	/**
84
	 * Save a php array to the disk.
85
	 *
86
	 * @param array $array The array to save
87
	 * @param string $filename The path of the file to save in
88
	 * @return int
89
	 */
90
	public static function save($array, $filename) {
91
		$content = "<?php\nreturn " . self::asPhpArray ( $array, "array", 1, true ) . ";";
92
		return UFileSystem::save ( $filename, $content );
93
	}
94
95 59
	public static function asPhpArray($array, $prefix = "", $depth = 1, $format = false) {
96 59
		$exts = array ();
97 59
		$extsStr = "";
98 59
		$tab = "";
99 59
		$nl = "";
100 59
		if ($format) {
101 32
			$tab = \str_repeat ( "\t", $depth );
102 32
			$nl = PHP_EOL;
103
		}
104 59
		foreach ( $array as $k => $v ) {
105 58
			if (\is_string ( $k )) {
106 58
				$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . self::parseValue ( $v, 'array', $depth + 1, $format );
107
			} else {
108 52
				$exts [] = self::parseValue ( $v, $prefix, $depth + 1, $format );
109
			}
110
		}
111 59
		if ($prefix !== '') {
112 59
			$extsStr = '()';
113
		}
114 59
		if (\sizeof ( $exts ) > 0) {
115 58
			$extsStr = "({$nl}{$tab}" . \implode ( ",{$nl}{$tab}", $exts ) . "{$nl}{$tab})";
116
		}
117 59
		return $prefix . $extsStr;
118
	}
119
120 28
	public static function asPhpClass($array, $name, $namespace = '', $format = false) {
121 28
		$tab = "";
122 28
		$nl = "";
123 28
		if ($format) {
124 28
			$tab = "\t";
125 28
			$nl = PHP_EOL;
126
		}
127 28
		$content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';';
128 28
		if ($namespace != null) {
129 28
			$namespace = "namespace {$namespace};{$nl}";
130
		}
131 28
		return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}";
132
	}
133
134 1
	public static function asJSON($array) {
135 1
		return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
136
	}
137
138 2
	public static function remove($array, $search) {
139 2
		if (\is_array ( $search )) {
140 1
			foreach ( $search as $val ) {
141 1
				$array = self::removeOne ( $array, $val );
142
			}
143
		} else {
144 2
			$array = self::removeOne ( $array, $search );
145
		}
146 2
		return \array_values ( $array );
147
	}
148
149
	/**
150
	 * Removes from array by key
151
	 *
152
	 * @param array $array
153
	 * @param int|string $key
154
	 * @return array
155
	 */
156 1
	public static function removeByKey($array, $key) {
157 1
		if (isset ( $array [$key] )) {
158 1
			unset ( $array [$key] );
159
		}
160 1
		return $array;
161
	}
162
163 16
	public static function removeRecursive(&$array, $key) {
164 16
		foreach ( $array as &$item ) {
165 16
			if (\array_key_exists ( $key, $item )) {
166 16
				unset ( $item [$key] );
167 16
			} elseif (\is_array ( $item )) {
168 16
				self::removeRecursive ( $item, $key );
169
			}
170
		}
171 16
	}
172
173
	public static function removeByKeys($array, $keys) {
174
		$assocKeys = [ ];
175
		foreach ( $keys as $key ) {
176
			$assocKeys [$key] = true;
177
		}
178
		return \array_diff_key ( $array, $assocKeys );
179
	}
180
181 3
	public static function removeOne($array, $search) {
182 3
		if (($key = \array_search ( $search, $array )) !== false) {
183 3
			unset ( $array [$key] );
184
		}
185 3
		return $array;
186
	}
187
188
	public static function update(&$array, $search, $newValue) {
189
		if (($key = \array_search ( $search, $array )) !== false) {
190
			$array [$key] = $newValue;
191
		}
192
		return $array;
193
	}
194
195
	/**
196
	 *
197
	 * @param array $array
198
	 * @return boolean
199
	 */
200
	public static function doubleBackSlashes(&$array) {
201
		return \array_walk ( $array, function (&$value) {
202
			$value = UString::doubleBackSlashes ( $value );
203
		} );
204
	}
205
206 58
	private static function parseValue($v, $prefix = "", $depth = 1, $format = false) {
207 58
		if (\is_numeric ( $v )) {
208 53
			$result = $v;
209 57
		} elseif ($v !== '' && UString::isBooleanStr ( $v )) {
210 46
			$result = UString::getBooleanStr ( $v );
211 57
		} elseif (\is_array ( $v )) {
212 54
			$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format );
213 57
		} elseif (\is_string ( $v ) && (UString::startswith ( trim ( $v ), '$config' ) || UString::startswith ( \trim ( $v ), "function" ) || UString::startswith ( \trim ( $v ), "array(" ))) {
214 9
			$result = $v;
215 57
		} elseif ($v instanceof \Closure) {
216 10
			$result = UIntrospection::closure_dump ( $v );
217 57
		} elseif ($v instanceof \DateTime) {
218
			$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')";
219
		} else {
220 57
			$result = UString::doubleBackSlashes ( $v );
221 57
			$result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
222
		}
223 58
		return $result;
224
	}
225
226 2
	public static function iSearch($needle, $haystack, $strict = null) {
227 2
		return \array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict );
228
	}
229
230
	public static function iRemove($array, $search) {
231
		if (\is_array ( $search )) {
232
			foreach ( $search as $val ) {
233
				$array = self::iRemoveOne ( $array, $val );
234
			}
235
		} else {
236
			$array = self::iRemoveOne ( $array, $search );
237
		}
238
		return \array_values ( $array );
239
	}
240
241 2
	public static function iRemoveOne($array, $search) {
242 2
		if (($key = self::iSearch ( $search, $array )) !== false) {
243 2
			unset ( $array [$key] );
244
		}
245 2
		return $array;
246
	}
247
248 8
	public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') {
249 8
		$res = [ ];
250 8
		foreach ( $array as $k => $v ) {
251
			$res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}";
252
		}
253 8
		return \implode ( $glue, $res );
254
	}
255
256
	public static function toJSON($array) {
257
		$result = [ ];
258
		foreach ( $array as $k => $v ) {
259
			if (\is_array ( $v )) {
260
				$v = self::toJSON ( $v );
261
			} elseif ($v != null && UString::startswith ( $v, 'js:' )) {
262
				$v = \substr ( $v, 3 );
263
			} else {
264
				$v = '"' . $v . '"';
265
			}
266
			$result [] = '"' . $k . '": ' . $v;
267
		}
268
		return '{' . \implode ( ',', $result ) . '}';
269
	}
270
}
271