Passed
Push — master ( d5c6e8...acf3eb )
by Jean-Christophe
06:08
created

UArray::parseValue()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 6
nop 4
crap 10

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
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.2
12
 *
13
 */
14
class UArray {
15
16
	/**
17
	 * Tests if array is associative
18
	 *
19
	 * @param array $array
20
	 * @return boolean
21
	 */
22 34
	public static function isAssociative($array) {
23 34
		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 3
	public static function getRecursive($array, $key, $default = null) {
67 3
		if (array_key_exists ( $key, $array )) {
68 3
			return $array [$key];
69
		} else {
70 2
			foreach ( $array as $item ) {
71 2
				if (is_array ( $item )) {
72 2
					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 26
	public static function asPhpArray($array, $prefix = "", $depth = 1, $format = false) {
84 26
		$exts = array ();
85 26
		$extsStr = "";
86 26
		$tab = "";
87 26
		$nl = "";
88 26
		if ($format) {
89 2
			$tab = str_repeat ( "\t", $depth );
90 2
			$nl = PHP_EOL;
91
		}
92 26
		foreach ( $array as $k => $v ) {
93 25
			if (is_string ( $k )) {
94 25
				$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . self::parseValue ( $v, 'array', $depth + 1, $format );
95
			} else {
96 23
				$exts [] = self::parseValue ( $v, $prefix, $depth + 1, $format );
97
			}
98
		}
99 26
		if (\sizeof ( $exts ) > 0 || $prefix !== "") {
100 26
			$extsStr = "(" . \implode ( "," . $nl . $tab, $exts ) . ")";
101 26
			if (\sizeof ( $exts ) > 0) {
102 25
				$extsStr = "(" . $nl . $tab . \implode ( "," . $nl . $tab, $exts ) . $nl . $tab . ")";
103
			}
104
		}
105 26
		return $prefix . $extsStr;
106
	}
107
108
	public static function asJSON($array) {
109
		return json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
110
	}
111
112 1
	public static function remove($array, $search) {
113 1
		if (\is_array ( $search )) {
114
			foreach ( $search as $val ) {
115
				$array = self::removeOne ( $array, $val );
116
			}
117
		} else {
118 1
			$array = self::removeOne ( $array, $search );
119
		}
120 1
		return array_values ( $array );
121
	}
122
123
	/**
124
	 * Removes from array by key
125
	 *
126
	 * @param array $array
127
	 * @param int|string $key
128
	 * @return array
129
	 */
130
	public static function removeByKey($array, $key) {
131
		if (isset ( $array [$key] )) {
132
			unset ( $array [$key] );
133
		}
134
		return $array;
135
	}
136
137 2
	public static function removeRecursive(&$array, $key) {
138 2
		foreach ( $array as &$item ) {
139 2
			if (array_key_exists ( $key, $item )) {
140 2
				unset ( $item [$key] );
141 1
			} elseif (is_array ( $item )) {
142 1
				self::removeRecursive ( $item, $key );
143
			}
144
		}
145 2
	}
146
147 2
	public static function removeByKeys($array, $keys) {
148 2
		$assocKeys = [ ];
149 2
		foreach ( $keys as $key ) {
150 2
			$assocKeys [$key] = true;
151
		}
152 2
		return array_diff_key ( $array, $assocKeys );
153
	}
154
155 1
	public static function removeOne($array, $search) {
156 1
		if (($key = array_search ( $search, $array )) !== false) {
157 1
			unset ( $array [$key] );
158
		}
159 1
		return $array;
160
	}
161
162
	public static function update(&$array, $search, $newValue) {
163
		if (($key = array_search ( $search, $array )) !== false) {
164
			$array [$key] = $newValue;
165
		}
166
		return $array;
167
	}
168
169
	/**
170
	 *
171
	 * @param array $array
172
	 * @return boolean
173
	 */
174
	public static function doubleBackSlashes(&$array) {
175
		return array_walk ( $array, function (&$value) {
176
			$value = UString::doubleBackSlashes ( $value );
177
		} );
178
	}
179
180 25
	private static function parseValue($v, $prefix = "", $depth = 1, $format = false) {
181 25
		if (\is_numeric ( $v )) {
182 23
			$result = $v;
183 24
		} elseif ($v !== '' && UString::isBooleanStr ( $v )) {
184 16
			$result = UString::getBooleanStr ( $v );
185 24
		} elseif (\is_array ( $v )) {
186 24
			$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format );
187 24
		} elseif (is_string ( $v ) && (UString::startswith ( trim ( $v ), '$config' ) || UString::startswith ( trim ( $v ), "function" ) || UString::startswith ( trim ( $v ), "array(" ))) {
188 2
			$result = $v;
189 24
		} elseif ($v instanceof \Closure) {
190 4
			$result = UIntrospection::closure_dump ( $v );
191
		} else {
192 24
			$result = "\"" . \str_replace ( '$', '\$', $v ) . "\"";
193 24
			$result = UString::doubleBackSlashes ( $result );
194
		}
195 25
		return $result;
196
	}
197
198 2
	public static function iSearch($needle, $haystack, $strict = null) {
199 2
		return array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict );
200
	}
201
202
	public static function iRemove($array, $search) {
203
		if (\is_array ( $search )) {
204
			foreach ( $search as $val ) {
205
				$array = self::iRemoveOne ( $array, $val );
206
			}
207
		} else {
208
			$array = self::iRemoveOne ( $array, $search );
209
		}
210
		return array_values ( $array );
211
	}
212
213 2
	public static function iRemoveOne($array, $search) {
214 2
		if (($key = self::iSearch ( $search, $array )) !== false) {
215 2
			unset ( $array [$key] );
216
		}
217 2
		return $array;
218
	}
219
220 2
	public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') {
221 2
		$res = [ ];
222 2
		foreach ( $array as $k => $v ) {
223
			$res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}";
224
		}
225 2
		return implode ( $glue, $res );
226
	}
227
}
228