Completed
Pull Request — gcconnex (#1540)
by Nick
16:17
created

start.php ➔ data_views_object_to_xml()   C

Complexity

Conditions 14
Paths 96

Size

Total Lines 33
Code Lines 20

Duplication

Lines 15
Ratio 45.45 %

Importance

Changes 0
Metric Value
cc 14
eloc 20
nc 96
nop 3
dl 15
loc 33
rs 5.0864
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
/**
4
 * This function serialises an object recursively into an XML representation.
5
 *
6
 * The function attempts to call $data->export() which expects a \stdClass in return,
7
 * otherwise it will attempt to get the object variables using get_object_vars (which
8
 * will only return public variables!)
9
 *
10
 * @param mixed  $data The object to serialise.
11
 * @param string $name The name?
12
 * @param int    $n    Level, only used for recursion.
13
 *
14
 * @return string The serialised XML output.
15
 */
16
function data_views_object_to_xml($data, $name = "", $n = 0) {
17
	$classname = ($name == "" ? get_class($data) : $name);
18
19
	$vars = method_exists($data, "export") ? get_object_vars($data->export()) : get_object_vars($data);
20
21
	$output = "";
22
23
	if (($n == 0) || ( is_object($data) && !($data instanceof \stdClass))) {
24
		$output = "<$classname>";
25
	}
26
27 View Code Duplication
	foreach ($vars as $key => $value) {
28
		$output .= "<$key type=\"" . gettype($value) . "\">";
29
30
		if (is_object($value)) {
31
			$output .= data_views_object_to_xml($value, $key, $n + 1);
32
		} else if (is_array($value)) {
33
			$output .= data_views_array_to_xml($value, $n + 1);
34
		} else if (gettype($value) == "boolean") {
35
			$output .= $value ? "true" : "false";
36
		} else {
37
			$output .= htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
38
		}
39
40
		$output .= "</$key>\n";
41
	}
42
43
	if (($n == 0) || (is_object($data) && !($data instanceof \stdClass))) {
44
		$output .= "</$classname>\n";
45
	}
46
47
	return $output;
48
}
49
50
/**
51
 * Serialise an array.
52
 *
53
 * @param array $data The data to serialize
54
 * @param int   $n    Used for recursion
55
 *
56
 * @return string
57
 */
58
function data_views_array_to_xml(array $data, $n = 0) {
59
	$output = "";
60
61
	if ($n == 0) {
62
		$output = "<array>\n";
63
	}
64
65 View Code Duplication
	foreach ($data as $key => $value) {
66
		$item = "array_item";
67
68
		if (is_numeric($key)) {
69
			$output .= "<$item name=\"$key\" type=\"" . gettype($value) . "\">";
70
		} else {
71
			$item = $key;
72
			$output .= "<$item type=\"" . gettype($value) . "\">";
73
		}
74
75
		if (is_object($value)) {
76
			$output .= data_views_object_to_xml($value, "", $n + 1);
77
		} else if (is_array($value)) {
78
			$output .= data_views_array_to_xml($value, $n + 1);
79
		} else if (gettype($value) == "boolean") {
80
			$output .= $value ? "true" : "false";
81
		} else {
82
			$output .= htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
83
		}
84
85
		$output .= "</$item>\n";
86
	}
87
88
	if ($n == 0) {
89
		$output .= "</array>\n";
90
	}
91
92
	return $output;
93
}
94