Completed
Push — master ( a814e8...9f92c8 )
by mw
67:27 queued 50:14
created

StatsFormatter::format()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.0935

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 16
nop 2
dl 0
loc 45
ccs 19
cts 24
cp 0.7917
crap 12.0935
rs 5.2653
c 0
b 0
f 0

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 SMW;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 2.5
8
 *
9
 * @author mwjames
10
 */
11
class StatsFormatter {
12
13
	/**
14
	 * Stats as plain string
15
	 */
16
	const FORMAT_PLAIN = 'plain';
17
18
	/**
19
	 * Stats as JSON output
20
	 */
21
	const FORMAT_JSON = 'json';
22
23
	/**
24
	 * Stats as HTML list output
25
	 */
26
	const FORMAT_HTML = 'html';
27
28
	/**
29
	 * @since 2.5
30
	 *
31
	 * @param array $stats
32
	 * @param string|null $format
33
	 *
34
	 * @return string|array
35
	 */
36 4
	public static function format( array $stats, $format = null ) {
37
38 4
		$output = '';
39
40 4
		if ( $format === self::FORMAT_PLAIN ) {
41 1
			foreach ( $stats as $key => $value ) {
42 1
				$output .= '- ' . $key . "\n";
43
44 1
				if ( !is_array( $value ) ) {
45 1
					continue;
46
				}
47
48
				foreach ( $value as $k => $v ) {
49
					$output .= '  - ' . $k . ': ' . $v . "\n";
50
				}
51
			}
52
		}
53
54 4
		if ( $format === self::FORMAT_HTML ) {
55 1
			$output .= '<ul>';
56 1
			foreach ( $stats as $key => $value ) {
57 1
				$output .= '<li>' . $key . '<ul>';
58
59 1
				if ( !is_array( $value ) ) {
60 1
					continue;
61
				}
62
63
				foreach ( $value as $k => $v ) {
64
					$output .= '<li>' . $k . ': ' . $v . "</li>";
65
				}
66
				$output .= '</ul></li>';
67
			}
68 1
			$output .= '</ul>';
69
		}
70
71 4
		if ( $format === self::FORMAT_JSON ) {
72 1
			$output .= json_encode( $stats, JSON_PRETTY_PRINT );
73
		}
74
75 4
		if ( $format === null ) {
76 1
			$output = $stats;
77
		}
78
79 4
		return $output;
80
	}
81
82
	/**
83
	 * @since 2.5
84
	 *
85
	 * @param array $stats
86
	 * @param string $separator
87
	 *
88
	 * @return array
89
	 */
90 3
	public static function getStatsFromFlatKey( array $stats, $separator = '.' ) {
91
92 3
		$data = $stats;
93 3
		$stats = array();
94
95 3
		foreach ( $data as $key => $value ) {
96 3
			if ( strpos( $key, $separator ) !== false ) {
97 2
				$stats = array_merge_recursive( $stats, self::stringToArray( $separator, $key, $value ) );
98
			} else {
99 3
				$stats[$key] = $value;
100
			}
101
		}
102
103 3
		return $stats;
104
	}
105
106
	// http://stackoverflow.com/questions/10123604/multstatsdIdimensional-array-from-string
107 2
	private static function stringToArray( $separator, $path, $value ) {
108
109 2
		$pos = strpos( $path, $separator );
110
111 2
		if ( $pos === false ) {
112 2
			return array( $path => $value );
113
		}
114
115 2
		$key = substr( $path, 0, $pos );
116 2
		$path = substr( $path, $pos + 1 );
117
118
		$result = array(
119 2
			$key => self::stringToArray( $separator, $path, $value )
120
		);
121
122 2
		return $result;
123
	}
124
125
}
126