Completed
Push — master ( 617758...6fedbf )
by Martin
05:23
created

DumpExtension::doDump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
1
<?php
2
namespace Yep\TracyTwigExtensions;
3
4
use Tracy\Dumper;
5
6
class DumpExtension extends \Twig_Extension {
7
	protected $name = 'dump';
8
	protected $options;
9
10 6
	public function __construct(array $options = []) {
11 6
		$this->options = $options;
12 6
	}
13
14 2
	public function getFunctions() {
15
		return [
16 2
			new \Twig_SimpleFunction($this->name, [$this, 'dump'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]),
17 2
		];
18
	}
19
20 4
	public function getName() {
21 4
		return $this->name;
22
	}
23
24 4
	public function dump(\Twig_Environment $environment, $context) {
25 4
		if (!$environment->isDebug()) {
26 2
			return '';
27
		}
28
29 2
		$arguments = func_get_args();
30 2
		array_shift($arguments);
31 2
		array_shift($arguments);
32 2
		$count = count($arguments);
33
34 2
		if ($count === 0) {
35 2
			$arguments = $context;
36 2
		}
37
38 2
		if ($count === 1) {
39 2
			$arguments = array_shift($arguments);
40 2
		}
41
42 2
		return $this->doDump($arguments);
43
	}
44
45 1
	protected function doDump($data) {
46 1
		if (!class_exists('\Tracy\Dumper')) {
47
			return '';
48
		}
49
50 1
		ob_start();
51 1
		Dumper::dump($data, $this->options);
52
53 1
		return ob_get_clean();
54
	}
55
}
56