JArray   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 73
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A implode() 0 15 4
A isAssociative() 0 4 1
A getValue() 0 8 3
B getConditionalValue() 0 14 5
A getDefaultValue() 0 6 2
A dimension() 0 8 2
A sortAssociative() 0 9 3
1
<?php
2
3
namespace Ajax\service;
4
5
class JArray {
6
7
	public static function isAssociative($array) {
8
		return (array_values($array) !== $array);
9
		// return (array_keys($array)!==range(0, count($array)-1));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
10
	}
11
12
	public static function getValue($array, $key, $pos) {
13
		if (array_key_exists($key, $array)) {
14
			return $array[$key];
15
		}
16
		$values=array_values($array);
17
		if ($pos < sizeof($values))
18
			return $values[$pos];
19
	}
20
21
	public static function getConditionalValue($array, $key, $condition) {
22
		$result=NULL;
23
		if (array_key_exists($key, $array)) {
24
			$result=$array[$key];
25
			if ($condition($result) === true)
26
				return $result;
27
		}
28
		$values=array_values($array);
29
		foreach ( $values as $val ) {
30
			if ($condition($val) === true)
31
				return $val;
32
		}
33
		return $result;
34
	}
35
36
	public static function getDefaultValue($array, $key, $default=NULL) {
37
		if (array_key_exists($key, $array)) {
38
			return $array[$key];
39
		} else
40
			return $default;
41
	}
42
43
	public static function implode($glue, $pieces) {
44
		$result="";
45
		if (\is_array($glue)) {
46
			$size=\sizeof($pieces);
47
			if ($size > 0) {
48
				for($i=0; $i < $size - 1; $i++) {
49
					$result.=$pieces[$i] . @$glue[$i];
50
				}
51
				$result.=$pieces[$size - 1];
52
			}
53
		} else {
54
			$result=\implode($glue, $pieces);
55
		}
56
		return $result;
57
	}
58
59
	public static function dimension($array) {
60
		if (is_array(reset($array))) {
61
			$return=self::dimension(reset($array)) + 1;
62
		} else {
63
			$return=1;
64
		}
65
		return $return;
66
	}
67
68
	public static function sortAssociative($array, $sortedKeys=array()) {
69
		$newArray=array ();
70
		foreach ( $sortedKeys as $key ) {
71
			if (\array_key_exists($key, $array)) {
72
				$newArray[$key]=$array[$key];
73
			}
74
		}
75
		return $newArray;
76
	}
77
}