Completed
Push — master ( 782061...10806f )
by Jean-Christophe
01:37
created

UArray   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 189
Duplicated Lines 10.58 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 52
lcom 0
cbo 2
dl 20
loc 189
rs 7.44
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A extractKeys() 0 9 3
A isAssociative() 0 3 1
A getValue() 0 8 3
A getRecursive() 0 12 4
A getDefaultValue() 0 6 2
B asPhpArray() 0 22 7
A asJSON() 0 3 1
A remove() 10 10 3
A removeByKey() 0 6 2
A removeRecursive() 0 9 4
A removeByKeys() 0 7 2
A removeOne() 0 6 2
A update() 0 6 2
A doubleBackSlashes() 0 3 1
B parseValue() 0 17 9
A iSearch() 0 3 1
A iRemove() 10 10 3
A iRemoveOne() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
 * @author jc
8
 *
9
 */
10
class UArray {
11
12
	/**
13
	 * Tests if array is associative
14
	 * @param array $array
15
	 * @return boolean
16
	 */
17
	public static function isAssociative($array) {
18
		return (array_keys($array) !== range(0, count($array) - 1));
19
	}
20
	
21
	/**
22
	 * Returns a new array with the keys $keys
23
	 * @param array $array an associative array
24
	 * @param array $keys some keys
25
	 * @return array
26
	 */
27
	public static function extractKeys($array,$keys){
28
		$result=[];
29
		foreach ($keys as $key){
30
			if(isset($array[$key])){
31
				$result[$key]=$array[$key];
32
			}
33
		}
34
		return $result;
35
	}
36
37
	public static function getValue($array, $key, $pos) {
38
		if (array_key_exists($key, $array)) {
39
			return $array[$key];
40
		}
41
		$values=array_values($array);
42
		if ($pos < sizeof($values))
43
			return $values[$pos];
44
	}
45
	
46
	public static function getRecursive($array,$key,$default=null){
47
		if(array_key_exists($key,$array)){
48
			return $array[$key];
49
		}else{
50
			foreach ($array as $item){
51
				if(is_array($item)){
52
					return self::getRecursive($item, $key,$default);
53
				}
54
			}
55
		}
56
		return $default;
57
	}
58
59
	public static function getDefaultValue($array, $key, $default) {
60
		if (array_key_exists($key, $array)) {
61
			return $array[$key];
62
		} else
63
			return $default;
64
	}
65
66
	public static function asPhpArray($array, $prefix="",$depth=1,$format=false) {
67
		$exts=array ();
68
		$extsStr="";$tab="";$nl="";
69
		if($format){
70
			$tab=str_repeat("\t",$depth);
71
			$nl=PHP_EOL;
72
		}
73
		foreach ( $array as $k => $v ) {
74
			if(is_string($k)){
75
				$exts[]="\"" . UString::doubleBackSlashes($k) . "\"=>" . self::parseValue($v, 'array',$depth+1,$format);
76
			}else{
77
				$exts[]=self::parseValue($v, $prefix,$depth+1,$format);
78
			}
79
		}
80
		if (\sizeof($exts) > 0 || $prefix !== "") {
81
			$extsStr="(" . \implode(",".$nl.$tab, $exts).")";
82
			if(\sizeof($exts)>0){
83
				$extsStr="(" .$nl.$tab. \implode(",".$nl.$tab, $exts).$nl.$tab.")";
84
			}
85
		}
86
		return $prefix . $extsStr;
87
	}
88
	
89
	public static function asJSON($array){
90
		return json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
91
	}
92
93 View Code Duplication
	public static function remove($array, $search) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
		if (\is_array($search)) {
95
			foreach ( $search as $val ) {
96
				$array=self::removeOne($array, $val);
97
			}
98
		} else {
99
			$array=self::removeOne($array, $search);
100
		}
101
		return array_values($array);
102
	}
103
	
104
	/**
105
	 * Removes from array by key
106
	 * @param array $array
107
	 * @param int|string $key
108
	 * @return array
109
	 */
110
	public static function removeByKey($array,$key){
111
		if(isset($array[$key])){
112
			unset($array[$key]);
113
		}
114
		return $array;
115
	}
116
	
117
	public static function removeRecursive(&$array,$key){
118
		foreach ($array as &$item){
119
			if(array_key_exists($key, $item)){
120
				unset($item[$key]);
121
			}elseif(is_array($item)){
122
				self::removeRecursive($item, $key);
123
			}
124
		}
125
	}
126
	
127
	public static function removeByKeys($array,$keys){
128
		$assocKeys = [];
129
		foreach($keys as $key) {
130
			$assocKeys[$key] = true;
131
		}
132
		return array_diff_key($array, $assocKeys);
133
	}
134
135
	public static function removeOne($array, $search) {
136
		if (($key=array_search($search, $array)) !== false) {
137
			unset($array[$key]);
138
		}
139
		return $array;
140
	}
141
142
	public static function update(&$array, $search, $newValue) {
143
		if (($key=array_search($search, $array)) !== false) {
144
			$array[$key]=$newValue;
145
		}
146
		return $array;
147
	}
148
	
149
	/**
150
	 * @param array $array
151
	 * @return boolean
152
	 */
153
	public static function doubleBackSlashes(&$array){
154
		return array_walk($array, function(&$value){$value=UString::doubleBackSlashes($value);});
155
	}
156
157
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
158
		if (UString::isBooleanStr($v)) {
159
			$result=UString::getBooleanStr($v);
160
		} elseif (\is_numeric($v)) {
161
			$result=$v;
162
		} elseif (\is_array($v)) {
163
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
164
		}elseif(is_string($v) && (UString::startswith(trim($v), '$config') || UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array("))){
165
			$result=$v;
166
		}elseif($v instanceof \Closure){
167
			$result=UIntrospection::closure_dump($v);
168
		}else {
169
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
170
			$result=UString::doubleBackSlashes($result);
171
		}
172
		return $result;
173
	}
174
	
175
	public static function iSearch($needle,$haystack,$strict=null){
176
		return array_search(strtolower($needle), array_map('strtolower', $haystack),$strict);
177
	}
178
	
179 View Code Duplication
	public static function iRemove($array, $search) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
		if (\is_array($search)) {
181
			foreach ( $search as $val ) {
182
				$array=self::iRemoveOne($array, $val);
183
			}
184
		} else {
185
			$array=self::iRemoveOne($array, $search);
186
		}
187
		return array_values($array);
188
	}
189
	
190
	public static function iRemoveOne($array, $search) {
191
		if (($key=self::iSearch($search, $array)) !== false) {
192
			unset($array[$key]);
193
		}
194
		return $array;
195
	}
196
	
197
	
198
}
199