Completed
Push — master ( 8ea584...86551f )
by Michal
15:15
created

IncidentsHelper::getStacktrace()   C

Complexity

Conditions 10
Paths 26

Size

Total Lines 48
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 17.6766

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 48
ccs 23
cts 40
cp 0.575
rs 5.3455
cc 10
eloc 37
nc 26
nop 2
crap 17.6766

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
/* vim: set noexpandtab sw=2 ts=2 sts=2: */
3
namespace app\View\Helper;
4
5
use App\Utility\Sanitize;
6
use App\View\Helper\AppHelper;
7
use Cake\View\View;
8
9
class IncidentsHelper extends AppHelper {
10
11 3
	public function __construct(View $view, $settings = array()) {
12 3
		parent::__construct($view, $settings);
13 3
	}
14
15 1
	public function createIncidentsLinks($incidents) {
16 1
		$links = array();
17 1
		foreach ($incidents as $incident) {
18 1
			$links[] = $this->linkToIncident($incident);
19 1
		}
20 1
		$string = implode(", ", $links);
21 1
		return $string;
22
	}
23
24 1
	public function linkToIncident($incident) {
25 1
		$incidentId = $incident["id"];
26 1
		$link = "<a href='/" . BASE_DIR . "incidents/view/$incidentId'>#$incidentId</a>";
27 1
		return $link;
28
	}
29
30 1
	public function incidentsDescriptions($incidents) {
31 1
		$descriptions = "";
32 1
		foreach ($incidents as $incident) {
33 1
			$descriptions .= "<span>Incident ";
34 1
			$descriptions .= $this->linkToIncident($incident);
35 1
			$descriptions .= ":</span>";
36 1
			$descriptions .= "<pre>";
37 1
			$descriptions .= $incident["steps"];
38 1
			$descriptions .= "</pre>";
39 1
		}
40 1
		return $descriptions;
41
	}
42
43 2
	public function getStacktrace($incident, $divClass) {
44 2
		$html = "";
45 2
		$html .= "<div class='$divClass'>";
46
47 2
		if (is_string($incident["stacktrace"])) {
48 1
			$incident["stacktrace"] =
49 1
					json_decode($incident["stacktrace"], true);
50 1
		}
51
52 2
		foreach ($incident["stacktrace"] as $level) {
53 2
			$exception_type = (($incident["exception_type"])?('php'):('js'));
54 2
			$html .= $this->_getStackLevelInfo($level, $exception_type);
55
			$html .= "<pre class='brush: "
56
				. $exception_type
57 2
				. "; tab-size: 2";
58 2
			if ($incident['exception_type']
59 2
				&& isset($level['line'])
60 2
				&& $level['line']
61 2
			) {
62
				$html .= "; first-line: " . $level['line'];
63
			}
64
65 2
			$html .= "'>";
66 2
			if($exception_type == 'js') {
67 2
				$html .= join("\n", $level["context"]);
68 2
			} else {
69
				$html .= $level["function"];
70
				$html .= "(";
71
				$argList = "";
72
				if (count($level["args"]) > 0) {
73
					foreach ($level["args"] as $arg) {
74
						$argList .= "\n"
75
							. getType($arg)
76
							. " => "
77
							. $arg;
78
						$argList .= ",";
79
					}
80
					$argList = substr($argList, 0, (strlen($argList)-1));
81
					$argList .= "\n";
82
				}
83
				$html .= $argList;
84
				$html .= ")";
85
			}
86 2
			$html .= "</pre>";
87 2
		}
88 2
		$html .= "</div>";
89 2
		return $html;
90
	}
91
92 2
	protected function _getStackLevelInfo($level, $exception_type = 'js') {
93 2
		$html = "<span>";
94 2
		if ($exception_type == 'js') {
95 2
			$elements = array("filename", "scriptname", "line", "func", "column");
96 2
		} else {
97
			$elements = array("file", "line", "function", "class");
98
		}
99 2
		foreach ($elements as $element) {
100 2
			if (isset($level[$element])) {
101
				$html .= "$element: " . $level[$element] . "; ";
102
			}
103 2
		}
104 2
		$html .= "</span>";
105 2
		return $html;
106
	}
107
}
108