Completed
Push — master ( 8d3377...5d9cd9 )
by mw
232:45 queued 197:48
created

StatsFormatter::format()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 16
nop 2
dl 0
loc 45
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\Utils;
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
	public static function format( array $stats, $format = null ) {
37
38
		$output = '';
39
40
		if ( $format === self::FORMAT_PLAIN ) {
41
			foreach ( $stats as $key => $value ) {
42
				$output .= '- ' . $key . "\n";
43
44
				if ( !is_array( $value ) ) {
45
					continue;
46
				}
47
48
				foreach ( $value as $k => $v ) {
49
					$output .= '  - ' . $k . ': ' . $v . "\n";
50
				}
51
			}
52
		}
53
54
		if ( $format === self::FORMAT_HTML ) {
55
			$output .= '<ul>';
56
			foreach ( $stats as $key => $value ) {
57
				$output .= '<li>' . $key . '<ul>';
58
59
				if ( !is_array( $value ) ) {
60
					continue;
61
				}
62
63
				foreach ( $value as $k => $v ) {
64
					$output .= '<li>' . $k . ': ' . $v . "</li>";
65
				}
66
				$output .= '</ul></li>';
67
			}
68
			$output .= '</ul>';
69
		}
70
71
		if ( $format === self::FORMAT_JSON ) {
72
			$output .= json_encode( $stats, JSON_PRETTY_PRINT );
73
		}
74
75
		if ( $format === null ) {
76
			$output = $stats;
77
		}
78
79
		return $output;
80
	}
81
82
	/**
83
	 * @since 2.5
84
	 *
85
	 * @param array $stats
86
	 * @param string $separator
87
	 *
88
	 * @return array
89
	 */
90
	public static function getStatsFromFlatKey( array $stats, $separator = '.' ) {
91
92
		$data = $stats;
93
		$stats = array();
94
95
		foreach ( $data as $key => $value ) {
96
			if ( strpos( $key, $separator ) !== false ) {
97
				$stats = array_merge_recursive( $stats, self::stringToArray( $separator, $key, $value ) );
98
			} else {
99
				$stats[$key] = $value;
100
			}
101
		}
102
103
		return $stats;
104
	}
105
106
	// http://stackoverflow.com/questions/10123604/multstatsdIdimensional-array-from-string
107
	private static function stringToArray( $separator, $path, $value ) {
108
109
		$pos = strpos( $path, $separator );
110
111
		if ( $pos === false ) {
112
			return array( $path => $value );
113
		}
114
115
		$key = substr( $path, 0, $pos );
116
		$path = substr( $path, $pos + 1 );
117
118
		$result = array(
119
			$key => self::stringToArray( $separator, $path, $value )
120
		);
121
122
		return $result;
123
	}
124
125
}
126