Completed
Push — master ( f4acaf...6e85d2 )
by Thomas
02:25
created

CollectionUtils   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
c 4
b 1
f 1
lcom 1
cbo 2
dl 0
loc 79
ccs 22
cts 24
cp 0.9167
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromCollection() 0 7 3
A fromArray() 0 3 1
B toCollection() 0 19 7
A toMap() 0 8 2
A toList() 0 7 2
1
<?php
2
namespace phootwork\collection;
3
4
use \Iterator;
5
use \InvalidArgumentException;
6
7
/**
8
 * CollectionUtils help to transform data recursively into collections. 
9
 * 
10
 * It must be mentioned the API is experimental and may change. Please 
11
 * report to the issue tracker.
12
 */
13
class CollectionUtils {
14
	
15
	/**
16
	 * Returns a proper collection for the given array (also transforms nested collections) 
17
	 * (experimental API)
18
	 * 
19
	 * @param array|Iterator $collection
20
	 * @return Map|ArrayList the collection
21
	 * @throws InvalidArgumentException
22
	 */
23 7
	public static function fromCollection($collection) {
24 7
		if (!(is_array($collection) || $collection instanceof Iterator)) {
25 1
			throw new InvalidArgumentException('$collection is neither an array nor an iterator');
26
		}
27
28 6
		return self::toCollection($collection);
29
	}
30
	
31
	/**
32
	 * @deprecated use fromCollection instead (will be removed in version 1.3)
33
	 * @param Iterator $array
34
	 * @return Map|ArrayList the collection
35
	 */
36
	public static function fromArray($array) {
37
		return self::fromCollection($array);
38
	}
39
	
40 8
	private static function toCollection($data) {
41
		// prepare normal array
42 8
		if (!($data instanceof Iterator)) {
43 8
			$data = json_decode(json_encode($data));
44
		}
45
46
		// check if we can transform it into a collection or just return as is
47 8
		if (!(is_array($data) || $data instanceof Iterator || $data instanceof \stdClass)) {
48 8
			return $data;
49
		}
50
51
		// check we have a list
52 8
		if (is_array($data) || $data instanceof AbstractList) {
53 6
			return self::toList($data);
54
		}
55
		
56
		// everything else must be a map
57 6
		return self::toMap($data);
1 ignored issue
show
Bug introduced by
It seems like $data defined by json_decode(json_encode($data)) on line 43 can also be of type object<stdClass>; however, phootwork\collection\CollectionUtils::toMap() does only seem to accept array|object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
	}
59
	
60
	/**
61
	 * Recursively transforms data into a map (on the first level, deeper levels 
62
	 * transformed to an appropriate collection) (experimental API)
63
	 * 
64
	 * @param array|Iterator $collection
65
	 * @return Map
66
	 */
67 6
	public static function toMap($collection) {
68 6
		$map = new Map();
69 6
		foreach ($collection as $k => $v) {
70 6
			$map->set($k, self::toCollection($v));
71
		}
72
		
73 6
		return $map;
74
	}
75
	
76
	/**
77
	 * Recursively transforms data into a list (on the first level, deeper levels 
78
	 * transformed to an appropriate collection) (experimental API)
79
	 *
80
	 * @param array|Iterator $collection
81
	 * @return ArrayList
82
	 */
83 6
	public static function toList($collection) {
84 6
		$list = new ArrayList();
85 6
		foreach ($collection as $v) {
86 6
			$list->add(self::toCollection($v));
87
		}
88 6
		return $list;
89
	}
90
91
}
92